In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use LiveDocx and Zend Framework to generate template-based documents, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Using PHP to generate well-printed, well-formed PDF documents is not easy. Traditionally, there are two main ways to generate PDF using PHP. If you have enough time and patience, both can get the job done, but there are still many inadequacies:
HTML-to-PDF: this approach is widely used in mainstream applications. Here you can use one of the many open source libraries to programmatically create an HTML document and convert it to PDF. However, because HTML is not a page-oriented format (such as PDF), you cannot perform a 1-to-1 mapping between HTML and PDF. Typical word processing file format features, such as headers and footers, orphans and widows, and even page numbers cannot be represented in HTML.
Programmatic: this method has complete control over the generated PDF. However, it requires that the x and y coordinates of each line of text, each geometric shape and figure, be set from the program code. This is not only a time-consuming solution, but also fragile: every time a graphic designer changes the layout of a document, the programmer must rewrite his or her program code.
A whole new approach.
In this paper, the author puts forward a new third method. It relies on templates created in the WYSIWYG environment, such as Microsoft ®Word or Open Office, and then populates the data with PHP. The resulting document can be saved not only as PDF, but also as DOCX,DOC and RTF.
Before we delve into the inner workings of this new approach, let's look at a practical example. The following PHP 5 code illustrates PDF generation, where the merged field software in the template template.docx [46.7 KB] is populated by licensees and companies with scalar data in PHP. The resulting document document.pdf [104.7 KB] has been created and written to disk.
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge (array ('username' = >' yourUsername', 'password' = >' yourPassword')); $phpLiveDocx-> setLocalTemplate ('template.docx'); $phpLiveDocx-> assign (' software', 'Magic Graphical Compression Suite v1.9'); $phpLiveDocx-> assign ('licensee',' Henry Smith'); $phpLiveDocx-> 'Company', 'Megasoft Cooperation') $phpLiveDocx-> createDocument (); $document = $phpLiveDocx-> retrieveDocument ('pdf'); file_put_contents (' document.pdf', $document); unset ($phpLiveDocx)
The code demonstrated in this article will be provided with Zend Framework 1.10. Although there is no official release date at the time of writing, 1.10 is expected to be released in the fourth quarter of 2009. In the meantime, you can examine the components in the standard incubator SVN repository.
Introduction to LiveDocx
LiveDocx is a SOAP-based document generation service based on TX Text Control .NET, a market-leading word processing component. LiveDocx allows word processing templates to be populated in any programming language that supports SOAP. The resulting document can be saved in any supported format. However, this article focuses on how to use LiveDocx in PHP 5.
The components of LiveDocx's Zend Framework implementation are located in / Zend / Service / LiveDocx / in the standard Zend Framework distribution file. You can use LiveDocx directly with PHP 5 SoapClient, without Zend Framework, or you can use a third-party library, NuSOAP. The NuSOAP method even allows the use of LiveDocx in PHP 4. However, this article focuses on the official Zend Framework components in PHP 5.
The key point of LiveDocx is to minimize the amount of work required to generate well-formed, printable word-processing documents. For the end user, the logic involved in creating any supported file format is the same. For example, the code (except for one parameter) is the same whether you want a PDF or a RTF file.
The core developers of LiveDocx also want to make sure that templates are generated as easily as possible, in an environment that end users are already familiar with. Therefore, you can create templates in Word or Open Office.
Templates and documents
Throughout the article, we refer to the term templates and documentation. It is important to understand the difference between the two.
Template: the term template is used to refer to the input file and contains formatting and text fields. Templates can be in any of the following file formats:
DOCX-Office Open XML format
DOC-Microsoft ®WordDOC format
RTF-Rich text format
TXD-TXTextControl ®format
Templates can be stored locally on the client computer (the computer that initiated the SOAP request) or remotely on the back-end server. Deciding which one to use depends on the type of application you are developing.
If you store the template locally, you must transfer the template with the data that should be populated on each request. This method is very inefficient if the template remains the same in each request. It is best to upload the template to the back-end server once and then reference it in all subsequent requests. In this way, only the data that should be populated is transferred from the client to the back-end server. Most applications that use LiveDocx fall into this category.
On the other hand, if you have an ever-changing template, or if you allow end users to upload templates, you can consider storing templates locally and transmitting them on each request. This approach is obviously slow because each request contains the template itself and the data that populates it.
Documentation: the term document is used to refer to the generated output file, which contains template files that populate the data-that is, the completed document. Documents can be saved in any of the following file formats:
DOCX-Office Open XML format
DOC-Microsoft ®WordDOC format
HTML-XHTML 1.0 transition format
RTF-Rich text format
PDF-Acrobat ®Portable document format
TXD-TX text control format
TXT-ANSI plain text
In addition to the above word processing file format, the document can also be saved in the following image file format:
BMP-bitmap image format
GIF-Graphics Interchange format
JPG-Joint Photographic expert Group format
PNG-Portable Network Graphics format
TIFF-tagged image file format
WMF-Windows metafile format
Use LiveDocx
In this section, we will look at the entire process of creating a document using LiveDocx from scratch.
Create a template in Microsoft ®Word2007
The first step in any LiveDocx project is to create a template. To do this, you can use Open Office or Microsoft ®Word. For the purposes of this article, we will use Microsoft ®Word2007. For instructions on using Open Office, check out the LiveDocx blog.
Insert merge fields in Microsoft ®Word2007 first create a new file in Microsoft ®Word2007, and then save the template file as template.docx.
You can then start writing the template, using the Fields dialog box to insert text, graphics, and merged fields, as shown in the figure.
After a while, you will have a template that contains images, text, and many merged fields. The merge field is represented by {MERGEFIELD name} and will be populated with scalar data in the next step. The following screenshot of the template in Microsoft ®Word2007 illustrates what the template looks like:
Save the template template.docx [/ img] [46 KB] when finished.
Assign scalar data types in LiveDocx
Now that we have the template file, the next step is to populate it with data. In the following example, we will assign a scalar data type (in this case, a string) to the template.
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge (array ('username' = >' yourUsername', 'password' = >' yourPassword')); $phpLiveDocx-> setLocalTemplate ('template.docx'); $phpLiveDocx-> assign (' software', 'Magic Graphical Compression Suite v1.9'); $phpLiveDocx-> assign ('licensee',' Henry Smith'); $phpLiveDocx-> 'Company', 'Megasoft Cooperation') $phpLiveDocx-> assign ('date', October 10, 2009'); $phpLiveDocx-> assign ('time',' 14:12:01'); $phpLiveDocx-> assign ('city',' Frankfurt'); $phpLiveDocx-> assign ('country',' Germany'); $phpLiveDocx-> createDocument (); retrieveDocument ('pdf'); file_put_contents (' document.pdf', $document); unset ($phpLiveDocx)
For many applications, especially those that use PDF files for archiving, you may want to set up metadata for PDF files. You can do this by specifying an associative array using the metadata that should be embedded in the PDF file. CreateDocument () must be called before the () setDocumentProperties method:
$documentProperties = array ('title' = >' Magic Graphical Compression Suite v1.9', 'author' = >' Megasoft Co-operation', 'subject' = >' Magic Graphical Compression Suite v1.9', 'keywords' >' graphics, magic, compression, suite, license'); $phpLiveDocx-> setDocumentProperties ($documentProperties)
The resulting document document.pdf [104 KB] is written to disk and can now be opened in your favorite PDF reader, such as the document viewer that comes with Ubuntu:
Assigning compound data types in LiveDocx
In addition to the scalar data types assigned to the template in the previous example, compound data types, such as associative arrays, can also be assigned. Consider the template template.doc [20.5 KB] and the generated document document.pdf [77.6 KB]. In particular, take a look at the following section of the template (click to enlarge):
The template portion between the bookmarks in Microsoft ®Word and Microsoft ®Word is repeated in the final document to generate the rows of the table. A subarray of the following associative array is used for each row.
Using the following PHP 5 code, we will populate the template with an associative array of phone connection data. For clarity, this example shows only the portion of the associative array assigned. The process for instantiating LiveDocx and creating and retrieving documents is the same as in the previous example and has been omitted:
/ / instantiate LiveDocx $billConnections = array (array ('connection_number' = >' + 11 (0) 222 333 441', 'connection_duration' = >' 00Rou 01R 01L', 'fee' = >' 1.15'), array ('connection_number' = >' + 11 (0) 222 333 442', 'connection_duration' = >' 001R RV 02', 'fee' = >' 1.15'), array ('connection_number' = >' + 11 (0) 222 333 443') 'connection_duration' = >' 00VO1VOR 03', 'fee' = '1.15'), array (' connection_number' = >'+ 11 (0) 222 333 444' 'connection_duration' = >' 00VO1VOR 04', 'fee' = > '1.15')) $phpLiveDocx-> assign ('connection', $billConnections); / / create and retrieve documents
The resulting document contains the following table containing the data in the assigned associative array (click to enlarge):
Use LiveDocx to generate image files
In addition to the word processing file formats supported by LiveDocx listed above, you can also save the resulting document as one or more image files. To do this, Zend_Service_LiveDocx_MailMerge provides methods getAllBitmaps () and getBitmaps ():
/ / instantiate LiveDocx / / get all bitmaps / / (zoomFactor,format) $bitmaps = $phpLiveDocx-> getAllBitmaps (100, 'png')
Similarly, images of pages within a specific range can be retrieved:
/ / get the bitmap within the specified range only / / (fromPage,toPage,zoomFactor, format) $bitmap = $phpLiveDocx-> getBitmaps (2,2,100, 'PNG')
Note the zoomFactor parameter. This is a percentage value, in the range of 10% to 400%. These methods are ideal for generating thumbnail images of the documents you create, for example, as previews in the browser.
By iterating through the $bitmaps array, you can write the actual image file to disk. Each record in the array has one page of binary data:
/ / write to disk / / (one page per record) foreach ($bitmaps as $pageNumber = > $bitmapData) {$filename = sprintf ('documentPage%d.png', $pageNumber); file_put_contents ($filename, $bitmapData); printf (' write% d bytes to disk as% s. % s', filesize ($filename), $filename, PHP_EOL);} deploy LiveDocx in your own application
The code provided in the Zend framework that makes up LiveDocx's PHP 5 implementation is released under the new BSD license, so it can be deployed, modified, and redistributed in most projects under the license terms. However, the actual LiveDocx SOAP server is proprietary software. You can deploy SOAP services in your own applications in three ways.
Free public server
For the vast majority of applications, developers choose this approach. The default LiveDocx server referenced in the Zend Framework component is a free public server. It can be used for your own applications completely for free. Sign up for a LiveDocx account.
Advanced managed Server
If your application generates thousands of documents per hour, you might consider paying a small monthly fee to access your own personal LiveDocx server. In partnership with leading managed service providers, you can rent such an advanced managed server.
Local license server
If your application generates more than 10, 000 documents in an hour, you can consider installing a LiveDocx server on your local network. Direct access in a local gigabit network is by far the fastest way to deploy LiveDocx.
The above is all the content of the article "how to generate template-based documents using LiveDocx and Zend Framework". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.