This article describes PHP generation PDF. We use the Tcpdf open source plugin to enable PHP to generate PDF documents. You can insert the functions of PHP to dynamically generate PDFs, such as pictures, HTML, links, tables, column charts, and so on.
PHP has a pecl extension called pdflib, and maintained in the January 2014, Pdflib Library for the individual is free, for commercial products need to purchase license. And the use is relatively complex. therefore excluded.
This article introduces a plug-in, tcpdf! Official website http://www.tcpdf.org. Once downloaded, it can be used in code. No need to compile/install additional extensions. Tcpdf's download package and official website will provide a large number of examples and dozens of fonts (except that Chinese cannot be used in addition to individual ...). )。 Use LGPL license Open source protocol.
The following goes straight to the topic. After downloading the website. The assumption is placed under the/var/www/directory.
The first step must be to introduce Tcpdf's entry file.
Require_once '/var/www/tcpdf/tcpdf.php ';
Instantiation of
$pdf = new TCPDF (' P ', ' mm ', ' A4 ', true, ' UTF-8 ', false);
Set up Document information
$pdf->setcreator (' Lane ');
$pdf->setauthor (' Lane ');
$pdf->settitle (' PHP generated pdf ');
$pdf->setsubject (' PHP dynamically generated PDF file ');
$pdf->setkeywords (' PHP pdf TCPDF ');
Set header information parameters are logo address, logo size, two lines of title, title color, split line color. Color is RGB
$pdf->setheaderdata ('/var/www/tcpdf/examples/images/tcpdf_logo.jpg ', ' php generate PDF ', ' php how to generate PDF file ', array ( 0,0,0), Array (0,0,0));
Set Footer information
$pdf->setfooterdata (Array (0,0,0), Array (0,0,0));
Set Header and footer fonts
$pdf->setheaderfont (Array (' stsongstdlight ', ' ', ' 12 '));
$pdf->setfooterfont (Array (' Helvetica ', ' ', ' 8 '));
Set the default equal-width font
$pdf->setdefaultmonospacedfont (' Courier ');
Set spacing
$pdf->setmargins (15, 27, 15);
$pdf->setheadermargin (5);
$pdf->setfootermargin (10);
Set Page Paging
$pdf->setautopagebreak (TRUE, 15);
Set picture proportions
$pdf->setimagescale (1.25);
Output the header footer information.
$pdf->addpage ();
Set font-the body title of the Oh. B is bold, 15 is size
$pdf->setfont (' stsongstdlight ', ' B ', 15);
$pdf->write, ' How PHP dynamically generates PDF ', ', 0, ' C ', True, 0, False, False, 0);
Set the font-the body content of the OH. B is bold, 15 is size
$pdf->setfont (' stsongstdlight ', ', 10);
L is left-aligned, R is right-aligned, and C is center-aligned.
$pdf->write (0, $content, ', 0, ' L ', True, 0, False, False, 0);
Output PDF. The second parameter defaults to I, which is the browser preview. D is download
$pdf->output (' php_to_pdf.pdf ', ' I ');
Copying and executing the code above will reveal that the browser opens a preview of the PDF file (if your browser is not IE). Replace the second parameter of output with D, and you can download it.
Speaking of which, the basic is finished. But there is a problem, you will find the font is very awkward, especially ugly. We can change a font, "Droidsansfallback". The font is not self-brought. Google Baidu can find the place to download, or in the http://sourceforge.net/projects/hawebs/files/assistance/php/droid%20sans%20fallback%20- %20php.zip/download. After downloading the extract, copy the three files of droidsansfallback.php, Droidsansfallback.z and droidsansfallback.ctg.z to the tcpdf/fonts/directory. Then replace the stsongstdlight in the code with the Droidsansfallback. In the execution, you will find the font looks much better. Of course, you can use Tcpdf's own tcpdf_addfont.php to convert other fonts into Tcpdf recognized fonts, and then move them into the tcpdf/fonts/directory. Like Microsoft Ya-black or something.
The above describes the PHP generated PDF file, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.