PHP checks whether string encoding is utf8 and the conversion method

Source: Internet
Author: User
Tags pdflib
PHP determines whether string encoding is utf8 and the conversion method. to enable PHP to operate PDF files, you must first install the PDFLib extension library in your system, if you are using a Linux system, you can download and compile a string from http://www.pdflib.com/pdflib/index.html. if you are using Wi PHP to determine whether the string encoding is utf8 and the conversion method
To enable PHP to operate PDF files, you must first install the PDFLib extension library in your system. if you are using a Linux system, you can.


Extension = php_cmd.dll

For dynamic loading, refer to the following command:


dl("php_pdf.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 = PDF_new (); // open a file named __open_file ($ pdf, "PDFTest.pdf"); // start a new page (A4) __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 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. syntax:


$ 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.

Before calling PDF_begin_page ()Functions and Pai_end_page ()The code between functions 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. Pass Performance_findfont ()And Pai_setfont ()Function selection and font registration are very convenient, Performance_findfont ()The function prepares a font to be used in the document. the required parameters include the font name, encoding, and whether the font is embedded in the PDF file. Performance_findfont ()The function returns a font object that will be used in the __setfont () function.


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


Once we set the font, we can use Pai_show_xy ()The function writes a string to a specified position 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); 


   Pai_show_xy ()The 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 (0, 0) is in the lower left corner of the document. Once the text is written, the page can be closed. Pai_end_page ()Of course, you can also write more pages. After writing all the pages, use Pai_close ()The function closes the document, and the document is saved to the call. Performance_open_file ()The file handle is destroyed under the specified file name and path in the function.

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 = PDF_open_image_file($pdf, "jpeg", "PDFImageTest.jpg"); PDF_place_image($pdf, $image, 50, 650, 0.25); 


Is it easy? Pai_open_image_file ()Function to open an image file. acceptable image types include: GIF, JPEG, TIFF, and PNG. This function returns the image handle, Pai_place_image ()The function inserts an image into a PDF file using the preceding image handle. 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 = PDF_new (); PDF_open_file ($ pdf, "LineExam.pdf"); PDF_begin_page ($ pdf, 595,842); $ arial = pai_findfont ($ pdf, "Arial ", "host", 1); PDF_setfont ($ pdf, $ arial, 12); // set the color of the straight line pai_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 PDF_moveto ($ pdf, 20,780); PDF_lineto ($ pdf, 575,780); pai_stroke ($ pdf ); // draw another straight line at the bottom of the page: pai_moveto ($ pdf, 20, 50); pai_lineto ($ pdf, 575, 50); pai_stroke ($ pdf ); // output some text __show_xy ($ pdf, "Meng's Corporation", 200, 35); PDF_end_page ($ pdf); PDF_close ($ pdf );

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.