PDF documentation generator written in PHP

PHP is one of the biggest advantages of the new technology it is very easy to support, scalability, this language allows developers to easily add new modules, and supports numerous support and expansion modules all over the world technical community makes PHP It has become one of the most complete Web programming language. Currently available expansion module has enable developers to perform operations IMAP and POP3, can be dynamically generated images and Shockwave Flash animation, card verification, encryption and decryption of sensitive data, but also able to parse data in XML format. But that's not all, now have a new module can be bound with PHP, that is PDFLib extension module that allows developers to dynamically generate documents PDF (Adobe Portable Document Format) format, the following look at to see how to use this module in PHP.

In order to enable PHP has the ability to operate a PDF document, you must first install on your system PDFLib extensions, if you are using Lunix system, you can get from  http://www.pdflib.com/pdflib/index. html download and compile, if you are using a Windows system, it's even easier, just download a compiled PDFLib library, and then in the PHP configuration file to the appropriate comment line can be removed.


extension=php_pdf.dll


If it is loaded dynamically, and may be reference to the following commands:


dl("php_pdf.dll");


In addition, you must have an Adobe Acrobat PDF Reader for viewing PDF format, if you do not, you can get from http://www.adobe.com/ download free.

Once you do the preparation, you can create PDF files, here is a simple example:

 


<?php

// Create a new PDF document handle

$ Pdf = PDF_new ();


// open a file

PDF_open_file($pdf, "PDFTest.pdf");


// start a new page (A4)

PDF_begin_page($pdf, 595, 842);


// get and use the font object

$arial = PDF_findfont($pdf, "Arial", "host", 1);

PDF_setfont($pdf, $arial, 10);


// output text

PDF_show_xy($pdf, "This is an exam of PDF Documents, It is a good Lib,",50, 750);

PDF_show_xy($pdf, "If you like,please try yourself!", 50, 730);


// end a

PDF_end_page($pdf);


// close and save the file

PDF_close($pdf);

?>

 

Then save the file as PHP, browsing in the browser, PHP will execute the above code, it creates a new PDF file and save it to a specified location.

Now we analyze what code to use PHP to create PDF files, there are four steps: 1. Create a document handle; 2, registration document fonts and colors; 3, write text to a file handle is provided with a function PDFLib or drawing; 4. save the document.

First, create a PDF document handle syntax is as follows:


$ Pdf = PDF_new ();


This task is performed by PDF_new () function to complete, it returns a handle to a PDF document, the handle will be the follow-up of all operations.

The next step is to give the PDF file a name () function is done by PDF_open_file, it requires previously created file handles and custom file names as arguments:


PDF_open_file($pdf, "PDFTest.pdf");


Once we have created a document, you can use PDF_begin_page () function in which to insert a new page of:


PDF_begin_page($pdf, 595, 842);


Then PDF_end_page () end of the page.

Note that, in PDF_begin_page () function, there are two other parameters, they represent the width and height of the page size in pounds (point, 1 pound is equal to 1/72 inch), perhaps where math is not your strong suit , PHP also provides most of the standard page size, such as A4 and so on, the above example is the use of A4 size.

Calling PDF_begin_page () function and PDF_end_page () function is to write the code between the contents of the PDF document, and the content can be text, images and geometric shapes and so on. Examples just write a line of text, to get a font, and then write the text document. By PDF_findfont () and PDF_setfont () function and select the font registration is convenient, PDF_findfont () function and prepare a font A document to be used, the required parameters have the name of the font, use the code, if you want to embed fonts PDF file. PDF_findfont () function returns a font object, it will () function is in use in PDF_setfont.


$arial = PDF_findfont($pdf, "Arial", "host", 1);

PDF_setfont($pdf,$arial, 10);


Once we set the font, you can use PDF_show_xy () function writes a string to a specified location on the page.


PDF_show_xy($pdf, "This is an exam of PDF Documents, It is a good Lib,",50, 750);

PDF_show_xy($pdf, "If you like,please try yourself!", 50, 730);


