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?
Is your virtual machine online? It may be possible that the renderer/parser tries to load the linked resources like the
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):
This basically sets up a DocumentBuilder and then parses my document (which is in String format and represented by the variable html)