我有一个遗留应用程序,它在 java 1.8.0_171 64 位上使用 itext 5.5.6,该应用程序在 Websphere 应用程序服务器 ND 9.0.5.13 和 Windows server 216 标准版本 1607 64 位 上运行,我正在使用它将 html 转换为 pdf 当最终用户输入一个非常大的单词(直到 30000 个字符)时,我遇到一个问题,例如:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa啊啊啊aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa....直到3万字符
它使 Itext 挂在以下行并且不抛出任何异常:
document.add(myPdfPTable)
此行为不会发生在 tomcat 服务器上
下面是我的代码:
public Document generatePDF(String fontPath,String html) throws Exception {
com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4, 20, 20, 100, 50);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pdfWriter = PdfWriter.getInstance(document, baos);
pdfWriter.setPageEvent(ppaHeaderFooter);
pdfWriter.setViewerPreferences(PdfWriter.PageModeUseOC);
pdfWriter.setPdfVersion(PdfWriter.VERSION_1_5);
document.open();
document.addAuthor(authorName);
document.addCreator(authorName);
document.addSubject(filename);
document.addCreationDate();
document.addTitle(filename);
Font arabicFont = FontFactory.getFont(fontPath, BaseFont.IDENTITY_H, 14);
FontFactory.register(fontPath);
Font bold = FontFactory.getFont(fontPath, BaseFont.IDENTITY_H, 14f, Font.BOLD);
Font regularfont = FontFactory.getFont(fontPath, BaseFont.IDENTITY_H, 10);
List<Element> pdfElements = getElementsFromHtml(html, fontPath);
PdfPTable contentTable = new PdfPTable(1);
contentTable.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
contentTable.setSplitLate(false);
contentTable.setWidthPercentage(90);
contentTable.setExtendLastRow(true);
for (Element element : pdfElements) {
PdfPCell cell = new PdfPCell();
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setBorder(Rectangle.NO_BORDER);
cell.setArabicOptions(ColumnText.DIGITS_EN2AN);
cell.addElement(element);
contentTable.addCell(cell);
}
document.add(contentTable);
return document;
}
public ArrayList<Element> getElementsFromHtml(final String html, String fontPath) throws IOException {
final ElementList elements = new ElementList();
StringBuilder stylefile = new StringBuilder();
stylefile.append("body {font-size: 14pt;font-family:MyCustomFont;line-height: 1.5;dir:rtl}");
stylefile.append("table{padding: 0px;border-spacing: 0px;font-size: 14pt;}");
stylefile.append("td{height:30px;}");
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(stylefile.toString().getBytes()));
cssResolver.addCss(cssFile);
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register(fontPath, "MyCustomFont");
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
final ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
final HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
final CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
final XMLWorker worker = new XMLWorker(cssPipeline, true);
final XMLParser p = new XMLParser(worker, Charset.forName("UTF-8"));
final String resolvedHtml = "<body>" + html + "</body>";
p.parse(new ByteArrayInputStream(resolvedHtml.getBytes(Charset.forName("UTF-8"))), Charset.forName("UTF-8"));
return new ArrayList<Element>(elements);
}
更新1 在 websphere 中启用挂起线程检测后,我能够找到挂起线程的原因:
ThreadMonitor W WSVR0605W: Thread "WebContainer : 4" (00003bed) has been active for 672628 milliseconds and may be hung. There is/are 1 thread(s) in total in the server that may be hung.
at com.ibm.tivoli.itcam.gdc.data.GDCInstanceDataManager.resetProvisional(GDCInstanceDataManager.java:489)
at com.ibm.tivoli.itcam.gdc.GDCMethodProbePoint.customProbeEndEvent(GDCMethodProbePoint.java:1691)
at com.ibm.tivoli.itcam.toolkit.ai.boot.callbacks.generic.CustomProbeCallback.customProbeAfterCallbackInstance(CustomProbeCallback.java:450)
at com.ibm.tivoli.itcam.toolkit.ai.boot.callbacks.generic.CustomProbeCallback.customProbeAfterCallback(CustomProbeCallback.java:409)
at com.itextpdf.text.pdf.PdfFont.width(PdfFont.java:138)
at com.itextpdf.text.pdf.PdfChunk.getCharWidth(PdfChunk.java:961)
at com.itextpdf.text.pdf.BidiLine.processLine(BidiLine.java:383)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1068)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:990)
at com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1523)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:996)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:990)
at com.itextpdf.text.pdf.PdfPCell.getMaxHeight(PdfPCell.java:1044)
at com.itextpdf.text.pdf.PdfPTable.getFittingRows(PdfPTable.java:2141)
at com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1753)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:996)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:990)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:978)
at com.itextpdf.text.pdf.PdfDocument.addPTable(PdfDocument.java:2673)
at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:750)
at com.itextpdf.text.Document.add(Document.java:278)
UPDATE2:发现终于创建了文件,但创建需要大约 15 分钟,而文件大小只有 350 KB,但包含 280 个页面,而在 tomcat 9 java 8 上的相同请求需要 30 秒才能完成所以这似乎是一个 Websphere 问题。