如何在链接中下载PDF文件?如何使用pdfbox生成可下载PDF(损坏的PDF)?
我使用JSF构建Web应用程序,当在“保存为PDF”用户点击链接PDF应该是可供下载。
到目前为止,我有一个生成PDF文件的工作代码,但该文件保存在我的桌面上,我想要做的是,当用户点击链接时,PDF文件应该可下载而不是存储在应用程序中。
更新3: 谢谢您的帮助球员,我体改我的代码以您的建议和它的工作。
更新2: 我收到以下错误:Adoble阅读器无法打开“yourfile.pdf”,因为要么是不支持的文件类型,或者因为文件已损坏
更新1: 我加入我当前的代码与你指着我让的变化,但是我仍然在努力使这项工作
This is my method that generated the PDF:
public ByteArrayOutputStream createPDF() throws IOException, COSVisitorException {
PDDocument document;
PDPage page;
PDFont font;
PDPageContentStream contentStream;
PDJpeg front;
PDJpeg back;
InputStream inputFront;
InputStream inputBack;
ByteArrayOutputStream output = new ByteArrayOutputStream();
// Creating Document
document = new PDDocument();
// Creating Pages
for(int i=0; i<2; i++) {
page = new PDPage();
// Adding page to document
document.addPage(page);
// Adding FONT to document
font = PDType1Font.HELVETICA;
// Retrieve Image to be added to the PDF
inputFront = new FileInputStream(new File("D:/Media/imageFront.jpg"));
inputBack = new FileInputStream(new File("D:/Media/imageBack.jpg"));
BufferedImage buffFront = ImageIO.read(inputFront);
BufferedImage resizedFront = Scalr.resize(buffFront, 460);
BufferedImage buffBack = ImageIO.read(inputBack);
BufferedImage resizedBack = Scalr.resize(buffBack, 460);
front = new PDJpeg(document, resizedFront);
back = new PDJpeg(document, resizedBack);
// Next we start a new content stream which will "hold" the to be created content.
contentStream = new PDPageContentStream(document, page);
// Let's define the content stream
contentStream.beginText();
contentStream.setFont(font, 8);
contentStream.moveTextPositionByAmount(10, 770);
contentStream.drawString("Amount: $1.00");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(font, 8);
contentStream.moveTextPositionByAmount(200, 770);
contentStream.drawString("Sequence Number: 123456789");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(font, 8);
contentStream.moveTextPositionByAmount(10, 760);
contentStream.drawString("Account: 123456789");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(font, 8);
contentStream.moveTextPositionByAmount(200, 760);
contentStream.drawString("Captura Date: 04/25/2011");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(font, 8);
contentStream.moveTextPositionByAmount(10, 750);
contentStream.drawString("Bank Number: 123456789");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(font, 8);
contentStream.moveTextPositionByAmount(200, 750);
contentStream.drawString("Check Number: 123456789");
contentStream.endText();
// Let's close the content stream
contentStream.close();
}
// Finally Let's save the PDF
document.save(output);
document.close();
return output;
}
This is my servlet that call the previous code and generates the output and set the header:
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
output = createPDF();
response.addHeader("Content-Type", "application/force-download");
response.addHeader("Content-Disposition", "attachment; filename=\"yourFile.pdf\"");
response.getOutputStream().write(output.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
}
我不知道,因为当我试图打开我得到了错误的PDF什么,我缺少的:Adoble阅读器无法打开“yourfile.pdf”,因为无论是不是支持的文件类型或因为文件已损坏
重新“更新2”,这可能是这个bug:http://issues.apache.org/jira/browse/PDFBOX-2026。它将在1.8.5中修复。或者下载快照。 – 2014-04-16 10:46:05
你好@晚上。我试图实现类似于你所完成的事情。您能否将响应对象声明放入您的servlet中?或者可能发布整个代码? – Erick 2016-05-24 01:46:01