我正在使用<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.0.4</version>
</dependency>
将我的HTML字符串转换为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();
}
代码在本地工作正常。但是在生产服务器上它抛出连接超时异常。 这是完整的堆栈跟踪
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)
代码中的第84行是Document doc = builder.parse(new ByteArrayInputStream(table.getBytes("UTF-8")));
起初我以为服务器可能无法获取外部CSS。所以我将html字符串保存为html文件,我可以在浏览器上看到该页面。这意味着服务器可以访问CSS。
答案 0 :(得分:0)
你可能仍然对你的CSS是正确的,因为主html已被保存的事实并不意味着CSS得到了很好的保存(实际上,我们甚至不知道它是否试图保存它,你尝试过吗?通过像Paros这样的代理来完成你的工作,以便了解你尝试的每一次沟通?)
http://sourceforge.net/projects/paros/
顺便说一下,你的问题可能会有所不同。您是否在生产环境中支持企业代理?您的“飞碟pdf库”是否需要任何可能卡在防火墙中并导致超时的外部资源(任何.xsd或类似的东西)?确保您可以控制应用程序生成的所有通信,并找到问题所在。