4/27/2021 0 Comments How To Generate Pdf File Using Php
For this purpose we will use the popular PHP library FPDF which will enable us to generate the PDF file with the content and ourput format we desire.Sed vel velit erat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.There are a lot of open source libraries in php that can be used to generate pdf files from html, but most of them lack some pdf features such as how to manipulate text written in Right to Left languages, also handling the different font types.TCPDF in the other hand is a powerful open source library that has almost all the features needed to generate a well working pdf files, from these features: all standard page formats, custom page formats, custom margins and units of measure.
Generate File Using Php Code And RightUTF-8 Unicode and Right-To-Left languages TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts. Check out TCPDF official website to figure the rest of the available features. Generate File Using Php Download Usage GoDownload Usage Go to the github repository of TCPDF and clone or download it. You need to add the library in your project folder and then include it like this: includeonce(TCPDFtcpdf.php); By including the main tcpdf.php you have access to all classes and methods. Configuration The main configuration file located in TCPDFconfigtcpdfconfig.php. This file contains the general configuration that applies as per document basis when generating pdf files. Try to uncomment and change the configuration values to suite your needs. Adjust these configurations values: Installation path (varwwwhtmlproject folderTCPDF). As shown i have created a new folder for images in the project root folder, this will be used to store the header logo: define (KPATHIMAGES, KPATHMAIN.images). In the first step we setup the overall document appearance, this include setting fonts, colors, document title, header, footer, and the margins. In the second step we begin by creating the pages by calling method AddPage(), then prepare the html to display, then call writeHtml() method and finally output the file. In the above code we begin the first step by creating a new instance of TCPDF using the TCPDF() constructor like this: pdf new TCPDF(PDFPAGEORIENTATION, PDFUNIT, PDFPAGEFORMAT, true, UTF-8, false). Then i set the metadata for the document like creator and author: pdf-SetCreator(Wael Salah). The other two arguments represents the RGB color for the text and line. After that i set the header and footer fonts as shown: pdf-setHeaderFont(array(PDFFONTNAMEMAIN,, PDFFONTSIZEMAIN)). Its important also to set margins for the document, this achieved by calling those methods: pdf-SetMargins(PDFMARGINLEFT, PDFMARGINTOP, PDFMARGINRIGHT). Also for the images i set the scale ratio to be 1.25 by calling setImageScale(). Finally i set the font for the document text by calling setFont() method which takes the font family, style, size, and the font file. The second step i added a new page like this: pdf-AddPage(); When ever you need to add a new page you must call this method. Next i specified the html text to be written to pdf file then we called writeHtml() method to write the html to the pdf page: html Demonstrating pdf with php. Adding new pages: Adding new pages is done easily by calling AddPage() method, lets add two more pages to the above example, add this code after pdf-writeHTML(html): pdf-AddPage(). Sed neque urna, ultrices ac volutpat vel, laoreet vitae augue. Sed vel velit erat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |