0

I am transforming some HTML with JSoup. After the final HTML is produced I want to render it into pdf file. For that I am using Flying saucer libraries.

Html example I am trying to render:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
<title></title> 
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
<meta name="viewport" content="width=device-width, initial-scale=1" /> 
...

Method for rendering the html to pdf:

private void createPdfFile(String html, String fileName) throws IOException, DocumentException {
    ITextRenderer renderer = new ITextRenderer();

    renderer.setDocumentFromString(html);
    renderer.layout();

    FileOutputStream fos = new FileOutputStream(fileName);
    renderer.createPDF( fos );
    fos.close();
    System.out.println( "File 2: '" + fileName + "' created." );
    }

Error log:

org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 64; The system identifier must begin with either a single or double quote character.
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
    at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:75)
    at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:157)
    at main.Main.createPdfFile(Main.java:106)
    at main.Main.getEmailAsset(Main.java:97)
    at main.ServletMain.doGet(ServletMain.java:52)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 64; The system identifier must begin with either a single or double quote character.
    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)
    ... 27 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 64; The system identifier must begin with either a single or double quote character.
    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)
    ... 30 more

Any idea why is that happening?

1
  • remove the first line from your html and try again please. Commented May 13, 2015 at 8:29

2 Answers 2

3

You need to wrap your DTD URI in quotes - you're currently unbalanced:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

In your existing code, you're missing the " before http://www.w3...

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but how is it possible that happened? Is there a way to fix it with JSoup so if there are other errors like that it would be checked? Probably not, but worth asking.
No idea, I've never used it.
I am having similar issue with second line. Would you mind helping me? I will be happy to vote up. Error: lineNumber: 2; columnNumber: 15; Element type "html" must be followed by either attribute specifications, ">" or "/>"
Try running the doc through the w3 validator and see what you get.
I found the issue, for some reason the Jsoup is adding additional double quotes right after equals sign.
|
0

check you quotation marks in the tag attributes. I didn't have a " mark and that was causing a similar error for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.