dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Generate PDF Files Dynamically Using XSL-FO  : Page 2

Learn how to generate Adobe Portable Document Format (PDF) files from XML documents using XSL Formatting Objects (XSL-FO) and a Java Servlet.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Step 1: Transform XML Into a .fo File using XSL-FO:
Import the following JAXP packages to perform the transformation process.

   import javax.xml.transform.TransformerFactory;
   import javax.xml.transform.Transformer;
   import javax.xml.transform.stream.StreamSource;
   import javax.xml.transform.stream.StreamResult;
   import javax.xml.transform.TransformerException;
   import  javax.xml.transform.
      TransformerConfigurationException;
You start the process by creating a TransformerFactory instance.

   TransformerFactory tfactory =  
      TransformerFactory.newInstance();
Use the TransformerFactory.newTransformer() method to create an instance of the abstract Transformer class. The Transformer class transforms a source tree into a result tree.

You can use the Transformer instance to process XML from a variety of sources and write the transformation output to a variety of sinks. For example, the following code creates a Transformer object and loads it with an XSLT file:



   Transformer transformer=
     tfactory.newTransformer
     (new StreamSource(getServletContext().
     getResource(xslPath).
     toString()));
You use the Transformer.Transform() method to transform the input XML document into the output .fo file.

   transformer.transform(new StreamSource
   (getServletContext().getResource(xmlPath).
   toString()),new StreamResult(new 
   FileOutputStream(filepath )));
The Servlet constructs the file names for the xslPath and xmlPath variables from the Request object. The example passes the filenames and the servlet adds the extension.

   String xslName = request.getParameter("xsl");
   String xmlName = request.getParameter("xml");
   String xslPath = xslName +".xsl";
   String xmlPath = xmlName +".xml";
The Transformer saves the output of the transformation (the .fo file) to the file represented by the filepath variable. That completes the first step.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date