5

Please help me to find the reason for this exception ,am using following jars,

core-renderer.jar
itext-paulo-155.jar

firstdoc.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My First Document</title>
        <style type="text/css"> b { color: green; } </style>
    </head>
    <body>
        <p>
            <b>Greetings Earthlings!</b>
            We've come for your Java.
        </p>
    </body>
</html>

JAVA :

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;

public class Sample1 {
    public static void main(String[] args)  throws IOException, DocumentException {
                String inputFile = "C:/Users/Joseph.M/WorkPlace_Struts2/Fly-Sauccer/src/test/pdf/firstdoc.xhtml";
                String url = new File(inputFile).toURI().toURL().toString();
                String outputFile = "C:/Users/Joseph.M/WorkPlace_Struts2/Fly-Sauccer/src/test/pdf/firstdoc.pdf";
                OutputStream os = new FileOutputStream(outputFile);             
                ITextRenderer renderer = new ITextRenderer();
                renderer.setDocument(url);
                renderer.layout();
                renderer.createPDF(os);             
                os.close();
    }
}

Exception :

plumbing.render INFO:: Using CSS implementation from: org.xhtmlrenderer.context.StyleReference
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: Reference is not allowed in prolog.
ERROR:  'Reference is not allowed in prolog.'
plumbing.exception WARNING:: Unhandled exception. Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.
Exception in thread "main" org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
    at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71)
    at org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:118)
    at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:98)
    at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:102)
    at test.pdf.Sample1.main(Sample1.java:23)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:189)
    ... 5 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)

1 Answer 1

15

Use < and > instead of

&lt; and &gt;

You do not have to escape XML special characters here.

3
  • Can you elaborate on why you don't need to use the special characters? Apr 6, 2016 at 16:16
  • @EdgeCaseBerg I would expect the SAX parser to take care of proper XML instead of wanting escaped characters. But I don't really know what I'm saying :)
    – randombee
    Apr 21, 2016 at 10:55
  • @EdgeCaseBerg you only need to escape XML special characters when they are used literally inside data in XML elements. If they are used as part of XML syntax (delimiting tags, etc.) they need not be escaped. Apr 21, 2016 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.