I made only one change not it is working
I removed
document.close();
from above code .
Now it is working properly
سؤال
I am trying to download a PDF file with Struts2. For generating the PDF I am using PDFBox.
I am able to download the PDF file, but the problem is that its size is 0 bytes.
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
<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 ?
المحلول
I made only one change not it is working
I removed
document.close();
from above code .
Now it is working properly