我在问题之前问过" Append PDF at the available space of another PDF file"我已经成功制作了一个包含本地iText和JFreeChart的PDF文件。
我使用教程" Spring Web MVC with PDF View Example (using iText 5.x)"将此设置合并到我的Spring MVC应用程序。
我理解使用Spring AbstractView
并依次实现为
@Override
protected void buildPdfDocument(Map<String, Object> map, Document document,
PdfWriter pdfWriter, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// do iText and JFreeCharts
}
并且JFreeChart实例需要将现有PDF文件插入其中,如&#34; Creating an iText pdf with embedded JFreeChart&#34;中所示:
// get the direct pdf content
PdfContentByte dc = docWriter.getDirectContent();
// get a pdf template from the direct content
PdfTemplate tp = dc.createTemplate(width, height);
// create an AWT renderer from the pdf template
Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper() );
Rectangle2D r2D = new Rectangle2D.Double(0,0, width,height);
chart.draw(g2,r2D,null);
g2.dispose();
// add the rendered pdf template to the direct content
// you will have to play around with this because the chart is absolutely positioned.
// 38 is just a typical left margin
// docWriter.getVerticalPosition(true) will approximate the position that the content above the chart ended
dc.addTemplate(tp, 38, docWriter.getVerticalPosition(true)-height);
我现在的问题是每当我调用扩展AbstractView
的方法时(参考第二个链接),在插入JFreeChart之前显示PDF而不是生成图表的那个。我还没有遇到任何错误,但我想知道是否有这种行为的解决方案。
我只希望用户使用JFreeChart查看PDF,就像Spring MVC中的普通PDF实现一样。
正如您所看到的,我只是按照上面的教程并将它们组合在一起。它们在一个单独的项目中完美地工作,但是当我与Spring合并时,JFreeChart没有显示,特别是显示了错误的PDF文件。
我知道我可以使用JFreeChart调用生成的PDF,并通过链接到正确的文件来拦截查看,但我对此设置保持希望,因为这将是使用这三种技术的便捷方式完全没有服务器中文件I / O的麻烦(我遇到了麻烦)。
以下是来自&#34; Spring Web MVC with PDF View Example (using iText 5.x)&#34;的课程AbstractITextPdfView
的摘录。我用的。
有人可以告诉我如何将PDF文件打开到带有图表的文件,即在buildPdfDocument()
内吗?
@Override
protected void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// IE workaround: write into byte array first.
ByteArrayOutputStream baos = createTemporaryOutputStream();
// Apply preferences and build metadata.
Document document = newDocument();
PdfWriter writer = newWriter(document, baos);
prepareWriter(model, writer, request);
buildPdfMetadata(model, document, request);
// Build PDF document.
document.open();
buildPdfDocument(model, document, writer, request, response);
document.close();
// Flush to HTTP response.
writeToResponse(response, baos);
}
我在ByteArrayOutputStream
&#34;附加&#34;之后保存到一个文件中拦截了JFreeChart
。本身是iText
PDF。
@Override
protected void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// IE workaround: write into byte array first.
ByteArrayOutputStream baos = createTemporaryOutputStream();
// Apply preferences and build metadata.
Document document = newDocument();
PdfWriter pdfWriter = newWriter(document, baos);
prepareWriter(model, pdfWriter, request);
buildPdfMetadata(model, document, request);
// Build PDF document.
document.open();
buildPdfDocument(model, document, pdfWriter, request, response);
document.close();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("D:/MUST-HAVE-CHART.pdf"));
baos.writeTo(fos);
} catch (IOException ioe) {
// Handle exception here
ioe.printStackTrace();
} finally {
fos.close();
}
System.out.println(baos.toString());
// Flush to HTTP response.
writeToResponse(response, baos);
}
发生的事情是MUST-HAVE-CHART.PDF
没有图表,与创建后的Spring MVC提供的图表相同。然而,带有图表的那个被保存在其他地方,如代码中所示:
@Override
protected void buildPdfDocument(Map<String, Object> map, Document document,
PdfWriter pdfWriter, HttpServletRequest request,
HttpServletResponse response) throws Exception {
logger.info("Start of PDF creation");
Domain domain = (Domain) map.get("domain");
ServletContext servletContext = request.getSession()
.getServletContext();
File servletTempDir = (File) servletContext
.getAttribute("javax.servlet.context.tempdir");
if (servletTempDir == null)
throw new IllegalStateException(
"Container does not provide a temp dir");
File targetFile = new File(servletTempDir, NAME_FILE);
pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(
targetFile));
logger.info("PDF file path: " + targetFile.getAbsolutePath().toString());
document.open();
PdfBuilder.assemble(document, pdfWriter, domain);
document.close();
pdfWriter.close();
logger.info("End of PDF creation");
map.put("file", targetFile.getAbsolutePath().toString());
}
正确的保存在javax.servlet.context.tempdir
或C:/tomcat/work/Catalina/localhost/my_spring_app/pdf_file.pdf
中。
答案 0 :(得分:0)
我遇到了同样的问题。 我使用以下代码解决了它,现在正在下载文件,而不是渲染一个新的pfd-view页面(我更喜欢这个)。
protected final void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// IE workaround: write into byte array first.
ByteArrayOutputStream baos = createTemporaryOutputStream();
// Apply preferences and build metadata.
Document document = newDocument();
PdfWriter writer = newWriter(document, baos);
prepareWriter(model, writer, request);
buildPdfMetadata(model, document, request);
// Build PDF document.
document.open();
buildPdfDocument(model, document, writer, request, response);
document.close();
//Custom code start
String filestorePath = "/var/www/xxx/xxxxx/";
String downloadfileNm = "DeliveryNote.pdf";
String filePath = filestorePath+downloadfileNm;
URL url = new URL("file:"+filePath);
File file = new File(url.toURI());
if (file.exists()) {
response.setContentLength(new Long(file.length()).intValue());
response.setHeader("Content-Disposition", "attachment; filename="+downloadfileNm);
FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());
}
//Custom code end
// Flush to HTTP response.
//writeToResponse(response, baos);
}