Karthikeyan
Karthikeyan

Reputation: 2011

The element type "META" must be terminated by the matching end-tag "</META>". while generating PDF from XML file using XSL

Am trying to convert XML to PDF document. While parsing XML with XSL for generating HTML for PDF creating. HTML doesnt contain </meta> closing tag, due to that am getting the below error

 Exception in thread "main" org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 3; The element type "META" must be terminated by the matching end-tag "</META>".

How i can include </meta> closing tag in HTML

Please find my java code to generate PDF from XML

public class XMLtoPDF {


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



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

Upvotes: 0

Views: 2155

Answers (1)

Michael Kay
Michael Kay

Reputation: 163549

The name org.xhtmlrenderer.util suggests to me that the library you are using (ITextRenderer) expects XHTML. You can get XHTML output from your XSLT transformation by

(a) changing it to use the XHTML output method instead of HTML

(b) changing it to use an XSLT 2.0 processor such as Saxon, because to use the XHTML output method, you need XSLT 2.0.

More specifically, change

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource("xsl_html_pagebreak_a.xslt"));

to

TransformerFactory tFactory = new net.sf.saxon.TransformerFactoryImpl();
Transformer transformer = tFactory.newTransformer(new StreamSource("xsl_html_pagebreak_a.xslt"));
transformer.setOutputProperty("method", "xhtml");

Alternatively you might find that changing the serialization method to "xml" will also work; in that case you don't need to change to XSLT 2.0.

Upvotes: 2

Related Questions