如何在链接中使PDF文件可下载?
我正在使用JSF构建Web应用程序。当用户点击“另存为PDF”链接时,应该可以下载PDF文件。
到目前为止,我有一个能够生成PDF文件的工作代码,但是该文件保存在我的桌面上,我想要的是当用户点击链接时,PDF文件应该可下载而不是存储在应用程序中。
更新3:感谢大家的帮助,我已经根据你们的建议修改了我的代码,现在它能够正常工作了。
更新2:我遇到了以下错误:Adobe Reader无法打开“yourfile.pdf”,因为要么不支持该文件类型,要么该文件已损坏
更新1:我正在添加我当前的代码,包括你们指出的更改,但是我仍然在努力让它工作
这是生成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;
}
这是我的Servlet,它调用了之前的代码并生成输出,并设置了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时,我收到了错误提示:Adobe Reader无法打开“yourfile.pdf”,因为它不是受支持的文件类型或文件已经损坏。