本文介绍了如何使用 pdfbox(损坏的 PDF)生成可下载的 PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使 PDF 文件可在链接中下载?

我正在使用 JSF 构建 Web 应用程序,当用户单击另存为 PDF"链接时,应该可以下载 PDF.

到目前为止,我有一个生成 PDF 文件的工作代码,但该文件保存在我的桌面上,我想要做的是当用户单击链接时,pdf 文件应该是可下载的,而不是被存储在应用程序中.

更新 3:感谢你们的帮助,我根据你们的建议修改了我的代码并且它正在工作.

更新 2:我收到以下错误:Adoble Reader 无法打开yourfile.pdf",因为文件类型不受支持或文件已损坏

更新 1:我正在添加我当前的代码以及您指出的更改,但是我仍在努力完成这项工作

这是我生成 PDF 的方法:

public ByteArrayOutputStream createPDF() 抛出 IOException, COSVisitorException {PDDocument 文件;PDPage页面;PDFont字体;PDPageContentStream contentStream;PDJpeg 前端;PDJpeg 回;InputStream inputFront;InputStream inputBack;ByteArrayOutputStream output = new ByteArrayOutputStream();//创建文档文档 = 新的 PDDocument();//创建页面for(int i=0; i

这是我的 servlet,它调用前面的代码并生成输出并设置标头:

尝试{ByteArrayOutputStream output = new ByteArrayOutputStream();输出 = createPDF();response.addHeader("Content-Type", "应用程序/强制下载");response.addHeader("Content-Disposition", "attachment; filename=\"yourFile.pdf\"");response.getOutputStream().write(output.toByteArray());} 捕捉(异常前){ex.printStackTrace();}

我不确定我错过了什么,因为当我尝试打开 PDF 时出现错误:Adoble Reader 无法打开yourfile.pdf",因为文件类型不受支持或因为文件已被损坏

解决方案

您需要设置正确的 http 标头,以便告诉浏览器下载文件.

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

How do I make a PDF file downloadable in a link?

I'm building a web application using JSF, when the user clicks in a "Save as PDF" link a PDF should be available to be downloaded.

So far I've a working code that generates the PDF file, but the file is saved on my desktop and what I want to do is that when the user clicks on the link the pdf file should be downloadable instead of being stored in the app.

UPDATE 3:Thank you for your help guys, I modifed my code with your suggestions and it's working.

UPDATE 2:I'm getting the following error: Adoble Reader could not open "yourfile.pdf" because is either not a supported file type or because the file has been damaged

UPDATE 1:I'm adding my current code with the changes you have pointed me out, however I'm still struggling to make this work

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;
}
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();
    }

I'm not sure what I'm missing since when I try to open the PDF I got the error: Adoble Reader could not open "yourfile.pdf" because is either not a supported file type or because the file has been damaged

解决方案

You need to set the proper http headers in order to tell the browser to download the file.

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

这篇关于如何使用 pdfbox(损坏的 PDF)生成可下载的 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:20