我们使用漂亮的库iText作为我客户的一个项目,从表示html页面的字符串生成pdf。 iText版本是5.5.10。
以下代码适用于在Windows上运行的开发环境和服务器,但它不适用于在iSeries上运行的客户服务器。
public class GeneratePDFCmdImpl extends ControllerCommandImpl implements GeneratePDFCmd { private String charsetStr = null; private Charset charset = null; private BaseFont bf = null; private String destFile = null; private String destFilename = null; private String srcContent = null; private String docName = null; public void setDocName(String docname) { this.docName = docname; } public void setSrcContent(String srcContent) { this.srcContent = srcContent; } private void prepareDefaultsAndSettings() { /* srcContent may be more complex html but even this simple one is not working */ srcContent = "<html><head></head><body>This is just a test</body></html>"; docName = "mypdf"; charsetStr = "UTF-8"; destFilename = docName+".pdf"; Date timestamp = new Date(); /* destFile = "/" is just for the sample. In my real project, the value is a folder where my app has full rights */ destFile = "/" + destFilename; charset = Charset.forName(charsetStr); FontFactory.register("/fonts/arial.ttf","Arial"); bf = FontFactory.getFont("Arial").getBaseFont(); } @Override public void performExecute() throws ECException { super.performExecute(); Document document = null; OutputStream os = null; prepareDefaultsAndSettings(); try { InputStream srcInputStream; srcInputStream = new ByteArrayInputStream(srcContent.getBytes(charset)); document = new Document(PageSize.A4, 20, 20, 75, 80); FileOutputStream destOutput = new FileOutputStream(destFile); PdfWriter writer = PdfWriter.getInstance(document,destOutput); writer.setPageEvent( new HeaderFooterPageEvent(bf)); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, srcInputStream, charset); document.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } finally { if(document != null) { document.close(); } document = null; try { if (os != null) { os.close(); } } catch(IOException e) { e.printStackTrace(); } os = null; } } private class HeaderFooterPageEvent extends PdfPageEventHelper { PdfContentByte cb; PdfTemplate template; BaseFont bf; Font f; float fs; public HeaderFooterPageEvent(BaseFont _bf) { super(); bf = _bf; f = new Font(bf); } @Override public void onOpenDocument(PdfWriter writer, Document document) { cb = writer.getDirectContent(); template = cb.createTemplate(50, 50); } @Override public void onEndPage(PdfWriter writer, Document document) { Date dat = new Date(); ColumnText ct = new ColumnText(writer.getDirectContent()); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm"); ct.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(sdf.format(dat) ), 100, 30, 0); String text = "Page " +writer.getPageNumber() + " to "; float len = bf.getWidthPoint(text, 12); cb.beginText(); cb.setFontAndSize(bf, 12); cb.setTextMatrix(450, 30); cb.showText(text); cb.endText(); cb.addTemplate(template, 450 + len, 30); } @Override public void onCloseDocument(PdfWriter writer, Document document) { template.beginText(); template.setFontAndSize(bf, 12); template.showText(String.valueOf(writer.getPageNumber())); template.endText(); } } }
在iSeries上执行时,我们收到错误消息
com.ibm.commerce.command.ECCommandTarget executeCommand CMN0420E: The following command exception has occurred during processing: "ExceptionConverter: java.io.IOException: The document has no pages.". ExceptionConverter: java.io.IOException: The document has no pages. at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:112) at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1256) at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:900) at com.itextpdf.text.Document.close(Document.java:415) at be.ourcustomer.package.GeneratePDFCmdImpl.performExecute(GeneratePDFCmdImpl.java:107)
我对自己做错了什么并不太了解。任何帮助将不胜感激