In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to write and generate PDF files using TCPDF". In daily operation, I believe many people have doubts about how to write and generate PDF files using TCPDF. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to write and generate PDF files using TCPDF". Next, please follow the editor to study!
I. demand
A contract needs to be generated online, and some of the content is fixed, while the other part needs to be added and edited.
The editing part is to fill in the contents of the form, which covers the two main html elements of table input.
Now you need to generate a PDF file to save locally and download a file after editing the contract on the page.
Second, choose TCPDF
TCPDF address: https://tcpdf.org/
1. The reason for choosing
There is no need to install any other dependencies, which is my favorite reason to use it.
two。 Installation
You can install it using composer. The address in packagist is: https://packagist.org/packages/tecnickcom/tcpdf
Author tecnickcom has all kinds of tcpdf-related packages: https://packagist.org/packages/tecnickcom/
Composer require tecnickcom/tcpdf3. Mode of use
Because you operate on html and then generate pdf, the method used is writeHtml ()
Many examples on the official website of tcpdf can be used directly. For example, writeHtml () has special examples to illustrate:
Https://tcpdf.org/examples/example_006/
4. Sample code
The following code needs to be noted:
I have removed some of the content, and if you need it, you can add it by referring to the example on the official website.
For example, I removed the header and footer, and the page spacing is 10.
The final writing method is $pdf- > writeHtml (), and the parameters can be written using the recommended parameters.
/ create new PDF document$pdf = new TCPDF (PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); / / set document information$pdf- > SetCreator (PDF_CREATOR); $pdf- > SetAuthor (' pdf author'); $pdf- > SetTitle ('PDF title'); $pdf- > setPrintHeader (false); $pdf- > setPrintFooter (false); / / set default monospaced font$pdf- > SetDefaultMonospacedFont (PDF_FONT_MONOSPACED); / / set margins$pdf- > SetMargins (10,10,10); $pdf- > SetHeaderMargin (PDF_MARGIN_HEADER) Pdf- > SetFooterMargin (PDF_MARGIN_FOOTER); / / set auto page breaks$pdf- > SetAutoPageBreak (TRUE, PDF_MARGIN_BOTTOM); / / set image scale factor$pdf- > setImageScale (PDF_IMAGE_SCALE_RATIO); / / Add a page// This method has several options, check the source code documentation for more information.$pdf- > AddPage (); / / set default font subsetting mode$pdf- > setFontSubsetting (true) / / Set font// dejavusans is a UTF-8 Unicode font, if you only need to// print standard ASCII chars, you can use core fonts like// helvetica or times to reduce file size.$pdf- > SetFont ('msyh',', 11,'', true); / / set text shadow effect$pdf- > setTextShadow (array ('enabled'= > false)); / / Set some content to print$html =' input {border:0;border-bottom: 1px solid # cccccc;padding:5px } Test generation PDF AAAA BBBB CCCC'; / / Print text using writeHTMLCell () / / $pdf- > writeHTMLCell (0,0,',', $html, 0,1,0, true,', true); / / output the HTML content$pdf- > writeHTML ($html, true, false, true, false,'); / / reset pointer to the last page$pdf- > lastPage () / /-- / Close and output PDF document// This method has several options, check the source code documentation for more information.$pdf- > Output ('example_001.pdf',' D'); / / = = + / / END OF FILE//===+5. Generate the result
3. Inevitable problems 1. XHTML
It should be noted that when using writeHtml (), XHTML is supported instead of HTML. Of course, HTML5 does not support it.
So when building the html code for the front-end page, use xhtml, otherwise some of the content will become invalid.
For example:
1) the input form must be closed
XHTML requires that all single tags must be self-closing and sum, but tcpdf naturally cannot recognize it.
Like above, input is unrecognizable if it is written according to the html5 standard.
2) if there are few styles, it is recommended to write them in the inline style > tag in tag.
Although TCPDF supports this way, writing such as margin-top does not take effect in tags, but writing in inline style actually works!
2. The way to generate PDF files
The last $pdf- > Output ('example_001.pdf',' D'); has two parameters:
The first parameter is the file name
The second parameter is the export method
1. The commonly used export methods are:
I: direct online preview of binary pdf files (note that in some PHP frames, it cannot be displayed directly and garbled will be displayed)
D: download directly to the client computer and automatically preview it in the browser
FD: save a copy on the server, download it to the current client and preview it in the browser
F: stored in the server, not downloaded on the client
2. If you choose F or FD, you may encounter mistakes like fopen or something.
The solution is that if you choose to save on the server (F or FD), the file name of the first parameter requires an absolute path, such as:
$pdf- > Output ('/ data/pdf/example_001.pdf','FD')
Then the problem can be solved.
IV. Chinese garbled code
Chinese must be garbled, and you don't have to think about it.
The simplest solution:
1. Download Chinese fonts supported by tcpdf
What I found in CSDN is Microsoft Yahei, which is available for personal testing. Download address:
Http://download.csdn.net/detail/wenyang800/9561869
two。 Replace the file
The compressed package contains three msyh.php,msyh.z,msyh.ctg.z files, which are directly placed in the fonts directory of tcpdf.
Use setfont ("msyh", "11) to set the font size of the generated pdf
I have set the Microsoft Yahei font in the above code, and then there will be no problem in Chinese.
At this point, the study on "how to write and generate PDF files using TCPDF" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.