Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

java - PDF generation using iText in Struts-2 : result type stream not working

My requirement is to generate PDF file using iText, I use below code to create a sample PDF

Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("success PDF FROM STRUTS"));
document.close();
ServletOutputStream outputStream = response.getOutputStream() ;
baos.writeTo(outputStream);
response.setHeader("Content-Disposition", "attachment; filename="stuReport.pdf"");
response.setContentType("application/pdf");
outputStream.flush();
outputStream.close();

If you see in the above code, iText is not using any inputStream parameter, rather it is writing directly to response's outputstream. Whereas struts-2 is mandating us to use InputStream parameter (see the configuration below)

<action name="exportReport" class="com.export.ExportReportAction">
    <result name="pdf" type="stream">
        <param name="inputName">inputStream</param>
        <param name="contentType">application/pdf</param>
        <param name="contentDisposition">attachment;filename="sample.pdf"</param>
        <param name="bufferSize">1024</param>
    </result>
</action>

I know that my class should have getters and setters for inputStream and i have that too in the class mentioned in struts-configuration

private InputStream inputStream;
public InputStream getInputStream() {
    return inputStream;
}

public void setInputStream(InputStream inputStream) {
    this.inputStream = inputStream;
}

But since iText doesn't really need inputstream rather it is writing directly to response's outputstream, i get exceptions since am not setting anything for the inputStream parameter.

Please let me know how to use iText code in struts-2 having the resultType as stream

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Found solution to this.

The method in the action which performs this PDF export can be void. The result type configuration is not needed while we are writing directly to response's outputstream

for example, have your action class this way

Class ExportReportAction extends ActionSupport {
  public void exportToPdf() { // no return type
    try {
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("success PDF FROM STRUTS"));
        document.close(); 
        ServletOutputStream outputStream = response.getOutputStream() ; 
        baos.writeTo(outputStream); 
        response.setHeader("Content-Disposition", "attachment; filename="stuReport.pdf""); 
        response.setContentType("application/pdf"); 
        outputStream.flush(); 
        outputStream.close(); 
    }catch (Exception e) {
        //catch
    }

  } 
}

and have your struts-configuration this way

<action name="exportReport" class="com.export.ExportReportAction"> 
 <!-- NO NEED TO HAVE RESULT TYPE STREAM CONFIGURATION-->
</action>

this works cool !!!

Thanks for all who attempted to answer this question


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...