Use PHP to generate a PDF file with fpdf

Source: Internet
Author: User
Tags pdflib

What is fpdf?
Fpdf is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the pdflib library. the advantage is that pdflib requires a mandatory for a specified cial usage. f from fpdf stands for free: You may use it for any kind of usage and modify it to suit your needs.

Fpdf allows us to create PDF files without calling pdflib

Here you can go to the latest Windows version:
Http://www.fpdf.org/en/dl.php? V = 152 & F = zip

Here you can go to the Chinese manual:
Http://www.fpdf.org/en/dl.php? Id = 72

For other versions, go to the following:
Http://www.fpdf.org/
This is also the official website of fpdf. If you have any questions, you can go to the above and reply quickly. I asked a question in the morning and they will return in the afternoon. However, the above time is the United States time.

Below is an example of creating a PDF file with fpdf and adding a page

<?
Define ('fpdf _ fontpath', 'f0000152/font /');
Require ('f0000152/fpdf. php ');
$ PDF = new fpdf;
$ PDF-> addpage ();
$ PDF-> output('a.pdf ', 'D ');
?>

You can also use $ PDF-> open (); To create a new PDF file.
But here, addpage () contains open (), which completes two steps: creating a PDF file and adding one page at the same time.

In the preceding example, define ('fpdf _ fontpath', 'f0000152/font/') defines the directory for storing the font files.
After downloading the compressed package, you can see the font folder. You just need to point to that folder.
The above sectionProgramNothing can be displayed
Now let's add two sentences.

<?
Define ('fpdf _ fontpath', 'f0000152/font /');
Require ('f0000152/fpdf. php ');
$ PDF = new fpdf;
$ PDF-> addpage ();
$ PDF-> setfont ('arial ');
$ PDF-> text (5, 20, 'test PDF ');
$ PDF-> output('a.pdf ', 'D ');
?>

Setfont () is used to set the font. This step must be used to call fpdf for the first time. Otherwise, when the PDF file is opened, the system will prompt "no defined font" and nothing will be displayed.
We recommend that you define commonly used fonts, which are not supported in Chinese.
Addfont () is required to support Chinese characters or relatively biased fonts, but I have not passed the test. If you want to pass the test, please refer to ^
Text () prints a string. The X-axis is 5, the y-axis is 20, and the string content is "test pdf"
For more information about the parameter 'D' in the final output (), see the manual.
In this way, the exported PDF file will display 'test PDF.
Add an image if you are interested.

<?
Define ('fpdf _ fontpath', 'f0000152/font /');
Require ('f0000152/fpdf. php ');
$ PDF = new fpdf;
$ PDF-> addpage ();
$ PDF-> setfont ('arial ');
$ PDF-> text (5, 20, 'test PDF ');
$ PDF-> image('jianxin_mark.jpg ', 5, 30, 60, 50 );
$ PDF-> output('a.pdf ', 'D ');
?>

Image () can insert an image in pdf. The file name must be prefixed. The path must be included. 5 is the horizontal coordinate, 30 is the vertical coordinate, 60 is the image width, and 50 is the height.
OK, so that a PDF file with text and images is created ^
In fact, there are many other functions in the manual, but they are not very detailed. You need to study and explore them by yourself ^

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.