oracle apex、apache pdfbox java存储过程问题

yzxexxkh  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(294)

一些背景:在我的应用程序中,有一些pdf报告。但是,这些pdf报告需要“图像”为基础,我被告知,报告服务器无法做到这一点。对报表服务器的调用是通过一个pl/sql过程完成的,结果是一个blob,所以现在我所能做的就是尝试执行这个转换的java存储过程。下面是我想到的(使用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;
        }
    }

pdfbox jar已加载到数据库中
数据库是oracle 19c standard edition 2 release 19.0.0.0.0
我将此代码作为一个独立的java项目进行了尝试,但有一个例外,即pdf文件是从磁盘读取的,新文件是写入磁盘的,在那里它可以完美地工作。
问题是:

我相信问题是从这一行开始的: BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB); 但是我不知道是什么导致了这个错误,也不知道如何调试它(我是通过一个艰苦的消除代码和抛出异常的过程得出这个结论的),特别是因为它在一个独立的项目中工作。java存储过程不是我的专长,这段代码是从网上许多不同的源代码拼凑而成的。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题