Search code examples
javapdfitextpdfbox

ItextPDF migration to pdfbox


I have the following code which i am migrating from itextpdf to pdfbox..

    @RequestMapping(value = "/displaypdf.action", method = RequestMethod.GET)
            public void displaypdf(HttpServletRequest p_objRequest, HttpServletResponse p_objResponse,
                    @RequestParam("filename") String p_sFilename) throws Exception {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(ClmSrDebugConstant.DISPLAYPDF);
                }
                String l_sReportName;
                Blob l_bContent = null;
                String l_sTcn = (String) p_objRequest.getSession().getAttribute(ClmSrConstant.TCN);
                ServletOutputStream l_objServletOutputStream = p_objResponse.getOutputStream();
                Map<String, Object> l_mapConfigFile = new HashMap<String, Object>();
//PDDocument doc = null;
    
                Document document = null;
                try {
                    l_mapConfigFile = m_objClinicalService.retrieveMap(l_sTcn);
                    Iterator<String> it = l_mapConfigFile.keySet().iterator();
                    while (it.hasNext()) {
                        String key = it.next();
                        if (key.contains(p_sFilename)) {
                            l_bContent = (Blob) l_mapConfigFile.get(key);
                        }
                    }
                    l_sReportName = p_sFilename;
                    if (l_bContent != null) {
//doc = new PDDocument();
                    //PDPage firstpage = new PDPage();
                    //doc.addPage(firstpage);
                        document = new Document(PageSize.A4);
                        PdfWriter writer = PdfWriter.getInstance(document, l_objServletOutputStream);
                        document.open();
                        document.addTitle(l_sReportName);
                        p_objResponse.setContentType("application/pdf");
                        byte[] l_bytes = l_bContent.getBytes(1, (int) l_bContent.length());
                        InputStream l_inptStrm = l_bContent.getBinaryStream();
                        PdfReader l_pdfReader = new PdfReader(l_inptStrm, l_bytes);
                        PdfContentByte l_cb = writer.getDirectContent(); // Holds the PDF
                        PdfImportedPage l_page;
                        int l_currentPageNumber = 0;
                        int l_pageOfCurrentReaderPDF = 0;
                        while (l_pageOfCurrentReaderPDF < l_pdfReader.getNumberOfPages()) {
                            if (l_pageOfCurrentReaderPDF > 0) {
                                document.newPage();
                            }
                            l_pageOfCurrentReaderPDF++;
                            l_currentPageNumber++;
                            l_page = writer.getImportedPage(l_pdfReader, l_pageOfCurrentReaderPDF);
                            l_cb.addTemplate(l_page, 0, 0);
                        }
                        writer.freeReader(l_pdfReader);
                    } else {
                        p_objResponse.setContentType(ClmSrConstant.TXT_CNTN_TYP);
                        p_objResponse.getOutputStream().print(ClmSrConstant.NLP_REPORT_ERROR);
                    }
        
                    l_objServletOutputStream.flush();
                    if (document != null) {
                        document.close();
                    }
                    l_objServletOutputStream.close();
        
                } catch (Exception exExp) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.error(ClmSrDebugConstant.EXCEP_DISPLAYPDF, exExp);
                    }
                }
            }

I am not getting that how can i convert this line

PdfWriter writer = PdfWriter.getInstance(document, l_objServletOutputStream); PdfReader l_pdfReader = new PdfReader(l_inptStrm, l_bytes); PdfContentByte l_cb = writer.getDirectContent(); // Holds the PDF

for further conversion.

Please find that i am using now pdfbox -- but unable to find PDFwriter and PDFReader in PDFBOX


Solution

  • PDFbox and itextpdf both are using a different approach, itextpdf skipping the html part you would first have to take a look what the itextpdf would have generated, re-create the same in html, then feed that into the html-to-pdf converter.

    We can use openhtmltopdf-pdfbox and flying-saucer-pdf-openpdf...

    open source libraries are : -- org.apache.pdfbox pdfbox 3.0.1

    org.apache.pdfbox fontbox 3.0.1 org.apache.pdfbox pdfbox-tools 3.0.1

    we can use as they doesn't require the license