简体   繁体   English

PDF文件未与内容一起下载

[英]Pdf file is not downloading with content

I am trying to download a PDF file with Struts2. 我正在尝试使用Struts2下载PDF文件。 For generating the PDF I am using PDFBox. 为了生成PDF,我使用PDFBox。

I am able to download the PDF file, but the problem is that its size is 0 bytes. 我可以下载PDF文件,但问题是它的大小为0字节。

Action Class 动作班

public class BarcodeAction extends ActionSupport {

    private InputStream inputStream;
    //getter and setter

    public String getPdf() {
        System.out.println("Get Pdf");
        PDDocument document = null;
        try {
            document = new PDDocument();
            PDPage page = new PDPage();
            document.addPage(page);
            PDFont headingFont = PDType1Font.TIMES_ROMAN;
            PDPageContentStream contentStream = 
                            new PDPageContentStream(document, page, false, true);
            contentStream.beginText();
            contentStream.setFont(headingFont, 26);
            contentStream.moveTextPositionByAmount(250, 700);
            contentStream.drawString("Hello World !");
            contentStream.endText();
            contentStream.close();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            document.save(bos);
            setInputStream(new ByteArrayInputStream(bos.toByteArray()));
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (COSVisitorException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
}

struts.xml 在struts.xml

<action name="GenerateBarCode" class="foo.bar.BarcodeAction" method="getPdf">
   <result name="success" type="stream">
        <param name="contentDisposition">attachment;filename=test.pdf</param>
        <param name="contentType">application/pdf</param>
        <param name="inputName">inputStream</param>
        <param name="bufferSize">1024</param>
    </result>
    <result name="input">/pages/ExpNImp/Export.jsp</result>
    <result name="login">/pages/login.jsp</result>
</action>

How to download the PDF file properly ? 如何正确下载PDF文件?

I made only one change not it is working I removed document.close(); 我仅作了一项更改,但没有起作用,我删除了document.close(); from above code . 从上面的代码。

Now it is working properly 现在它可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM