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:
XML Version = "1.0" encoding = "GB2312"?>
repeort_param>
record>
record>
repeort_records> report>
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:
PHP
$ xslt = new xmlTransformer ("Report.xsl", "Report.xml");
$ xslt-> apply ("pdfreport.xml");
?>
New XML documents generated after conversion are as follows:
XML Version = "1.0" encoding = "GB2312"?>
hEAD>