Convert HTML to PDF - Error loading XML resource

I am trying to convert strict XHTML to PDF using a flying saucer and iText. I checked XHTML and the file input and output path is accurate. I have no idea why this throws an exception to the string renderer.setDocument ("file: / c: /example/First.html").

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: "I am not creating a file. Error: Cannot load the XML resource (using the TRaX transformer). Java.lang.NullPointerException

An exception was thrown in the main stream java.lang.NullPointerException on org.xhtmlrenderer.layout.BoxBuilder.createRootBox (BoxBuilder.java:81) on org.xhtmlrenderer.pdf.ITextRenderer.layout (ITextRenderer.javairstcerdfdf). 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?

+4
source share
1 answer

Is your virtual machine online? Maybe the handler / parser is trying to load related resources like

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

which are necessary to ensure the correctness of the xml (xhtml) you provided.

In the servlet, I do the following, which seems to work (some online resources are available in my own file system, because the server does not have an 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 the DocumentBuilder and then parses my document (which is in String format and represented by the html variable)

+2
source

Source: https://habr.com/ru/post/1398055/


All Articles