PDF document generating technology based on PHP and XML (summary)

zhaozj2021-02-16  43

Full text See: http://www22.brinkster.com/cymanhome/show.asp? Id = 85

Summary

This paper briefly introduces the principles of PHP, XML, PDF and other technologies and their application. Forced to use PHP-oriented properties to build a set of online PDF document generating systems based on PHP and XML. The components of the entire system and their respective implementation processes are discussed in detail. And give an example of a dynamically created report using this system implementation.

Abstract

This article introduced the fundamentls of PHP, XML and PDF and their application situation at present, expecting to build a PHP & XML-based dynamic PDF documents creating system through the PHP's OO features. Furthermore, we discussed in detail on the components of the whole system and .

Key words

PHP XML XSLT DTD PDF

1 Introduction

In the era of rapid development of information technology, regardless of the government, enterprises or individuals, they have a strong interest in how to improve their work efficiency through information technology. They are eager to find an excellent technology, which can transition traditional paper, reports, ticket documents, manuals, applications, etc., etc., etc., etc., etc., can be very convenient to generate automatically in the Internet and internal networks. Spread, download, browse, print electronic documents. Nowadays, "Paperless Office", "e-commerce", and so on now will be based on this.

This document format is the public utility standard for electronic document distribution of electronic version of the world in Adobe's PDF (portable document format). Any browser can freely browse, download, and print the PDF document freely as long as you install the Acrobat Reader 5.0 plugin. PDF has undoubtedly have other electronic document formats unparalleled superiority.

We know that the B / S system as the current and most popular software architecture, which can achieve a variety of browser-based web applications, while PHP is used as an excellent web programming language, especially suitable for development User form input, query database, etc. For the front-end application for the browser user. Since PHP is open source, this makes it more widely used than other similar Web scripts, and its functions are constantly expanding and perfect. Now the latest PHP version is already good to support PDF, XML, etc. Through the API provided by the system, we can generate a PDF document very quickly, and the most charming thing is that we can form all kinds of PDF documents through PHP, query the database or XML data file and insert the result in the generated PDF document. Good browsing and printed reports, documents, manuals, etc.

It is not difficult to see that the three technologies of PHP, XML, PDF are constructed, and a system that can be dynamically generated by the PDF document is very realistic, and its main performance is:

· Documentation can be generated on the network and distribute through network. Save a lot of human material. It has precise and beautiful printing effects that truly realize paperless work.

· Various tickets in the e-commerce transaction process can be generated online through the PHP script and is sent to the PDF format.

· The generation of various-oriented statements in the enterprise MIS system, and can be obtained directly through the browser, no need to install the client, which is extremely convenient.

· The previous file is transferred to be "printed, then distributed", the cost of spending in printing every year is the government, the heavy burden of the company. And the PDF document "exits first, print", the snap can be printed as needed after browsing. The cost of printing is greatly reduced. Moreover, it is very conducive to environmental protection. 2. Introduction to the topic

During the development of some software projects, we encountered a key issue is a large number of printed reports, and the generation of documents. We know that HTML is suitable for browsing, but it is not suitable for printing of format specification. It is therefore necessary to find a document format that can be dynamically generated by PHP, and having a good printing effect. And this is the most direct demand that I study this topic. Understand this, we naturally think of PDF and PHP PDF support library PDFLIB. With a set of APIs provided by PDFLIB, we can easily create a PDF document dynamically in PHP scripts. But this is just a very basic function, only some simple outputs, such as lines, text, rectangular boxes, etc., and specify coordinates for each of them before an object is output. If you use this function directly to do some actual applications, such as complex reporting build, it is unimaginable. We can't create such a report, while computing the coordinates of each element, and draws a single line with a rectangular box.

So, our first step is to use the PHP object-oriented programming method to encapsulate the basic API to generate multiple practical object modules with independent features (such as Page objects, Table objects, Text objects, etc.) . It should be said that this piece is the most basic and most important part of this project. I am using and partially adopted

Some similar open source procedures have developed a set of powerful class libraries on this basis. It greatly simplifies the generation of PDF documents, especially in the Table object, you can easily quickly and easily achieve a variety of complex tables as any of the Table tags in HTML (this is very useful for dynamic generating reports).

After generating PDF issues, we face new problems, for example, how to pass the result set of information containing a lot of information to the PDF generating page? The method we think is to pass the text file, that is, write the data to a text file in the database query page, and

Different categories of data define a set of distinct tags, the PDF generating page reads this file, insert the content into the PDF. But this is not reliable. Because in this text file, we use specific characters (or spaces) to separate data, if you just contain the same characters or spaces in useful data? It can be seen that data is hidden in this way. In fact, we mentioned that different categories of data are distinguished in a text file. And this is the idea of ​​XML technology. How does it take to use XML as a means of data delivery? Moreover, PHP has good support for XML and XSLT. With the EXPAT parser, we can use the data in the XML document, or convert the XML document with the XSLT engine Sablotron for PHP.

First, data (from database or user input, etc.) is placed in an XML document that meets prior defined DTDs, which describes the data content in this document, does not contain any format information. This XML document is then converted to another XML document containing display style information by "XML Converter". Finally, this document is read by "PDF Builder", and the corresponding PDF document is generated according to the contents and display styles. In this process, I have to do the object-oriented characteristics of PHP again, build reusable classes: XMLWriter, XMLParser (parsing XML file), and XMLTransformer (package for XSLT functions). After the system is successfully constructed, it is specific application. Mainly, various reports, documents, documents, documents.

3. Feasibility analysis

Develop a powerful, adaptive PDF document online generation system, inevitably need to be elastic, flexible

High development model. We propose PHP and XML-based PDF document online generation technology, providing new ideas for printing applications, such as reports, single tickets, manuals, etc. We use PHP to query the database, process user input, and generate raw XML documents on this basis; then add the XML document with display layer information to generate another new XML document. Finally, the "PDF Builder" is used to convert this new XML document into the PDF document in the corresponding format. For the initially generated XML document, I can do a two-degree utilization because the document contains all useful information, which can be easily handled by other applications. If we want to change the style of the information in the PDF document, it can be very easy to implement. As long as you have a specific person to modify the corresponding XSL style single file, you don't need to make any modifications to other links, it has very good flexibility. In addition, both PHP, XML, and PDF have excellent portability and can be used across platforms. The study of the system is not imagined, it is established on top of direct demand. So far, this technology has been put into practical applications and received extremely satisfactory results. Practice has proven that a set of online PDF document generated systems developed by PHP and XML has broad and very practical application prospects.

4 overall design

This topic mainly completes the design and programming of four basic modules. These four class modules are PDFCreator, XMLWriter, XMLTransFomer, and XMLParser. They are distributed in all aspects of the system, with their respective independent functions and functions, are the core components of the system (see the figure below).

System constitute

As can be seen from the figure, four of the organic integrals are closely linked in this system. XMLWRITER as the system's input

In the interface, responsible for generating the original XML data file. The format specification (DTD) of this file is written in advance, and XMLWRITER generates the corresponding XML document in accordance with the DTD. This XML document will then be handed over to XMLTransFomer. XMLTransFomer is actually packaged for the XSLT function provided by PHP. It generally accepts two parameters, one of which is the XML document that needs to be converted, and the other is a corresponding XSL style single file. XMLTransFomer is transforming the original XML document to another XML document that complies with this style single file according to the style single file (containing information placed in the PDF document). Then, this new XML file continues to be handled by "PDF Builder". And this process is divided into two parts: First, this XML document must be parsed, extract the data required, this step has XMLParser to complete, XMLParser parsing this XML document, converting it into an object tree Each node in the XML document is an object, each with its own properties (ie all information about the corresponding node). In this way, we can very convenient access to any of this XML document. Thereafter, what to do is to convert the information read from the XML document (including format information and content information) to the final PDF document output by pdfcreator. 5. Application example

Here, we use the above system to create a printed report - "inventory history

Table ". The information contained in this report is: Report Name (Associate Inventory History), unit, Jian Table Date, etc., the data extracted from the database, the product name (LLPROD), LLOC ), Warehouse (LWHS), LLOCT, quantity (lnum), date (LDATE), etc. Suppose we have generated the following original XML document (Report.xml) with XMLWRITER:

