如何使用iText在PDF中插入图像并下载到客户端计算机? [英] How to insert Image in PDF using iText and download to client machine?

查看:200
本文介绍了如何使用iText在PDF中插入图像并下载到客户端计算机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jdbc从数据库中获取数据然后使用iText我创建了一个可以在客户端计算机上下载的PDF文件。该应用程序以html / jsp编码,并在Apache Tomcat上运行。

I'm using jdbc to fetch data from database and then using iText I create a PDF file which can be downloaded on client machine. The application is coded in html/jsp and runs on Apache Tomcat.

我使用 response.getOutputStream 来创建立即输出PDF文件。

I use the response.getOutputStream to create an output PDF file immediately.

问题是现在,我无法在此文档中插入图像,因为它给了我并且错误

The problem is that now, I cannot insert an image in this document as it gives me and error that


此响应已调用getOutputStream()

getOutputStream() has already been called for this response

我明白我在插入图像时再次调用 Outputstream 因此错误

I understand that I'm calling Outputstream again while inserting the image and therefore the error

如何在图像中插入图像文档并仍然生成一个动态PDF文件,可以由客户端机器下载?

How can I insert an image in the document and still generate a dynamic PDF file which can be downloaded by client machine?

相关代码:

response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=\"LicenseInfo.pdf\""); // Code 1
Document document = new Document();

PdfWriter.getInstance(document, response.getOutputStream()); // Code 2

Image image = Image.getInstance("logo.jpg");

document.open();

document.add(image);


推荐答案

对不起,你没有露面任何相关的代码,因为您复制/粘贴的代码不对您提到的例外负责。

I'm sorry, but you aren't showing any relevant code, as the code you copy/pasted isn't responsible for the exception you mention.

相关部分是您正在使用JSP,而您没有阅读我的书第9章中列出的有关JSP的重要警告。

The relevant part is that you're using JSP, and that you didn't read the important warnings concerning JSP listed in chapter 9 of my book.

当你编写JSP时,你可能喜欢空格和缩进,例如:

When you write JSP, you probably like white space and indentation, for instance:

<% //a line of code %>
<%
   // some more code
%>
<% // another line of code %>
<%
   response.getOutputStream();
%>

这将始终导致异常getOutputStream()已被调用无论你是否使用iText,这个回复。您在JSP脚本中引入第一个空白字符的那一刻就调用了 getOutputStream()方法。

This will always cause the exception "getOutputStream() has already been called for this response" regardless if you're using iText or not. The getOutputStream() method was called the moment you introduced your first white space character in your JSP script.

To解决此问题,您需要删除所有空格:

To fix this, you need to remove all white space:

<% //a line of code %><%
   // some more code
%><% // another line of code %><%
   response.getOutputStream();
%>

<%%> 标记。如更好的JSP手册中所述,您不应使用JSP来创建二进制文件。为什么不?因为JSP在二进制文件中的任意位置引入了空格字符。这会导致文件损坏。改为使用Servlets!

Not a single character is accepted outside the <% and %> markers. As explained in the better JSP manuals, you shouldn't use JSP to create binary files. Why not? Because JSP introduces white space characters at arbitrary places in your binary file. That results in corrupt files. Use Servlets instead!

这篇关于如何使用iText在PDF中插入图像并下载到客户端计算机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