0

Currently I work using Spring Framework. I was asked to print a report with a PDF output that retrieves data from a SQL database, but the project I'm working on has Apache POI installed where the output is Excel XLSX. Is there a way to convert Excel XLSX format to PDF? I already have Apache POI and recently added iTextPDF.

Apache POI

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>

iTextPDF

    <properties>
        <itext.version>7.2.3</itext.version>
    </properties>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>${itext.version}</version>
            <type>pom</type>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>${itext.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>io</artifactId>
            <version>${itext.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>${itext.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>forms</artifactId>
            <version>${itext.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>pdfa</artifactId>
            <version>${itext.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>sign</artifactId>
            <version>${itext.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>barcodes</artifactId>
            <version>${itext.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>font-asian</artifactId>
            <version>${itext.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>hyph</artifactId>
            <version>${itext.version}</version>
        </dependency>

EDIT : This is the code to generate XLSX file

    @RequestMapping(value = "/download", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('CTRL_REPORT_READ')")
    @ResponseBody
    public void download(@Valid @ModelAttribute ReportForm reportForm, BindingResult result, RedirectAttributes redirectAttrs, HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.debug("IN: reimbursement/dowload report-POST");
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook();
            downloadExcel(reportForm, workbook);
            response.setContentType("application/vnd.ms-excel");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
            String fileName = "Reimbursement Report " + sdf.format(new Date()) + ".xlsx"; 
            response.setHeader("Content-disposition", "attachment; filename=" + fileName);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            logger.error("Error occurs when add new data in method reimbursement with error message : " + e.getMessage());
            redirectAttrs.addFlashAttribute(AppDataConstant.ERROR_FLASH_RESP, "Download reimbursement report failed");
        } finally {
            if (workbook != null) {
                workbook.close();
            }
        }

    }

Thank you very much before

  • 2
    [Apache POI 3.15 is over 6 years old and has loads of bugs](https://poi.apache.org/devel/history/changes-3x.html#3.15) why are you using such an old version? – Gagravarr Aug 09 '22 at 07:59
  • if you're looking for a solution within the itext ecosystem, [pdfOffice](https://itextpdf.com/products/itext-7/convert-ms-office-to-pdf-pdfoffice) does the trick (XLS to PDF). – André Lemos Aug 09 '22 at 10:12
  • re-reading your question, if the project is already using Apache POI to generate XLSX, I'm guessing it should be pretty trivial to generate PDFs. do you have the code that actually generates the XLSX files? – André Lemos Aug 09 '22 at 10:13
  • I'd probably look more into this direction https://stackoverflow.com/a/51337157/1566339 – André Lemos Aug 09 '22 at 10:15
  • @Gagravarr because the project I'm working on already has Apache POI 3.15. Should I upgrade to the latest version? – Ikhsan Kurnia Prayoga Aug 10 '22 at 01:12
  • @AndréLemos yes, I have the code. I'll edit my question to insert the code that generates the XLSX file and I will try the method you gave – Ikhsan Kurnia Prayoga Aug 10 '22 at 01:15

0 Answers0