Using PHP to generate PDF files with FPDF

Source: Internet
Author: User
Tags add define pdflib php class require string

What is FPDF?
FPDF is a PHP class which allows to generate PDF files with pure PHP, which is to say without using the Pdflib library. The advantage is this pdflib requires a fee for a commercial usage. F from FPDF stands for free:you The IT for any kind the usage and modify it to suit your.

Fpdf allows us to create PDF files without having to call Pdflib

Here is a very simple example of creating a PDF file with fpdf and adding a page

?
Define (' Fpdf_fontpath ', ' fpdf152/font/');
Require (' fpdf152/fpdf.php ');
$pdf = new FPDF;
$pdf->addpage ();
$pdf->output (' a.pdf ', ' D ');
?>

Of course, you can also use $pdf->open () to create a new PDF file
But here, AddPage () contains open (), and he completes a new PDF file and adds 1 pages two steps

The Define (' Fpdf_fontpath ', ' fpdf152/font/') in the example above, defines the directory where the font files are stored
After downloading the compressed package, you will see the font folder, just point to that folder on the line
The above program doesn't show anything yet.
Now let's add two sentences.

?
Define (' Fpdf_fontpath ', ' fpdf152/font/');
Require (' fpdf152/fpdf.php ');
$pdf = new FPDF;
$pdf->addpage ();
$pdf->setfont (' Arial ');
$pdf->text (5,20, ' Test pdf ');
$pdf->output (' a.pdf ', ' D ');
?>

SetFont () Set the font, this step in the 1th time to call fpdf must be, otherwise the PDF file will be prompted when open "no font" and nothing displayed
It is best to define a more commonly used font, and Chinese does not support it.
To support Chinese or more biased font to use the AddFont (), but I did not pass the test, I hope that the pass can be pointed ^^
Text () prints a string with a horizontal coordinate of 5 and an ordinate of 20, with the string content "test PDF"
The parameter ' D ' in the final output () can be referenced in the manual, which is clearly written.
The exported PDF file will display ' Test PDF '.
If you are interested, add a picture.

?
Define (' Fpdf_fontpath ', ' fpdf152/font/');
Require (' fpdf152/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 a picture in the PDF, preceded by the filename, to include the path, 5 is the horizontal axis, 30 is the ordinate, 60 is the picture width, 50 is the height
OK, so a PDF file with text and pictures is established ^^
In fact, there are many functions, manuals are written, but not very detailed, you need to explore their own ^^



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.