One of the biggest advantages of PHP is that it supports new technologies very easily. the scalability of this language allows developers to easily add new modules, in addition, the support of technical groups all over the world and the support of many extension modules make PHP one of the most functional Web programming languages. The available extension modules enable developers to execute IMAP and POP3 PDF files.
One of the biggest advantages of PHP is that it supports new technologies very easily. the scalability of this language allows developers to easily add new modules, in addition, the support of technical groups all over the world and the support of many extension modules make PHP one of the most functional Web programming languages. Currently, the extended modules allow developers to perform IMAP and POP3 operations, dynamically generate images and Shockwave Flash animations, perform credit card verification, and encrypt and decrypt sensitive data, it can also parse data in XML format. But this is not all. now, another new module can be bound to PHP, namely the PDFLib extension module, which allows developers to dynamically generate PDF (Adobe Portable Document Format) the following describes how to use this module in PHP.
To enable PHP to operate PDF files, you must first install the PDFLib extension library in your system. if you are using an Lunix system, you can.
Extension = php_cmd.dll
For dynamic loading, refer to the following command:
Dl ("php_cmd.dll ");
In addition, you must have an Adobe Acrobat PDF reader to browse the PDF format. if you do not have one, you can download it from http://www.adobe.com/for free.
Once you have prepared a PDF file, you can create a PDF file. The following is a simple example:
// Create a new PDF document handle
$ Pdf = pai_new ();
// Open a file
Pai_open_file ($ pdf, "your testbench ");
// Start a new page (A4)
__Begin_page ($ pdf, 595,842 );
// Obtain and use the font object
$ Arial = pai_findfont ($ pdf, "Arial", "host", 1 );
Performance_setfont ($ pdf, $ arial, 10 );
// Output text
Pai_show_xy ($ pdf, "This is an exam of PDF Documents, It is a good Lib,", 50,750 );
Pai_show_xy ($ pdf, "If you like, please try yourself! ", 50,730 );
// End page
Pai_end_page ($ pdf );
// Close and save the file
Pai_close ($ pdf );
?>
Save it as a PHP file and browse it in the browser. PHP will execute the above code, generate a new PDF file, and save it to the specified location.
Now let's analyze what code we need to use PHP to create a PDF file. There are four steps: 1. create a document handle; 2. Register the font and color of the document; 3, use the functions provided by PDFLib to write text or drawing to the file handle; 4. save the document.
First, create a PDF document handle. The syntax is as follows:
$ Pdf = pai_new ();
This task is completed by the PDF_new () function. It returns a PDF document handle, which will be used by all subsequent operations.
The next step is to give the PDF file a name, which is completed by the pai_open_file () function. it requires the previously created file handle and custom file name as parameters:
Pai_open_file ($ pdf, "your testbench ");
Once the document is created, you can use the plu_begin_page () function to insert a new page:
__Begin_page ($ pdf, 595,842 );
Then end the page with pai_end_page.
Note that in the PDF_begin_page () function, there are two other parameters, which respectively represent the width and height of the page, in the unit of LBS (point, 1 lb equals 1/72 inch ), maybe mathematics is not your strength here. PHP also provides most standard page sizes, such as A4. the above example uses A4 size.
The code between the PDF_begin_page () function and the PDF_end_page () function is written to the PDF document. the content can be text, images, and Ry. In this example, only one line of text is written. first, a font is obtained and the text is written to the document. It is very convenient to select and register the font through the pai_findfont () and pai_setfont () functions. the pai_findfont () function has prepared a font to be used in the document, the required parameters include the font name, encoding used, and whether the font should be embedded in the PDF file. The pai_findfont () function returns a font object, which will be used in the pai_setfont () function.
$ Arial = pai_findfont ($ pdf, "Arial", "host", 1 );
Performance_setfont ($ pdf, $ arial, 10 );
Once the font is set, you can use the pai_show_xy () function to write a string to a specified position on the page.
Pai_show_xy ($ pdf, "This is an exam of PDF Documents, It is a good Lib,", 50,750 );
Pai_show_xy ($ pdf, "If you like, please try yourself! ", 50,730 );
The pai_show_xy () function is used to write content to the page. the last two parameters are the coordinates of the string to be written. Note that the coordinate origin () is in the lower left corner of the document. Once the text is written, the page can be closed with pai_end_page (). of course, you can also write more pages. After writing all the pages, close the document using the pai_close () function. then the document is saved to the file name and path specified when the pai_open_file () function is called, and the document handle is destroyed.
The PDFLib library can do more than this. you can also add images to the page. for example, add an image file under the text file, the following statement adds an image:
$ Image = pai_open_image_file ($ pdf, "jpeg", "PDFImageTest.jpg ");
Pai_place_image ($ pdf, $ image, 50,650, 0.25 );
Is it easy? The pai_open_image_file () function opens an image file. acceptable image types include: GIF, JPEG, TIFF, and PNG. This function returns an image handle. the pai_place_image () function uses the preceding image handle, insert an image to a PDF file. Note that the coordinates here refer to the lower left corner of the image. the last parameter is the proportional factor when the image is displayed. 1 is displayed in the same size as the actual size, and 0.5 is displayed in half of the original size.
In addition to drawing images in PDF documents, the PDF module also provides a number of functions for us to draw Ry. For example, for geometric patterns such as straight lines, circles, and rectangles, the following describes how to draw a straight line:
$ Pdf = pai_new ();
Pai_open_file ($ pdf, "LineExam.pdf ");
__Begin_page ($ pdf, 595,842 );
$ Arial = pai_findfont ($ pdf, "Arial", "host", 1 );
Performance_setfont ($ pdf, $ arial, 12 );
// Set the color of the line
__Setcolor ($ pdf, "stroke", "rgb", 0, 0, 0 );
// Place a Logo in the upper left corner.
$ Image = pai_open_image_file ($ pdf, "jpeg", "logo.jpg ");
Pai_place_image ($ pdf, $ image, 50,785, 0.5 );
// Draw a straight line under the Logo
Pai_moveto ($ pdf, 20,780 );
Pai_lineto ($ pdf, 575,780 );
Performance_stroke ($ pdf );
// Draw another line at the bottom of the page
Pai_moveto ($ pdf, 20, 50 );
Pai_lineto ($ pdf, 575, 50 );
Performance_stroke ($ pdf );
// Output some text
Performance_show_xy ($ pdf, "Meng's Corporation", 200, 35 );
Pai_end_page ($ pdf );
Pai_close ($ pdf );
?>
As shown in the preceding example, to draw a straight line, you only need three functions: pai_moveto (), pai_lineto (), and pai_stroke (). In the above example, we first use the PDF_moveto ($ pdf, 20,780) function to move the cursor to the coordinate (20,780), and then use PDF_lineto ($ pdf, 575,780) the function defines the coordinates (575,780) of another vertex of a straight line, and draws a line using pai_stroke ($ pdf. The color function PDF_setcolor ($ pdf, "stroke", "rgb", 0, 0, 0) has several parameters, the color fill mode includes three options: stroke, fill, and both. the color can be the color value of the RGB or CMYK color scheme. It is worth noting that the value used in the pai_setcolor () function is the percentage of the color, that is, the brightness of the color. for example, if you want to set it to red (RGB: 255, 0, 0 ), you can write: PDF_setcolor ($ pdf, "stroke", "rgb", 1, 0, 0). to set it to yellow, you can do this: PDF_setcolor ($ pdf, "stroke", "rgb", 1, 1, 0 ).
To draw a rectangle and a circle with a fill color, you can use the following method:
// Set the fill color
__Setcolor ($ pdf, "fill", "rgb", 1, 1, 0 );
// Set the border line color
__Setcolor ($ pdf, "stroke", "rgb", 0, 0, 0 );
// Draw a rectangle. the following four parameters are the coordinates X, Y, width, and height in the lower left corner.
Performance_rect ($ pdf, 50,500,200,300 );
Pai_fill_stroke ($ pdf );
__Setcolor ($ pdf, "fill", "rgb", 0, 1, 0 );
__Setcolor ($ pdf, "stroke", "rgb", 0, 0, 1 );
// Draw the circle. the parameters are the coordinates of the center and the radius of the circle.
Pai_circle ($ pdf, 400,600,100)
In addition, PDFLib provides functions for writing document summary information. these functions start with pai_set_info _ * (). These functions can include the author, title, content, and topic of a document. Below are several common functions:
__Set_info_author ($ pdf, "net_lover ");
__Set_info_creator ($ pdf, "Meng Xianhui ");
Pai_set_info_title ($ pdf, "PHP Exam ");
Pai_set_info_subject ($ pdf, "PHP ");
Pai_set_info_keywords ($ pdf, "php pdf PDFLib ");
When you open such a document with Acrobat Reader, you can see the information written above in the menu "file"-"document attributes"-"Summary.
Speaking of this, I believe you have a basic understanding of how to use PDFLib to create PDF documents. Next, let's take a practical example to see how to serve our work. In this example, a pie chart is generated based on the provided data. first, a data input form is created to enter the size of each segment in the pie chart. The file is as follows:
Use PHP to create a PDF document (pie chart)
Pie chart generator
The following is the code for the pie. php file:
// Accept the library
$ Data = $ _ POST ['data'];
$ Slices = explode (",", $ data );
// Initialize the variable
$ Sum = 0;
$ Degrees = Array ();
$ Diameter = 200;
$ Radius = $ diameter/2;
// Set the color of each pie chart, which is stored in arrays.
$ Colours = array (0, 0), array (0, 0, 1), array (0, 0 ),
Array (, 0), array (, 1), array (, 0 ),
Array (1, 0, 1 ));
// Calculate the total value
$ Sum = array_sum ($ slices );
// Convert each part into a corresponding percentage (360 degrees circle)
For ($ y = 0; $ y
// Start creating a PDF document
$ Pdf = pai_new ();
Pai_open_file ($ pdf, "chart.pdf ");
__Begin_page ($ pdf, 500,500 );
__Setcolor ($ pdf, "stroke", "rgb", 1, 1, 0 );
Pai_moveto ($ pdf, 250,250 );
Pai_lineto ($ pdf, 350,250 );
Performance_stroke ($ pdf );
For ($ z = 0; $ z
// Redraw the outer contour
Pai_circle ($ pdf, 250,250,100 );
Performance_stroke ($ pdf );
Pai_end_page ($ pdf );
Pai_close ($ pdf );
// If you want to directly output the data to the client, add the following code
$ Buf = pai_get_buffer ($ p );
$ Len = strlen ($ buf );
Header ("Content-type: application/pdf ");
Header ("Content-Length: $ len ");
Header ("Content-Disposition: inline; filename=pie_php ");
Print $ buf;
Performance_delete ($ p );
?>
Run the above program and enter different values. you will get different pie charts.
PDFLib is a compatible module. you can not only write it in PHP, but also use Java, C #, and VB. NET, VB5/6 (ActiveX/COM), ASP (VBScript/Jscript), Borland Delphi, Windows Script Host, ColdFusion4.5 +, C/C ++, Python, Perl, and RPG; supported platforms include not only Windows, but also Unix/Linux, Mac OS, IBM eServer iSeries 400, and zSeries S/390, for specific operating environment, please visit their website at any time to obtain the latest information.
Source: hooday