PDF_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 the coordinates of the origin (0,0) is in the lower left corner of the document. Once finished the text, you can close the page PDF_end_page (), of course, you can also write more pages. After all the pages finished with PDF_close () function closes the file, then save the document and returned to the calling PDF_open_file () function when the specified file name and path, the document will handle destruction.

PDFLib library can do far more than that, you can also add an image in the page, we have an example in front of the file, add the following text in an image file, the following sentence is added to achieve the image features:


$image = PDF_open_image_file($pdf, "jpeg", "PDFImageTest.jpg");

PDF_place_image($pdf, $image, 50, 650, 0.25);


Is not it simple? PDF_open_image_file () function to open a graphics file, an acceptable image types are: GIF, JPEG, TIFF and PNG, the function returns an image handle, PDF_place_image () function using the handle to the front of the image, the image is inserted into the PDF document in. Note that the coordinate position refers to the lower left corner of the image, the last parameter is a scaling factor at the time of image display, a display is the same as the actual size, according to 0.5 is a half of the original size.

In addition to the existing draw the image to a PDF file outside, PDF module also provides a number of functions to allow us to draw geometric shapes. For example: linear, circular, rectangular and other geometric patterns, the following is a straight line drawn some implementation:


<?php

$ Pdf = PDF_new ();

PDF_open_file($pdf, "LineExam.pdf");

PDF_begin_page($pdf, 595, 842);

$arial = PDF_findfont($pdf, "Arial", "host", 1);

PDF_setfont($pdf, $arial, 12);


// set the color line

PDF_setcolor($pdf, "stroke", "rgb", 0, 0, 0);


// place a logo in the upper left corner Logo

$image = PDF_open_image_file($pdf, "jpeg", "logo.jpg");

PDF_place_image($pdf, $image, 50, 785, 0.5);


// draw a straight line in the Logo logo

PDF_moveto ($ pdf, 20, 780);

PDF_lineto ($ pdf, 575, 780);

PDF_stroke($pdf);


// Draw another straight line at the bottom of the page

PDF_moveto ($ pdf, 20,50);

PDF_lineto ($ pdf, 575, 50);

PDF_stroke($pdf);


// output some text

PDF_show_xy($pdf, "Meng‘s Corporation", 200, 35);

PDF_end_page($pdf);

PDF_close($pdf);

?>

 

As can be seen from the above example, to draw a straight line, only three functions to: PDF_moveto (), PDF_lineto () and PDF_stroke (). The above example is to use PDF_moveto ($ pdf, 20, 780) function to move the cursor to the coordinates (20,780), and the coordinates of a point with additional PDF_lineto ($ pdf, 575, 780) define a straight line function (575, 780), and finally draw lines with PDF_stroke ($ pdf). Set the color function PDF_setcolor ($ pdf, "stroke", "rgb", 0, 0, 0) has several parameters, which are filled with color modes are stroke, fill, both three options, color can be RGB or CMYK color value of the color scheme. It is worth noting: PDF_setcolor values ​​used in the function () is the percentage of color, that is the brightness of the color, for example: If you want to red (RGB: 255,0,0), you can write: PDF_setcolor ($ pdf, "stroke", "rgb", 1, 0, 0), yellow if you want to be like this: PDF_setcolor ($ pdf, "stroke", "rgb", 1, 1, 0).


Videos with rectangular and circular in order to fill color, can use the following method:


// set the fill color

PDF_setcolor($pdf, "fill", "rgb", 1, 1, 0);


// set the color of the border line

PDF_setcolor($pdf, "stroke", "rgb", 0, 0, 0);


// draw a rectangle, the latter four parameters are the lower-left corner coordinates X, Y, width, height

PDF_rect ($ pdf, 50, 500, 200, 300);

PDF_fill_stroke($pdf);

PDF_setcolor($pdf, "fill", "rgb", 0, 1, 0);

PDF_setcolor($pdf, "stroke", "rgb", 0, 0, 1);


// draw a circle, parameters are center coordinates and radius of a circle


PDF_circle($pdf, 400, 600, 100)


In addition, PDFLib also provides functions written document summary information, these functions begin with PDF_set_info _ * (), this information may include: document author, title, content, themes and so on. Here are a few commonly used functions:


PDF_set_info_author($pdf, "net_lover");

