This blog talks about on our project for one of the clients to create dynamic PDF using web service (XLST Transformation). Below are the sections in this blog:
- Overview: What is XSLT transformation
- A sample workflow
- Practical Use cases
- Sample PDF Generated
- Code Snippet
XSLT is a language for transforming XML documents into other XML documents or other different formats such as HTML web pages, plain text files or XSL-FO (formatting objects) which subsequently can be converted into other so-called standard formats such as PDF, PNG etc. The Xtensible Stylesheet Language is based on XML and consists of two parts:
- XSL Transformation (XSLT): XML to XML, XML to HTML and XML to text
- XSL-FO: describes print documents
XSLT can be used to convert XML to HTML and XSL-FO doesn’t have to be generated by XSLT but can also be generated through any XML-capable template engine or custom application code
A Sample workflow:
XML document gets generated from some data source. The XML is used to create an XSL-FO document using an XSLT processor. The FO processor then processes the XSL-FO document by layouting the content to individual pages which are written to a print spool. Finally, the print spool gets printed.
Practical Use Case: Overview
We created a dynamic PDF document which had fixed fields on one side and customized variable information (customer information) mapping to those fixed fields. The variable information, i.e. customer specific information, can be updated at one go and multiple pdfs with barcodes mapping to each customer information can be printed all at once.
In the subsequent pages, you’ll find the sample PDF document that gets created and the code snippets.
Sample PDF Document
As you can see, the fields descriptions on the LHS are fixed i.e. ‘Car Number’, ‘Name ID’ and ‘Phone’.
The field values on the RHS are specific to each customer and the barcodes also display the information pertaining to each specific customer
The overall formatting and presentation (company logo, borders etc.) can all be customized and configured as per requirements.
Input code snippet (via XML): The requisite values (such as Car number, name and phone) are entered into the system
Processing Code Snippet àBarcoding: The barcoding is done as shown below. Currently, it is fetching display information from the mentioned website (bold in red below) but can be customized as required. Further, the barcodes placement in the sheet can also be adjusted as required.
Image Input: The code below is for inserting the logo image from a predefined folder location. The function can either call the image from a folder or from any defined path. Further, the image can be positioned anywhere across the page as per the requirement.
Sheet formatting: The overall page margins can be set as per the requirements.