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 program cannot display anything
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.