Java 如何使用pdfbox(损坏的PDF)生成可下载的PDF?
如何使PDF文件在链接中可下载 我正在使用JSF构建一个web应用程序,当用户单击“另存为PDF”链接时,应该可以下载PDF 到目前为止,我有一个生成PDF文件的工作代码,但该文件保存在我的桌面上,我想做的是,当用户单击链接时,PDF文件应该可以下载,而不是存储在应用程序中 更新3: 谢谢你们的帮助,伙计们,我用你们的建议修改了我的代码,它开始工作了 更新2: 我收到以下错误:Adoble Reader无法打开“yourfile.pdf”,因为它不是受支持的文件类型,或者因为文件已损坏 更新1:Java 如何使用pdfbox(损坏的PDF)生成可下载的PDF?,java,web,pdfbox,Java,Web,Pdfbox,如何使PDF文件在链接中可下载 我正在使用JSF构建一个web应用程序,当用户单击“另存为PDF”链接时,应该可以下载PDF 到目前为止,我有一个生成PDF文件的工作代码,但该文件保存在我的桌面上,我想做的是,当用户单击链接时,PDF文件应该可以下载,而不是存储在应用程序中 更新3: 谢谢你们的帮助,伙计们,我用你们的建议修改了我的代码,它开始工作了 更新2: 我收到以下错误:Adoble Reader无法打开“yourfile.pdf”,因为它不是受支持的文件类型,或者因为文件已损坏 更新1:
我正在添加我当前的代码和您指出的更改,但我仍在努力使其正常工作 这是我生成PDF的方法:
我不确定我错过了什么,因为当我尝试打开PDF时,我遇到了一个错误:Adoble Reader无法打开“yourfile.PDF”,因为它不是受支持的文件类型,或者因为文件已损坏我已经有一段时间没有这样做了,所以请耐心等待,但您所做的不是通过流将PDF保存到文件中,将流保存在内存中作为字节数组,然后当用户单击链接时,将MIME类型设置为PDF,然后将字节数组作为流打开,并作为响应返回。我很抱歉对细节有点模糊。我想我也使用了jpedal和iText来完成它 我无法向您展示所有代码,但这里有一些:
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
... // class, etc.
public ByteArrayOutputStream createOrderFormPdf(OrderFormDTO dto)
throws DocumentException {
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
Paragraph header = new Paragraph();
header.add(new Phrase(...));
OrderFormDtoPdfAdapter pdfAdapter = new OrderFormDtoPdfAdapter(dto);
header.add(pdfAdapter.getPdfHeaderTable());
document.add(header);
... // other code
Paragraph footer = new Paragraph();
footer.add(pdfAdapter.getPDFFooterTable());
document.add(footer);
Paragraph paragraph = new Paragraph();
PdfTableUtils.addEmptyLine(paragraph, 2);
document.add(paragraph);
document.add(new DottedLineSeparator());
document.close();
return baos;
}
然后,您可以使用正确的MIME类型在响应中以pdf格式写出BAO 您需要设置正确的http头,以便通知浏览器下载文件
response.addHeader("Content-Type", "application/force-download")
response.addHeader("Content-Disposition", "attachment; filename=\"yourFile.pdf\"")
根据您的回答,我用修改后的代码更新了我的问题。我在问题中添加了示例代码,也添加了标题,但在尝试打开PDF文件时出现以下错误:“Adoble Reader无法打开”yourfile.PDF“,因为它不是受支持的文件类型,或者文件已损坏。“yourfile.PDF”应该是您正在生成的文件名。我只是把它作为一个样本名称。尝试删除反斜杠和引号.response.addHeader(“Content Disposition”,“attachment;filename=yourFile.pdf”)我仍在努力工作,我的意思是,感谢您的回复和Ben Brunk回复,现在我能够生成可下载的文件,但它似乎已损坏,可能问题在于我如何处理响应中的保存或输出流。如果是“更新2”,可能是以下错误:。它将在1.8.5中固定。或者下载快照。你好@Night。我正试图实现一些类似于你所完成的事情。请将响应对象声明放在servlet中好吗?或者把全部代码都发出去?
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
... // class, etc.
public ByteArrayOutputStream createOrderFormPdf(OrderFormDTO dto)
throws DocumentException {
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
Paragraph header = new Paragraph();
header.add(new Phrase(...));
OrderFormDtoPdfAdapter pdfAdapter = new OrderFormDtoPdfAdapter(dto);
header.add(pdfAdapter.getPdfHeaderTable());
document.add(header);
... // other code
Paragraph footer = new Paragraph();
footer.add(pdfAdapter.getPDFFooterTable());
document.add(footer);
Paragraph paragraph = new Paragraph();
PdfTableUtils.addEmptyLine(paragraph, 2);
document.add(paragraph);
document.add(new DottedLineSeparator());
document.close();
return baos;
}
response.addHeader("Content-Type", "application/force-download")
response.addHeader("Content-Disposition", "attachment; filename=\"yourFile.pdf\"")