Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

converting to pdf using xslt

Posted on 2013-06-23
4
842 Views
Last Modified: 2013-06-24
Hello,

Here is one example where i want to convert to Pdf file.
This xml is getting converted  to an html using XSLT.

 but how to convert  to a  Pdf  file is my concern

======================
invoices-summary>
  <invoice id="">
    <issue-date>2011-10-17</issue-date>
    <amount>108</amount>
    <vat>19.47</vat>
    <vat-base>22</vat-base>
    <currency>EURO</currency>
    <customer-id>0001008</customer-id>
   </invoice>
  <invoice id="">
    <issue-date>2011-10-17</issue-date>
     <amount>40</amount>
    <vat>7.21</vat>
     <vat-base>22</vat-base>
     <currency>EURO</currency>
    <customer-id>0000017</customer-id>
  </invoice>
  <invoice id="">
    <issue-date>2011-10-17</issue-date>
    <amount>1700</amount>
    <vat>306.56</vat>
    <vat-base>22</vat-base>
    <currency>EURO</currency>
     <customer-id>0000040</customer-id>
  </invoice>
</invoices-summary>

=============================

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title/>
                <style type="text/css" xml:space="preserve">
                              body {
                                    font-family: arial;
                                    font-size: 12px;
                                    text-align: center;
                              }
                              table {
                                    border-collapse: collapse;
                                    width: 100%;
                                    border: solid black 1px;
                              }
                              table th, td {
                                    border: solid black 1px;
                              }
                              @media screen {
                                    body {
                                          width: 570px;
                                    }
                              }
                              @media print {
                                    table th {
                                          font-weight: bold;
                                    }
                              }
                        </style>
            </head>
            <body>
                <h2>Invoices summary</h2>
                <table>
                    <thead>
                        <tr>
                            <th style="width: 30px;">Crt.<br/>no.</th>
                            <th style="width: 80px;">Invoice<br/>no.</th>
                            <th style="width: 80px;">Issue date</th>
                            <th style="width: 80px;">Amount</th>
                            <th style="width: 80px;">VAT</th>
                            <th style="width: 80px;">VAT base</th>
                            <th style="width: 80px;">Customer ID</th>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:for-each select="//invoice">
                            <tr>
                                <td>
                                    <xsl:number value="position()" format="1. "/>
                                </td>
                                <td>
                                    <xsl:value-of select="@id"/>
                                </td>
                                <td>
                                    <xsl:value-of select="issue-date"/>
                                </td>
                                <td>
                                    <xsl:value-of select="concat(currency, ' ', amount)"/>
                                </td>
                                <td>
                                    <xsl:value-of select="vat"/>
                                </td>
                                <td>
                                    <xsl:value-of select="vat-base"/>
                                </td>
                                <td>
                                    <xsl:value-of select="customer-id"/>
                                </td>
                            </tr>
                        </xsl:for-each>
                    </tbody>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
0
Comment
Question by:roy_sanu
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Vijay Pratap Singh
ID: 39270310
Try this tutorial this will solve your query:

http://oreilly.com/catalog/orxmlapp/chapter/ch07.pdf
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39270324
@rickyzen, that link has nothing to do with converting to PDF !?

@roy_sanu,

The problem is that there is no direct link between xml/xsl and pdf. What you will most likely need to do, is do the XSL transformation to HTML as you are doing now, and then add an additional step to render the HTML to a PDF document.

A quick Google search gives the following options,

 - iText
 - Flying Saucer (uses iText internally but may provide useful extensions)

Note however, that because these will be using different rendering engines to what any of the normal browsers use, the output could well look different to what you see in a browser (just like how different browsers show some pages slightly differently)
0
 

Author Comment

by:roy_sanu
ID: 39270341
thanks let me try it out
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39270395
In case you get stuck, using iText and XMLWorker (I used the 5.4.1 versions from Maven)

http://itextpdf.com/download.php

...and with this code, it is made quite easy (you may want to play around with the styles, as iText will be different to what you normally see in a browser)
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class ConvertXMLtoPDF {
    
    public static void main(String[] args) throws TransformerFactoryConfigurationError, TransformerException, DocumentException, IOException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new File("transform.xsl")));
        StringWriter writer = new StringWriter();
        transformer.transform(new StreamSource(new File("input.xml")), new StreamResult(writer));
        
        Document document = new Document();
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
        document.open();
        XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, new StringReader(writer.toString()));
        document.close();
    }
}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question