function PDF_html_2_pdf($data) { global $auth; $pdf = new HTML2FPDF(); $pdf->UseCSS(); $pdf->UseTableHeader(); $pdf->AddPage(); $pdf->WriteHTML(AdjustHTML($data)); $pdf->Output(); }
function WriteHTML($html) { //! @desc HTML parser //! @return void /* $e == content */ $this->ReadMetaTags($html); $html = AdjustHTML($html, $this->usepre); //Try to make HTML look more like XHTML if ($this->usecss) { $html = $this->ReadCSS($html); } //Add new supported tags in the DisableTags function $html = str_replace('<?php', '< ', $html); //Fix '<?XML' bug from HTML code generated by MS Word //$html = str_replace ("%2", "/", $html); $html = strip_tags($html, $this->enabledtags); //remove all unsupported tags, but the ones inside the 'enabledtags' string //Explode the string in order to parse the HTML code $a = preg_split('/<(.*?)>/ms', $html, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($a as $i => $e) { if ($i % 2 == 0) { //TEXT //Adjust lineheight // $this->lineheight = (5*$this->FontSizePt)/11; //should be inside printbuffer? //Adjust text, if needed if (strpos($e, "&") !== false) { if (strpos($e, "#") !== false) { $e = value_entity_decode($e); } // Decode value entities //Avoid crashing the script on PHP 4.0 $version = phpversion(); $version = str_replace('.', '', $version); if ($version >= 430) { $e = html_entity_decode($e, ENT_QUOTES, 'cp1252'); } else { $e = lesser_entity_decode($e); } } $e = str_replace(chr(160), chr(32), $e); //unify ascii code of spaces (in order to recognize all of them correctly) if (strlen($e) == 0) { continue; } if ($this->divrevert) { $e = strrev($e); } if ($this->toupper) { $e = strtoupper($e); } if ($this->tolower) { $e = strtolower($e); } //Start of 'if/elseif's if ($this->titulo) { $this->SetTitle($e); } elseif ($this->specialcontent) { if ($this->specialcontent == "type=select" and $this->selectoption['ACTIVE'] == true) { $stringwidth = $this->GetStringWidth($e); if (!isset($this->selectoption['MAXWIDTH']) or $stringwidth > $this->selectoption['MAXWIDTH']) { $this->selectoption['MAXWIDTH'] = $stringwidth; } if (!isset($this->selectoption['SELECTED']) or $this->selectoption['SELECTED'] == '') { $this->selectoption['SELECTED'] = $e; } } else { $this->textbuffer[] = array("???" . $this->specialcontent . "???" . $e); } } elseif ($this->tablestart) { if ($this->tdbegin) { $this->cell[$this->row][$this->col]['textbuffer'][] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', $this->strike, $this->outlineparam, $this->bgcolorarray); $this->cell[$this->row][$this->col]['text'][] = $e; $this->cell[$this->row][$this->col]['s'] += $this->GetStringWidth($e); } //Ignore content between <table>,<tr> and a <td> tag (this content is usually only a bunch of spaces) } elseif ($this->pbegin or $this->HREF or $this->divbegin or $this->SUP or $this->SUB or $this->strike or $this->buffer_on) { $this->textbuffer[] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', $this->strike, $this->outlineparam, $this->bgcolorarray); } else { if ($this->blockjustfinished) { $e = ltrim($e); } if ($e != '') { $this->Write($this->lineheight, $e); //Write text directly in the PDF if ($this->pjustfinished) { $this->pjustfinished = false; } } } } else { $this->colorarray = array(); $this->strike = false; //Tag if ($e[0] == '/') { $this->CloseTag(strtoupper(substr($e, 1))); } else { $regexp = '|=\'(.*?)\'|s'; // eliminate single quotes, if any $e = preg_replace($regexp, "=\"\$1\"", $e); $regexp = '| (\\w+?)=([^\\s>"]+)|si'; // changes anykey=anyvalue to anykey="anyvalue" (only do this when this happens inside tags) $e = preg_replace($regexp, " \$1=\"\$2\"", $e); //Fix path values, if needed if (stristr($e, "href=") !== false or stristr($e, "src=") !== false) { $regexp = '/ (href|src)="(.*?)"/i'; preg_match($regexp, $e, $auxiliararray); $path = $auxiliararray[2]; $path = str_replace("\\", "/", $path); //If on Windows //Get link info and obtain its absolute path $regexp = '|^./|'; $path = preg_replace($regexp, '', $path); if ($path[0] != '#') { if (strpos($path, "../") !== false) { $backtrackamount = substr_count($path, "../"); $maxbacktrack = substr_count($this->basepath, "/") - 1; $filepath = str_replace("../", '', $path); $path = $this->basepath; //If it is an invalid relative link, then make it go to directory root if ($backtrackamount > $maxbacktrack) { $backtrackamount = $maxbacktrack; } //Backtrack some directories for ($i = 0; $i < $backtrackamount + 1; $i++) { $path = substr($path, 0, strrpos($path, "/")); } $path = $path . "/" . $filepath; //Make it an absolute path } elseif (strpos($path, ":/") === false) { $path = $this->basepath . $path; } //Do nothing if it is an Absolute Link } $regexp = '/ (href|src)="(.*?)"/i'; $e = preg_replace($regexp, ' \\1="' . $path . '"', $e); } //END of Fix path values //Extract attributes $contents = array(); preg_match_all('/\\S*=["\'][^"\']*["\']/', $e, $contents); preg_match('/\\S+/', $e, $a2); $tag = strtoupper($a2[0]); $attr = array(); if (!empty($contents)) { foreach ($contents[0] as $v) { if (ereg('^([^=]*)=["\']?([^"\']*)["\']?$', $v, $a3)) { $attr[strtoupper($a3[1])] = $a3[2]; } } } $this->OpenTag($tag, $attr); } } } //end of foreach($a as $i=>$e) //Create Internal Links, if needed if (!empty($this->internallink)) { foreach ($this->internallink as $k => $v) { if (strpos($k, "#") !== false) { continue; } //ignore $ypos = $v['Y']; $pagenum = $v['PAGE']; $sharp = "#"; while (array_key_exists($sharp . $k, $this->internallink)) { $internallink = $this->internallink[$sharp . $k]; $this->SetLink($internallink, $ypos, $pagenum); $sharp .= "#"; } } } }
<?php require_once "setPDF.php"; // เพิ่มหน้าใน PDF $pdf->AddPage(); // กำหนด HTML code หรือรับค่าจากตัวแปรที่ส่งมา // กรณีกำหนดโดยตรง // ตัวอย่าง กรณีรับจากตัวแปร // $htmlcontent =$_POST['HTMLcode']; $htmlcontent = '<p>ทดสอบ</p>'; $htmlcontent = stripslashes($htmlcontent); $htmlcontent = AdjustHTML($htmlcontent); // สร้างเนื้อหาจาก HTML code $pdf->writeHTML($htmlcontent, true, 0, true, 0); // เลื่อน pointer ไปหน้าสุดท้าย $pdf->lastPage(); // ปิดและสร้างเอกสาร PDF $pdf->Output('test.pdf', 'I');
function WriteHTML($html) { //! @desc HTML parser //! @return void /* $e == content */ if (!$this->usetitle) { $regexp = '/<title>.*?<\\/title>/si'; // eliminate <TITLE> content </TITLE> $html = preg_replace($regexp, '', $html); } AdjustHTML($html, $this->usepre); $html = $this->CreateInternalLinks($html); if ($this->usecss) { $html = $this->ReadCSS($html); } //Add new supported tags in the DisableTags function $html = strip_tags($html, $this->enabledtags); //remove all unsupported tags, but the ones inside the 'enabledtags' string $a = preg_split('/<(.*)>/U', $html, -1, PREG_SPLIT_DELIM_CAPTURE); //explodes the string foreach ($a as $i => $e) { //echo "i=[" . $i . "] e[" . $e ."] pbegin[".$this->pbegin."]<br/>\n"; if ($i % 2 == 0) { //TEXT //Prepare text, if needed if (strlen($e) == 0) { continue; } if ($this->divrevert) { $e = strrev($e); } if ($this->toupper) { $e = strtoupper($e); } if ($this->tolower) { $e = strtolower($e); } if ($this->cssbegin and !$this->tablestart) { if ($this->divwidth == 0) { $this->divwidth = $this->GetStringWidth($e); } if ($this->divheight == 0) { $this->divheight = 5; } $bak_x = $this->x; // if (!$this->divbegin) $this->x = $this->oldx + $this->GetStringWidth($e) - $this->cMargin; if (!$this->divbegin and $this->oldx > 0) { $this->x = $this->oldx; } if ($this->divbgcolor) { $this->Cell($this->divwidth, $this->divheight, '', $this->divborder, '', $this->divalign, $this->divbgcolor); } if ($this->dash_on) { $this->Rect($this->x - $this->divwidth, $this->y, $this->divwidth, $this->divheight); } if ($this->dotted_on) { $this->DottedRect($this->x - $this->divwidth, $this->y, $this->divwidth, $this->divheight); } $this->x = $bak_x; } //Start of 'if/elseif's if ($this->titulo) { $this->titulo = $e; $this->Header(); } elseif ($this->issetlist) { if ($this->li) { if ($this->orderedlist) { $this->textbuffer[] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', 0, $this->strike); if ($this->oldx < 0) { $this->oldx = $this->x + $this->GetStringWidth($this->orderedlist . '. ') + 3; } $this->oldx += $this->GetStringWidth($e); } else { $this->textbuffer[] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', 0, $this->strike); if ($this->oldx < 0) { $this->oldx = $this->x + $this->GetStringWidth(chr(149)) + 3; } $this->oldx += $this->GetStringWidth($e); } } } elseif ($this->tablestart) { if ($this->tdbegin) { $this->cell[$this->row][$this->col]['textbuffer'][] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', 0, $this->strike); $this->cell[$this->row][$this->col]['text'][] = $e; if ($this->cell[$this->row][$this->col]['s'] < $this->GetStringWidth($e) + 3) { $this->cell[$this->row][$this->col]['s'] = $this->GetStringWidth($e) + 3; } } } elseif ($this->divbegin) { $this->textbuffer[] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', 0, $this->strike); } elseif ($this->pbegin) { $this->textbuffer[] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', 0, $this->strike); if ($this->oldx < 0) { $this->oldx = $this->x + $this->GetStringWidth(' '); } $this->oldx += $this->GetStringWidth($e); if ($this->oldx < $this->pgwidth + $this->lMargin) { $this->oldy = $this->y; } else { $this->oldy = $this->y + 5 * (int) ($this->oldx / ($this->pgwidth + $this->lMargin)); } } elseif ($this->SUP) { $this->textbuffer[] = array($e); } elseif ($this->SUB) { $this->textbuffer[] = array($e); } elseif ($this->strike) { $this->textbuffer[] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', 0, $this->strike); } elseif ($this->centertag) { $this->textbuffer[] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', 0, $this->strike); } elseif ($this->addresstag) { $this->textbuffer[] = array($e, $this->HREF, $this->currentstyle, $this->colorarray, $this->currentfont, $this->SUP, $this->SUB, '', 0, $this->strike); } elseif ($this->HREF) { $this->PutLink($this->HREF, $e); } elseif ($this->frm_textarea) { $this->textbuffer[] = array($e); } elseif ($this->frm_select) { $this->textbuffer[] = array($e); } else { $lineheight = 5; //para compensar os H1 H2 H3 H4 usados... if ($this->FontSizePt == 20) { $lineheight = 7; } if ($this->FontSizePt == 16) { $lineheight = 6; } if ($this->FontSizePt == 11) { $lineheight = 5; } if ($this->FontSizePt == 8) { $lineheight = 4; } $this->Write($lineheight, stripslashes(txtentities($e))); } } else { //Tag if ($e[0] == '/') { $this->CloseTag(strtoupper(substr($e, 1))); } else { $regexp = '/ (\\w+?)=([^\\s>"]+)/si'; // change algo=algo to algo="algo" (only do this when this happens inside tags) $e = preg_replace($regexp, " \$1=\"\$2\"", $e); //Extract attributes $contents = array(); preg_match_all('/\\S*=["\'][^"\']*["\']/', $e, $contents); preg_match('/\\S+/', $e, $a2); $tag = strtoupper($a2[0]); $attr = array(); if (!empty($contents)) { foreach ($contents[0] as $v) { if (ereg('^([^=]*)=["\']?([^"\']*)["\']?$', $v, $a3)) { $attr[strtoupper($a3[1])] = $a3[2]; } } } $this->OpenTag($tag, $attr); } } } }