ÀûÓÃTcpdfÉú³Épdf,ʹÓà html±êÇ©,Éú³É pdf
Ò»¡¢ ÀÈ¡À©Õ¹´úÂë ʹÓà composer ÀÈ¡
composer require tecnickcom/tcpdf
https://github.com/tecnickcom/tcpdf Ö±½ÓÏÂÔØ
¹ÙÍøµØÖ· : http://www.tcpdf.org
ÈçÏÂЧ¹û:
¶þ¡¢¿ª·¢´úÂë html Éú³Épdf htmlÑùʽ¸ñʽ¿ÉÒԲο¼¹ÙÍø°¸Àý https://tcpdf.org/examples/
require_once(dirname(dirname(__FILE__)) . '/sdk/tcpdf.php');
public function createPdf($pdf_name,$data=[],$type=1)
{
if(empty($pdf_name)){
$pdf_name = md5(microtime());
}
$pdf_name = $pdf_name;
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,true, 'UTF-8', false);
$pdf->SetCreator('hello world');
$pdf->SetAuthor('hello world');
$pdf->SetTitle('hello world');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, PHP');
$pdf->setFooterData(array(0,64,0), array(0,64,128));
$pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
$pdf->setFooterFont(Array('helvetica', '', '8'));
$pdf->SetDefaultMonospacedFont('courier');
$pdf->SetMargins(15, 27, 15);
$pdf->SetHeaderMargin(5);
$pdf->SetFooterMargin(10);
$pdf->SetAutoPageBreak(TRUE, 25);
$pdf->setImageScale(1.25);
$pdf->setFontSubsetting(true);
$pdf->SetFont('stsongstdlight', '', 14);
$pdf->AddPage();
$str1 = $this->getHtml($pdf_name,$data,$pdf);
$pdf->writeHTML($str1, true, false, true, false, '');
ob_clean();
$pdf_path = ROOT_PATH .'public/pdf/'.$pdf_name.'.pdf';
$pdf_png_path = ROOT_PATH .'publi/pdf/png/'.$pdf_name.'.png';
$pdf->Output($pdf_path, 'F');
$this->pdf2png($pdf_path,$pdf_png_path);
return array($pdf_url,$pdf_png_url);
}
public funcitoin getHtml(){
$html = '<h1>HTML Example</h1>
Some special characters: < € € € & ¨¨ è © > \\slash \\\\double-slash \\\\\\triple-slash
<h2>List</h2>
List example:
<ol>
<li><img src="images/logo_example.png" alt="test alt attribute" width="30" height="30" border="0" /> test image</li>
<li><b>bold text</b></li>
<li><i>italic text</i></li>
<li><u>underlined text</u></li>
<li><b>b<i>bi<u>biu</u>bi</i>b</b></li>
<li><a href="http://www.tecnick.com" dir="ltr">link to http://www.tecnick.com</a></li>
<li>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.<br />Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</li>
<li>SUBLIST
<ol>
<li>row one
<ul>
<li>sublist</li>
</ul>
</li>
<li>row two</li>
</ol>
</li>
<li><b>T</b>E<i>S</i><u>T</u> <del>line through</del></li>
<li><font size="+3">font + 3</font></li>
<li><small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal</li>
</ol>
<dl>
<dt>Coffee</dt>
<dd>Black hot drink</dd>
<dt>Milk</dt>
<dd>White cold drink</dd>
</dl>
<div style="text-align:center">IMAGES<br />
<img src="images/logo_example.png" alt="test alt attribute" width="100" height="100" border="0" /><img src="images/tcpdf_box.svg" alt="test alt attribute" width="100" height="100" border="0" /><img src="images/logo_example.jpg" alt="test alt attribute" width="100" height="100" border="0" />
</div>';
return $html;
}
Ò²¿ÉÒÔʹÓÃÆäËûÐÎʽ,Éú³É pdf,ÓÐÐËȤµÄͬѧ¿ÉÒÔ³¢ÊÔÒ»ÏÂ
Èý¡¢Ê¹Óà Imagick ½« pdf Éú³É png ¸ù¾Ý½éÉÜ°²×°Ò»ÏÂphpÀ©Õ¹ https://www.php.net/manual/zh/book.imagick.php
public function pdf2png($from_path,$target_path)
{
try {
$img = new \Imagick();
$img->setCompressionQuality(100);
$img->setResolution(120, 120);
$img->readImage($from_path);
$canvas = new \Imagick();
$imgNum = $img->getNumberImages();
foreach ($img as $k => $sub) {
$sub->setImageFormat('png');
$sub->stripImage();
$sub->trimImage(0);
$width = $sub->getImageWidth() + 10;
$height = $sub->getImageHeight() + 10;
if ($k + 1 == $imgNum) $height += 10;
$canvas->newImage($width, $height, new \ImagickPixel('white'));
$canvas->compositeImage($sub, \Imagick::COMPOSITE_DEFAULT, 5, 5);
}
$canvas->resetIterator();
$canvas->appendImages(true)->writeImage($target_path);
return $target_path;
} catch (Exception $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
return false;
}
}
ËÄ¡¢×¢ÒâÊÂÏî
1¡¢Ê¹Óà Tcpdf htmlÉú³É pdfʱ,×¢ÒâÒ»ÏÂhtmlµÄÑùʽÎÊÌâ,²»Ö§³ÖÍⲿcssÑùʽ
2¡¢Ê¹Óà Tcpdf htmlÉú³É pdfʱ,×¢ÒâÒ»ÏÂhtmlµÄÑùʽÎÊÌâ, Äã¿´µ½html²»Ò»¶¨ÄÜÉú³ÉÕý³£htmlŶ!
3¡¢Imagick °²×°Ê±,Ò»¶¨Òª×¢Òâ¼ì²éÊÇ·ñ°²×°³É¹¦,·ñÔò»áÓб¨´íÐÅÏ¢,²»ÄÜÕý³£Ê¹ÓÃ
|