Php pdf file generation

Source: Internet
Author: User
Tags pdflib php pdf
: This article describes how to generate PDF files in PHP. For more information about PHP tutorials, see.

This document describes how to generate a PDF file in PHP. We use the TCPDF open-source plug-in to generate PDF documents in PHP. You can insert images, HTML, links, tables, bar charts, and other PHP functions to dynamically generate PDF files.

One of the PECL extensions of PHP is pdflib, which was maintained in January 2014. the PDFLib library is free for individuals and must be licensed for commercial products. It is relatively complex to use. Therefore, this rule is excluded.

This article introduces a plug-in, TCPDF! Official http://www.tcpdf.org. After downloading it, introduce it in the code. No additional extensions need to be compiled/installed. The TCPDF download package and the official website both provide a large number of examples and dozens of fonts (except for some other Chinese files, they cannot be used ...). LGPL license open source protocol is used.

Go straight to the topic below. Download it from the official website. Suppose it is stored in the/var/www/directory.

// The first step is to introduce the TCPDF entry file.

Require_once '/var/www/tcpdf. php ';

// Instantiate

$ Pdf = new TCPDF ('P', 'mm', 'A4 ', true, 'utf-8', false );

// Set 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 the header information parameters to the LOGO address, LOGO size, two-line title, title color, and split line color .. The color is RGB.

$ Pdf-> SetHeaderData ('/var/www/tcpdf/examples/images/tcpdf_logo.jpg', 30, 'php generate PDF ', 'php generate pdf file', array, 0), array (0, 0 ));

// Set footer information

$ Pdf-> setFooterData (array (0, 0, 0), array (0, 0 ));

// Set the header and footer fonts

$ Pdf-> setHeaderFont (Array ('stsonstcdlight ', '', '12 '));

$ Pdf-> setFooterFont (Array ('Helvetica ', '', '8 '));

// Set the default same width font

$ Pdf-> setdefamonomonospacedfont ('courier ');

// Set the spacing

$ Pdf-> SetMargins (15, 27, 15 );

$ Pdf-> SetHeaderMargin (5 );

$ Pdf-> SetFooterMargin (10 );

// Set pagination

$ Pdf-> SetAutoPageBreak (TRUE, 15 );

// Set the image proportion

$ Pdf-> setImageScale (1.25 );

// Output the header and footer information.

$ Pdf-> AddPage ();

// Set the font-text title. B is bold, 15 is the size

$ Pdf-> SetFont ('stsongstdlight ',' B ', 15 );

$ Pdf-> Write (20. 'php how to dynamically generate PDF ',', 0, 'C', true, 0, false, false, 0 );

// Set the font-text content. B is bold, 15 is the size

$ Pdf-> SetFont ('stsongstdlight ', '', 10 );

// L is left alignment, R is right alignment, and C is center alignment.

$ Pdf-> Write (0, $ content, '', 0, 'L', true, 0, false, false, 0 );

// Output PDF. The second parameter is I by default, which is browser preview. D. Download

$ Pdf-> output('php_to_{}', 'I ');

Copy and execute the above code, and you will find that the browser opens the PDF file preview (if your browser is not IE ). Replace the second parameter of Output with "D" to download the file.

Speaking of this, it is basically done. But there is a problem. you will find the font is awkward and ugly. We can change the font "droidsansfallback". This font is not provided. You can find the download location through Google Baidu, or in http://sourceforge.net/projects/hawebs/files/Assistance/PHP/Droid%20Sans%20Fallback%20-%20PHP.zip/download. Download and decompress the package. copy the droidsansfallback. php, droidsansfallback. z, and droidsansfallback. ctg. z files to the tcpdf/fonts/directory. Then, replace stsongdlight in the code with droidsansfallback. In the execution, we will find that the font looks much better .. Of course, you can use tcpdf_addfont.php of TCPDF to convert other fonts into TCPDF recognized fonts, and then move them to the tcpdf/fonts/directory. For example,.


The above describes how to generate a PDF file in PHP, including related content. I hope my friends who are interested in the PHP Tutorial can help me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.