Search code examples
javaxmlxsltpdfcss-to-pdf

Java generating PDF from XML


Still I am trying to generate a pdf from xml. I found a way through the HTML, but I'll let any modifications, the code still contains errors.

Can you help me? I am desperate :/

A use examplary code, bud i edited xsl only - http://thinktibits.blogspot.cz/2011/05/java-itext-convert-xml-to-pdf-example.html

import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

Java:
public class FAtoPDF {
 public static void main(String[] args) 
            throws IOException, TransformerException,TransformerConfigurationException,FileNotFoundException, DocumentException {                  

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(new StreamSource("sample.xsl"));
        transformer.transform(new StreamSource("report.xml"),new StreamResult(new FileOutputStream("sample.html")));
        String File_To_Convert = "sample.html";
        String url = new File(File_To_Convert).toURI().toURL().toString();
        //System.out.println(""+url);
        String HTML_TO_PDF = "ConvertedFile.pdf";
        OutputStream os = new FileOutputStream(HTML_TO_PDF); 
        ITextRenderer renderer = new ITextRenderer();
        //renderer.setDocument(url);      
        renderer.setDocument(new File("sample.html"));
        renderer.layout();
        renderer.createPDF(os);   
        os.close();
    }
}

XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='4.0' encoding='ISO-8859-1' indent='yes' /> 
  <xsl:template match="/">    
  <html>
  <body>
  <h2>FAP - Report from FIO Banka</h2>
    <table>
      <tr style="background-color:#9acd32;">
        <th align="left">ID pohybu</th>
        <th align="left">Datum</th>
        <th align="left">Objem</th>
        <th align="left">Mena</th>
        <th align="left">Protiucet</th>
        <th align="left">Kod banky</th>
        <th align="left">Nazev banky</th>
        <th align="left">KS</th>
        <th align="left">Uzivatel. identifikace</th>
        <th align="left">Typ</th>
        <th align="left">Provedl</th>
        <th align="left">Komentar</th>
        <th align="left">ID Pokynu</th> 
      </tr>
      <xsl:for-each select="AccountStatement/TransactionList/Transaction">
      <tr>
        <td><xsl:value-of select="column_22"/></td>
        <td><xsl:value-of select="column_0"/></td>
        <td><xsl:value-of select="column_1"/></td>
        <td><xsl:value-of select="column_14"/></td>
        <td><xsl:value-of select="column_2"/></td>
        <td><xsl:value-of select="column_3"/></td>
        <td><xsl:value-of select="column_12"/></td>
        <td><xsl:value-of select="column_4"/></td>
        <td><xsl:value-of select="column_7"/></td>
        <td><xsl:value-of select="column_8"/></td>
        <td><xsl:value-of select="column_9"/></td>
        <td><xsl:value-of select="column_25"/></td>
        <td><xsl:value-of select="column_17"/></td>
      </tr>
      </xsl:for-each>
    </table>
    <p>Vygenerovano </p> 
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

The problem may also be that the XML/HTML contains characters like "ščřžýáíéě" ...

HTML: http://pastebin.com/qSPUpV1u


Solution

  • First, I would not have gone from XML to HTML to PDF when you could have just as easily changed that XSL to product XSL FO. That said, here's a fiddle showing you your HTML can go to PDF just fine (and actually uses your HTML and converts it back to XSL FO with css styling intact).

    http://jsfiddle.net/m35q1d9w/

        function printMe() {
        xepOnline.Formatter.Format('print_me',
        {pageWidth:"11in", pageHeight:"8in"});
    

    Push the print button. It prints the div whose ID is 'print_me' which surrounds your table and heading.

    You should take more time to clean up the HTML. I added thead element to at least give you repeating headers at page breaks. And you could add css to that table to vastly improve the appearance.

    This uses css-to-pdf on GitHub which supports the language you desire and also your use of entities like iacute.

    Result PDF