如何使用pdfbox生成可下载的PDF文件(损坏的PDF)?

16

如何在链接中使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”,因为它不是受支持的文件类型或文件已经损坏。


关于“更新2”,这可能是这个错误:http://issues.apache.org/jira/browse/PDFBOX-2026。它将在1.8.5中修复。或者下载快照。 - Tilman Hausherr
你好@Night。 我正在尝试实现与您所完成的类似的功能。 您能否将响应对象声明放在您的Servlet内部?或者可能发布整个代码? - Erick
2个回答

7

为了让浏览器下载文件,您需要设置适当的HTTP头。

response.addHeader("Content-Type", "application/force-download")
response.addHeader("Content-Disposition", "attachment; filename=\"yourFile.pdf\"")

我在我的问题中添加了示例代码,也添加了头文件,但是当尝试打开PDF文件时,出现以下错误:Adobe Reader无法打开“yourfile.pdf”,因为它不是受支持的文件类型或文件已损坏。 - Night Elve
“yourfile.pdf” 应该是您正在生成的文件名。我只包含它作为示例名称。尝试删除反斜杠和引号。 - jloper3
response.addHeader("Content-Disposition", "attachment; filename=yourFile.pdf") 响应.添加头("内容-分配", "附件; 文件名=yourFile.pdf") - jloper3
我仍然在努力让它工作,我的意思是感谢你和Ben Brunk的回复,现在我能够生成可下载的文件了,但似乎它是损坏的,也许问题在于我如何处理保存或响应中的输出流。 - Night Elve

4
我有一段时间没有做这个了,所以请耐心等待,但是你需要做的是,不要通过流将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类型将响应中的baos写成PDF格式。

我根据您的回复更新了我的问题,并提供了修改后的代码版本。 - Night Elve

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接