How to Create a PDF on the Fly Using PHP
PDF files provide your website visitors with a read-only document. The document settings prevent users from changing the layout and text, and the PDF reader is a freely distributed application from Adobe. PDF format is a standard on the Internet, and PHP provides you with the ability to create these files on-the-fly. This type of programming is useful when you want to create a PDF file that depends on user input on your website.
Instructions
-
-
1
Create a new PDF file. Two lines of code are required when creating the file. The first opens a PDF object, and the second line of code specifies the file name. The following code creates the PDF file:
$pdfObject = PDF_new();
PDF_open_file("pdffile.pdf", ""); -
2
Set the PDF font. You can use any installed font on the server. The set font is used in the page for your text. the following code sets your font:
$font= PDF_findfont($pdfObject, "Arial", "host", 0);
PDF_setfont($pdfObject, $font, 10); -
-
3
Write some text to the file. You can now write to the file. The following code writes a simple, one-line paragraph to the file:
PDF_show_xy($pdf, "PDF file created by PHP", 50, 750);
-
4
Close the file once you are finished writing to it. The following code closes the file and releases the server's resources:
PDF_close($pdf);
-
1
References
- Photo Credit document case image by Maya Kruchancova from Fotolia.com