Oracle Apex, Apache PDFBox java stored procedure issues

321 Views Asked by At

Some background: in my app, there are some pdf reports. But, these pdf reports need to be "image" based and I was told that the report server is unable to do this. The call to the report server is done from a pl/sql procedure and the result is a blob, so now all I have at my disposal to try to do this conversion is a java stored procedure. Here is what I came up with (using Apache PDFBox):

create or replace and compile java source named "APDFUtil"
    as
    import oracle.sql.*;
    import oracle.jdbc.driver.*;
    import java.sql.*;
    import oracle.sql.BLOB;
    import java.sql.Blob;
    import javax.imageio.ImageIO;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.io.*;
    import java.util.*;
    import javax.imageio.ImageIO;
    import org.apache.pdfbox.pdmodel.*;
    import org.apache.pdfbox.rendering.*;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.pdmodel.common.PDRectangle;
    import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
    import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
    import org.apache.pdfbox.rendering.PDFRenderer;
    import org.apache.pdfbox.tools.imageio.ImageIOUtil;
    import java.awt.image.BufferedImage;
    
        public class APDFUtil{
            
           
    
     static OracleDriver ora = new OracleDriver();
        static Connection conn;
        static ByteArrayOutputStream out;
        
        static {
            try {
                conn = ora.defaultConnection();
            } catch (Exception ex) {}
        }
          
        public static oracle.sql.BLOB flattenPDF (oracle.sql.BLOB value) throws Exception {
            
            if (conn == null) conn = ora.defaultConnection();
            BLOB retBlob = BLOB.createTemporary(conn, true, oracle.sql.BLOB.DURATION_SESSION);
             
            /*BEGIN TO_JPG*/
            InputStream inputStream = value.getBinaryStream();
            PDDocument document = PDDocument.load(inputStream);
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            
            int noOfPages = document.getNumberOfPages();
            BufferedImage[] pdfJPEG = new BufferedImage[noOfPages];
            
            for (int page = 0; page < noOfPages; ++page) {
                    BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);           
                    pdfJPEG[page] = bim;
            }         
            
            /*write images to new pdf*/
            PDDocument documentOut = new PDDocument();
                
            for (int page = 0; page < noOfPages;++page) {
                /*get page from old document to determine width and height*/
                PDPage oldPage = document.getPage(page);
                Float pw = oldPage.getMediaBox().getWidth();
                Float ph = oldPage.getMediaBox().getHeight();
                
                PDRectangle rec = new PDRectangle(pw,ph);
                PDPage newPage = new PDPage(rec);
                
                documentOut.addPage(newPage);
                
                PDImageXObject pdImage = LosslessFactory.createFromImage(documentOut, pdfJPEG[page]);
        
                PDPageContentStream contents = new PDPageContentStream(documentOut, newPage);
                contents.drawImage(pdImage, 0, 0,pw,ph);
                contents.close();                       
            }               
                   
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            documentOut.save(out);
        
            documentOut.close();
            document.close();
            /*END OF TO_JPG*/
            
           /*out - we used to get this back from TO_JPG*/
            try {
                java.io.OutputStream outStr = retBlob.setBinaryStream(0);
                outStr.write(out.toByteArray());
                outStr.flush();
            } finally {
            out.close();
            }
            return retBlob;
        }
    }
  • the pdfbox jars have been loaded into the database
  • database is oracle 19c standard edition 2 release 19.0.0.0.0
  • I tried this code as a standalone java project with the exception that the pdf file is being read from the disk, and new file written to the disk and there it works flawlessly.

The issue:

enter image description here

I believe the problem starts at this line: BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB); but I don't know what is causing the error or how to debug it (I came to this conclusion by a painstaking process of elimination of code and throwing exceptions) especially since it works in a standalone project. Java stored procedures are not a specialty of mine and this code was pieced together from many different sources online.

0

There are 0 best solutions below