PDF_set_info_creator($pdf, "Meng Xianhui");

PDF_set_info_title($pdf, "PHP Exam");

PDF_set_info_subject($pdf, "PHP");

PDF_set_info_keywords($pdf, "PHP PDF PDFLib");


When you open such a document with Acrobat Reader, the menu "File" - "Document Properties" - "Summary" where you can see the information written above into the.

Having said that, I believe we create PDF documents on how to use PDFLib have a basic understanding of it. Here, we have to look at a practical example of how our services work. The data of this example is provided to generate a pie chart, first, to establish a data entry form, the input of each block size of the pie chart. File as follows:


<html>

<head>

<Title> use PHP to create PDF documents (pie) </ title>

</head>

<body>

<H3> Pie generator </ h3>

<table cellspacing="5" cellpadding="5">

<form action="pie.php" method=POST>

<tr>

<Td> enter a data value for each pie to (,) separated: </ td> </ tr>

<tr><td><input type=text name=data></td></tr>

<tr><td><input type=submit value="产生PDF饼图"></td></tr>

</form>

</table>

</body>

</html>


Here is the code pie.php file:


<?php

// Accepts stacks

$data = $_POST[‘data‘];

$slices = explode(",", $data);


// Initialize variables

$sum = 0;

$degrees = Array();

$diameter = 200;

$radius = $diameter/2;


// set the color of each piece of pie, with an array of storage

$colours = array(array(0,0,0),array(0,0,1),array(0,1,0),

array(1,0,0),array(0,1,1),array(1,1,0),

array(1,0,1));


The total value calculated //

$sum = array_sum($slices);


// to each block are converted into the corresponding percentage (360 ° circle)

for ($y=0; $y<sizeof($slices); $y++) {

$ Degrees [$ y] = ($ slices [$ y] / $ sum) * 360;

}


// start creating PDF documents

$ Pdf = PDF_new ();

PDF_open_file($pdf, "chart.pdf");

PDF_begin_page($pdf, 500, 500);

PDF_setcolor($pdf, "stroke", "rgb", 1, 1, 0);

PDF_moveto ($ pdf, 250, 250);

PDF_lineto ($ pdf, 350, 250);

PDF_stroke($pdf);


for ($z=0; $z<sizeof($slices); $z++)

{

// set the fill color

PDF_setcolor($pdf, "fill", "rgb", $colours[$z][0],

$colours[$z][1], $colours[$z][2]);


// end point coordinate is calculated for each arc

$end_x = round(250 + ($radius * cos($last_angle*pi()/180)));

$end_y = round(250 + ($radius * sin($last_angle*pi()/180)));


// Each block is divided by a straight line circular arc

PDF_moveto ($ pdf, 250, 250);

PDF_lineto ($ pdf, $ end_x, $ end_y);


// calculated and drawn arc

PDF_arc($pdf, 250, 250, $radius, $last_angle,($last_angle+$degrees[$z]));


// save the final angle

$last_angle = $last_angle+$degrees[$z];


// fill color

PDF_fill_stroke($pdf);

}


// redraw the outer contour

PDF_circle($pdf, 250, 250, 100);

PDF_stroke($pdf);


PDF_end_page($pdf);

PDF_close($pdf);


// If you want to output directly to the client, then add the following code

$buf = PDF_get_buffer($p);

$ Len = strlen ($ buf);

header("Content-type: application/pdf");

header("Content-Length: $len");

header("Content-Disposition: inline; filename=Pie_php.pdf");

print $buf;

PDF_delete($p);

?>

 

Run the above procedure and enter a different value, you will get a different pie.

PDFLib is a very good compatibility module, you can not only written in PHP, you can also use Java, C #, VB.NET, VB5 / 6 (ActiveX / COM), ASP (VBScript / Jscript), Borland Delphi, Windows Script Host , ColdFusion4.5 +, C / C ++, Python, Perl, RPG; supported platforms not only for Windows, as well as Unix / Linux, Mac OS, IBM eServer iSeries 400 and zSeries S / 390, the specific operating environment please feel free to visit their website to get the latest information.

Guess you like

Origin www.cnblogs.com/li123456/p/11322384.html