HTML to PDF Conversion - "Can't load the XML resource" Error

srijan picture srijan · Feb 23, 2012 · Viewed 13.2k times · Source

I'm trying to convert strict XHTML to PDF using flying saucer and iText. I've validated XHTML and path of input and output file are precise. I have no goddamn clue why this is throwing an exception on renderer.setDocument("file:/c:/example/First.html") line.

My Class:

package flyingsaucerpdf;
    import java.io.*;
    import org.xhtmlrenderer.pdf.ITextRenderer;
    import com.lowagie.text.DocumentException;

    public class FirstDoc {
     public static void main(String[] args) throws IOException, DocumentException
         {
                 String outputFile = "results/firstdoc.pdf";
                 OutputStream os = new FileOutputStream(outputFile);
                 ITextRenderer renderer = new ITextRenderer();
             try
             {
                 renderer.setDocument("file:/c:/example/First.html");
             }
             catch( Exception e )
             {
                 System.out.println("Me not create file. Error:"+e.getMessage());
             }
             renderer.layout();
             renderer.createPDF(os);

             os.close();
         }

    }

My Exception:

ERROR: ''Me not create file. Error:Can't load the XML resource (using TRaX transformer). java.lang.NullPointerException

Exception in thread "main" java.lang.NullPointerException at org.xhtmlrenderer.layout.BoxBuilder.createRootBox(BoxBuilder.java:81) at org.xhtmlrenderer.pdf.ITextRenderer.layout(ITextRenderer.java:152) at flyingsaucerpdf.FirstDoc.main(FirstDoc.java:31)

My 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</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>

Any help?

Answer

mana picture mana · May 22, 2012

Is your virtual machine online? It may be possible that the renderer/parser tries to load the linked resources like the

http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd

that are needed to ensure that the xml (xhtml) you provide is correct.

In a Servlet I do the following that seems to work (some online resources are made available within my own filesystem because the server has no internet connection):

final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() {
  @Override
  public InputSource resolveEntity(String publicId, String systemId)
      throws SAXException, IOException {
    if (systemId.contains("xhtml1-transitional.dtd")) {
      return new InputSource(new FileReader(realPath + "/WEB-INF/dtd/xhtml1-transitional.dtd"));
    } else if (systemId.contains("xhtml-lat1.ent")) {
      return new InputSource(new FileReader(realPath + "/WEB-INF/dtd/xhtml-lat1.ent"));
    } else if (systemId.contains("xhtml-symbol.ent")) {
      return new InputSource(new FileReader(realPath + "/WEB-INF/dtd/xhtml-symbol.ent"));
    } else if (systemId.contains("xhtml-special.ent")) {
      return new InputSource(new FileReader(realPath + "/WEB-INF/dtd/xhtml-special.ent"));
    } else {
      return null;
    }
  }
});
final ByteArrayInputStream inputStream = new ByteArrayInputStream(html.getBytes("UTF-8"));
final Document doc = builder.parse(inputStream);
inputStream.close();
final ITextRenderer renderer = new ITextRenderer(26f * 4f / 3f, 26);
renderer.setDocument(doc, request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort());
renderer.layout();

This basically sets up a DocumentBuilder and then parses my document (which is in String format and represented by the variable html)