Inventory History Statement </ Title></p> <p><unit> square meter </ unit></p> <p><Date> 20020611 </ date></p> <p></ repeort_param></p> <p><Report_Records></p> <p><record></p> <p><LLPROD> W2308 </ llprod></p> <p><lloc> 1234 </ lloc></p> <p><LCLS> a </ lcls></p> <p><lwhs> 01 </ lwhs></p> <p><lloct> 0001 </ lloct></p> <p><lnum> 200 </ lnum></p> <p><LDATE> 20020609 </ ldate></p> <p></ record></p> <p><record></p> <p><LLPROD> W2307 </ llprod></p> <p><lloc> 4321 </ lloc></p> <p><LCLS> a </ lcls></p> <p><lwhs> 01 </ lwhs></p> <p><lloct> 0001 </ lloct></p> <p><lnum> 100 </ lnum></p> <p><LDATE> 20020609 </ ldate></p> <p></ record></p> <p></ repeort_records> </ report></p> <p>This document contains all useful information for this report, and we need to use the specific XSL style to add format information. XMLTransFormer executes the conversion code as follows:</p> <p><? PHP</p> <p>$ xslt = new xmlTransformer ("Report.xsl", "Report.xml");</p> <p>$ xslt-> apply ("pdfreport.xml");</p> <p>?></p> <p>New XML documents generated after conversion are as follows:</p> <p><? XML Version = "1.0" encoding = "GB2312"?></p> <p><pdfreport pagetype = "a4" pagesize = "25" TOP = "20" bottom = "20" left = "20" Right = "20"></p> <p><HEAD></p> <p><line top = "5" bottom = "5" size = "50%" lineType = "single" show = "false" /></p> <p><text fontsize = "30" fontlaguage = "cn" align = "center"> inventory history transaction table </ text></p> <p><line top = "5" bottom = "30" size = "80%" lineType = "double" show = "true" /></p> <p><text fontsize = "12" fontlaguage = "cn" align = "left"> unit: square meter </ text></p> <p></ hEAD></p> <p><body></p> <p><table></p> <p><TR> <TH> Product Name </ TH> <TH> Batch No. </ TH> <TH> Level </ TH> <TH> Warehouse </ TH> <TH> Library Chat </ TH> <TH> Number </ TH> <TH> Date </ tH> </ tr></p> <p><Tr> <TD> W2308 <TD> <TD> 1234 </ TD> <TD> a </ TD> <TD> 01 </ TD> <TD> 0001 </ TD> <TD> 200 </ TD > <TD> 20020609 </ TD> </ TR></p> <p><Tr> <TD> W2307 <TD> <TD> 4321 <TD> <TD> a </ TD> <TD> 01 </ TD> <TD> 0001 </ TD> <TD> 100 </ TD > <TD> 20020609 </ TD> </ TR></p> <p></ TABLE></p> <p></ body></p> <p><foot></p> <p><line top = "5" bottom = "5" size = "50%" lineType = "single" show = "false" /> <text fontsize = "12" fontlaguage = "cn" align = "center"> Date: 20020611 </ text></p> <p></ foot></p> <p></ pdfreport></p> <p>After the XMLParser is parsed to the XML document, you get an object tree containing all information, and we can easily access the content. The generated PDF report is as follows:</p> <p>The sequence is as follows:</p> <p><? Incrude ("../include/pc_init.inc");?></p> <p><? Include ("xmlparser.inc");</p> <p><?</p> <p>$ xmlobject = getrootnode ("report.xml");</p> <p>// Get the attrs of root element</p> <p>$ PageSet = $ XMLOBJECT-> ATTRS;</p> <p>// Get the report-head</p> <p>$ Head = $ XMLObject-> Nodes [0];</p> <p>// Code ignored ...</p> <p>?></p> <p><?</p> <p>Function Draw_Line (& $ PARENT, $ LINE) {</p> <p>$ line = & pc_create_Object ($ PARENT, "LINE");</p> <p>$ line-> pc_set_linestyle ($ line-> attrs ["lineType"]);</p> <p>$ line-> pc_set_width ($ line-> attrs ["size"]);</p> <p>$ line-> pc_set_alignment ("center");</p> <p>IF ($ line-> attrs ["show"] == false) {</p> <p>$ line-> pc_set_linecolor ("white");</p> <p>}</p> <p>$ line-> pc_set_margin ("TOP" => $ line-> attrs ["top"], "bottom" => $ line-> attrs ["bottom"], "left" => 0, "right" => 0));</p> <p>}</p> <p>Function Draw_Text (& $ PARENT, $ text) {</p> <p>// Code ignored ...</p> <p>}</p> <p>Function Draw_Table (& $ PARENT, $ TABLE) {</p> <p>// Code ignored ...</p> <p>}</p> <p>Function Addhead (& $ PARENT, $ HEAD) {</p> <p>For ($ I = 0; $ i <$ hEAD-> N; $ i ) {</p> <p>Switch ($ Head-> Nodes [$ I] -> Name) {</p> <p>Case "Line": Draw_Line ($ PARENT, $ HEAD-> NODES [$ I]); Break;</p> <p>Case "text": Draw_text ($ PARENT, $ HEAD-> NODES [$ I]); Break;</p> <p>}</p> <p>}</p> <p>}</p> <p>// ..</p> <p>?></p> <p><?</p> <p>// CREATE A PDF Document $ PDF = & PC_CREATE_PDF (Array ("Author" => "cyman", "title" => "a report example);</p> <p>// Create an A4-Format Page</p> <p>$ Page1 = & PC_CREATE_PAGE ($ PDF, $ Pageset ["PageType"]);</p> <p>Addhead ($ PAGE1, $ HEAD);</p> <p>$ PDF-> PC_DRAW ();</p> <p>?></p> <p>6. Summary</p> <p>Although it is busy during the graduation design of a few months, it is very fulfilling. Through analysis, research, argumentation, and implementation of an actual topic. I feel a lot. At present, this system has been put into use, and it has received very satisfactory results, which can be easily made of aesthetically practical report, document, etc. However, this system still has many shortcomings due to the limited time rush and its own level. The most regret that there is no set of XML tags that are common to various documents (including reports, documents, manuals, etc.), and prepare universal programs to convert this XML document to PDF, as the browser resolves HTML. same. This does not have to define each document to define their respective XML tags and write a corresponding conversion program, which can greatly improve work efficiency.</p> <p>Although the graduation design has ended, I will continue to study this issue in the future.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-27950.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="27950" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.031</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '1qHOVRfbdZn7A268zxgUDO2RqUxaVoyODOGZARnujGaXD_2BlMZ2KJslFWAUlbLcRgZFgvFXHT4Xvdbt8F5dyA2A_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>