Daler
Daler

Reputation: 1205

How to save generated pdf in the web app directory [xdocreport]?

I am using xdocreport to generate a pdf document from *.docx files. I use struts2 for implementation. I followed this https://github.com/opensagres/xdocreport/wiki/DocxReportingStruts2

I do have it working correctly, it does generate pdf and fills out the docx files with everything from the java.

My problem is: once the cycle is done, it throws the generated file directly to the user for download. So once the xdocreport generates everything, the user's browser will start downloading it.

Instead, I want the generated pdf be saved in the web app [or in the same drive separate folder] directory, and then from there I would like to ask the user if he wants to download it or no, if he clicks yes, then download it. Also, I have to have the backup of the generated pdf document, so I have to somehow first generate and save it in web app [or in the same drive separate folder] folder and after that give it to the user.

How can I change it's behavior?

EDIT1 [based on below answer]:

So now it throws this exception, but even though it throws it I can see the converted pdf shown on the browser but I can't open the pdf document itself, which is generated in the filesystem.

* ERROR 2016-08-04 09:33:16,079 ExceptionConverter: org.eclipse.jetty.io.EofException (XWPF2PDFViaITextConverter.java [qtp559888791-19])
* ERROR 2016-08-04 09:33:16,096 Error while executing action (ExceptionInterceptor.java [qtp559888791-19])
fr.opensagres.xdocreport.converter.XDocConverterException: org.apache.poi.xwpf.converter.core.XWPFConverterException: ExceptionConverter: org.eclipse.jetty.io.EofException
at     fr.opensagres.xdocreport.converter.docx.poi.itext.XWPF2PDFViaITextConverter.convert(XWPF2PDFViaITextConverter.java:72)
at fr.opensagres.xdocreport.document.AbstractXDocReport.convert(AbstractXDocReport.java:713)
at org.apache.struts2.views.xdocreport.AbstractXDocReportResult.doProcessReportWithConverter(AbstractXDocReportResult.java:561)
at org.apache.struts2.views.xdocreport.AbstractXDocReportResult.doExecute(AbstractXDocReportResult.java:284)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:369)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:273)

Upvotes: 0

Views: 1070

Answers (1)

Maksim Maksimov
Maksim Maksimov

Reputation: 115

As you can see on the page you gave a link to, you can set param download to false and that will disable download.

<action name="DisplayProject" class="example.DisplayProject">
  <result name="success" type="xdocreport">
  <param name="location">/docx/DocxProjectWithVelocityList.docx</param>
  <param name="templateEngine">Velocity</param>
  <param name="fieldAsList">developers.Name,developers.LastName</param>
  <param name="download">false</param>
</result>

and to save file in web app folder, I guess you can

public void populateContext(IXDocReport report, IContext context)
        throws Exception {
context.put("project", getProject());
context.put("developers", getDevelopers());

try (FileOutputStream fOut = new FileOutputStream(servletContext.getRealPath("/") + "filename")) {
    report.process(context, fOut);
} catch (Exception e) {
     // Handle exception
}

}

Just assumptions, not tested.

Upvotes: 1

Related Questions