java Struts2 在jsp中显示pdf文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11137362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Struts2 Display pdf file in jsp
提问by user1471970
My requirement is to create a dynamic report pdf file with some data from database which I'm doing it using iText. Now, I want to display this pdf file inline in the webpage alongwith menu,header, footer, etc.
我的要求是创建一个动态报告 pdf 文件,其中包含我使用 iText 执行的数据库中的一些数据。现在,我想在网页中内嵌显示此 pdf 文件以及菜单、页眉、页脚等。
So, If the user has some pdf viewer then this pdf should be displayed in user machine with print option to print that pdf.
所以,如果用户有一些 pdf 查看器,那么这个 pdf 应该显示在用户机器上,并带有打印选项来打印该 pdf。
回答by anupam
This is how I am doing it. You can call this action inside an iframe or in a regular jsp
这就是我的做法。您可以在 iframe 或常规 jsp 中调用此操作
public class GeneratePdf extends ActionSupport{
private InputStream inputStream;
public String execute(){
HttpServletResponse response = ServletActionContext.getResponse();
Document document = new Document();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
PdfWriter.getInstance(document, buffer);
document.open();
// do your thing
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
byte[] bytes = null;
bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
if(bytes!=null){
inputStream = new ByteArrayInputStream ( bytes );
}
return SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
}
In your struts.xml
在你的 struts.xml 中
<action name="GeneratePdf" class="com.xxx.action.GeneratePdf">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="test.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
回答by rvazquezglez
回答by Michael Santos
public ByteArrayInputStream generatePDF(List<Object> items) {
try {
List<InputStream> listInputStream = new ArrayList<InputStream>();
for (int i = 0; i < items.size(); i++) {
listInputStream.add(new ByteArrayInputStream(getBytes(items.get(i)));
}
HttpServletResponse response = ServletActionContext.getResponse();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, buffer);
document.open();
PdfContentByte cb = writer.getDirectContent();
for (InputStream inputStream : listInputStream) {
PdfReader reader = new PdfReader(inputStream);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
document.setPageSize(reader.getPageSize(i));
document.newPage();
PdfImportedPage page = writer.getImportedPage(reader, i);
cb.addTemplate(page, 0, 0);
}
}
document.close();
byte[] bytes = null;
bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
return new ByteArrayInputStream(bytes);
} catch(Exception e){
System.out.println(e);
}
}