Search code examples
jakarta-eepdf-generationbirtoutputstream

Birt edition with two output streams


I have a birt edition launched from a Java EE application, when the user launches the action, pdf generation launches in a new window, all of it works well, changes I want to make is this: I want to keep the same pdf in my server to send it in email later.

PDFRenderOption pdfOptions = new PDFRenderOption(options);
pdfOptions.setOutputFormat("pdf");
options.setSupportedImageFormats("JPG;BMP;PNG;JPEG");
pdfOptions.setOutputFileName(OUTPUT_LOCATION + "project/exportprojet.pdf");
pdfOptions.setOutputStream(response.getOutputStream());
runAndRenderTask.setRenderOption(pdfOptions);

after adding pdfOptions.setOutputFileName(OUTPUT_LOCATION + "project/exportprojet.pdf");, the pdf is generated and stocked in the server, but what worked before is not working anymore, a new window is launched to the user and the pdf is not displayed, it looks like the added line took the generated pdf and put in the server by preventing it from going to the user


Solution

  • the implementation of the solution proposed by hvb works perfectly, following changes made to my old code :

     PDFRenderOption pdfOptions = new PDFRenderOption(options);
    String sUrlExportFile = OUTPUT_LOCATION + "projet/" + idProjetAGenerer + "/exportprojet.pdf";
    pdfOptions.setOutputFormat("pdf");
    options.setSupportedImageFormats("JPG;BMP;PNG;JPEG");
    pdfOptions.setOutputFileName(sUrlExportFile);
    runAndRenderTask.setRenderOption(pdfOptions);
    runAndRenderTask.getAppContext().put(EngineConstants.APPCONTEXT_BIRT_VIEWER_HTTPSERVET_REQUEST, request);
    runAndRenderTask.run();
    runAndRenderTask.close();
    //copy the genereted file to response.getOutputStream()
    File f = new File(sUrlExportFile);
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(f);
        IOUtils.copy(inputStream, response.getOutputStream());
    } finally {
        IOUtils.closeQuietly(inputStream);
    }