0

I am using <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf-itext5</artifactId> <version>9.0.4</version> </dependency> to convert my HTML string to PDF.

    try {
        String table = getHtmlAsString();//returns html string which contains reference to external CSS
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(table.getBytes("UTF-8")));

        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(doc, null);

        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        renderer.layout();
        renderer.createPDF(byteArray);
        byte[] pdf = byteArray.toByteArray();
        byteArray.close();
        writeByteArrayToFile(pdf);
    } catch (Exception e) {
        e.printStackTrace();
    }

The code is working fine locally. But on production server Its throwing connection time out exception. Here is complete stacks trace

java.net.ConnectException: Connection timed out: connect at
 java.net.DualStackPlainSocketImpl.connect0(Native Method) at
 java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at
 java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at
 java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at
 java.net.AbstractPlainSocketImpl.connect(Unknown Source) at
 java.net.PlainSocketImpl.connect(Unknown Source) at
 java.net.SocksSocketImpl.connect(Unknown Source) at
 java.net.Socket.connect(Unknown Source) at
 java.net.Socket.connect(Unknown Source) at
 sun.net.NetworkClient.doConnect(Unknown Source) at
 sun.net.www.http.HttpClient.openServer(Unknown Source) at
 sun.net.www.http.HttpClient.openServer(Unknown Source) at
 sun.net.www.http.HttpClient.<init>(Unknown Source) at
 sun.net.www.http.HttpClient.New(Unknown Source) at
 sun.net.www.http.HttpClient.New(Unknown Source) at
 sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) at
 sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) at
 sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) at
 sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at
 org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source) at
 org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source) at
 org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source) at
 org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source) at
 org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source) at
 org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at
 org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at
 org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at
 org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at
 org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at
 org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) at
 javax.xml.parsers.DocumentBuilder.parse(Unknown Source) at
 com.mypackage.PDFProcessor.getPdf(PDFProcessor.java:84)

Line No.84 in code is Document doc = builder.parse(new ByteArrayInputStream(table.getBytes("UTF-8")));

At first I thought may be server is not able to fetch External CSS. So I saved html string as html file and I am able to see the page on browser. That means server is able to access the CSS.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Anurag Tripathi
  • 1,208
  • 1
  • 12
  • 31
  • I see `XMLDTDScannerImpl.setInputSource` in the stack trace. Could it be that some DTD is defined in an URL not accessible from the production server? – mkl Jul 01 '14 at 09:03

1 Answers1

0

You may still be right about your CSS, since the fact that the main html has been saved does not mean that the CSS is well saved (actually, we don't even know if it tries to save it, have you tried to do your work through a proxy like Paros in order to know every communication you attempt?)

http://sourceforge.net/projects/paros/

By the way, your problem may be different. Are you behind a corporate proxy in your production environment? Does your "flying saucer pdf library" need any external resource (any .xsd or something like that) that could be stuck in the firewall and cause that time-out? Make sure that you have under controll all the communication your application generates and you will find your problem.

Jorge_B
  • 9,712
  • 2
  • 17
  • 22