There are applications in which thousands of populated templates need to be concatenated into one PDF file. Consider, for example, the case of a telephone company, wishing to generate an invoice for all its customers. Such a scenario can be split up into two separate and distinct processes: invoice generation and invoice printing.
LiveDocx is the tool of choice for the invoice generation process. But how do you efficiently get a printed copy of the invoices to the customer? One way would be to concatenate all the created invoices into one massive PDF file and then dispatch this PDF file to the printers.
Backend concatenation
As of LiveDocx 1.2, it has been possible to assign multiple records in the form of a nested associative array to a template. The result is one concatenated document, containing the populated template (one per nested sub-array).
This backend concatenation approach is a fantastic way to create hundreds of documents in one run, but in the case that your application demands thousands or even tens of thousands of documents, the amount of data that has be transfered via SOAP in one request does get extremely large and depending upon your Internet connection, can become a little slow and consequently burdensome.
This blog post offers a new approach to generate documents using LiveDocx on the backend, but perform the concatenation locally. The following screenshot illustrates the sample application in action (click to zoom):
The code has been written for the official Zend_Service_LiveDocx_MailMerge component, which is currently in the Standard Trunk. By modifying the constructor, however, you can easily make it work with Tis_Service_Service_MailMerge.
Generate remotely, concatenate locally
In the sample application, documents are created by the backend LiveDocx server and concatenated locally, using either pdftk or Ghostscript in the function concatenatePdfFilenames(). This approach has the advantage that only the singular documents are transfered from the backend LiveDocx service via SOAP to the server and not the entire concatenated document, which theoretically can be many gigabytes in size. The disadvantage, of course, is that one or these tools must be installed on the local server. However, as both are open source and available for Windows and Linux, this should not pose such a problem. Often they will already be installed on your server.
If both pdftk and Ghostscript are available, it is recommended to use pdftk, as it is considerably faster and uses less memory. Ghostscript, on the other hand, generates slightly small PDF files.
Try it out yourself
The source code to this sample application is located at /demos/Zend/Service/LiveDocx/MailMerge/license-agreement/generate-document-concat.php of the Standard Trunk. Simply checkout the entire repository or update your local working copy. If you are just curious about what the code looks like, you can view the source code here: generate-document-concat.php.
If you have any questions about this local concatenation approach, please post into the comments below.
