将数据库中的图像转换为pdf文件

时间:2015-12-30 11:20:23

标签: java apache pdf-generation

我正在尝试从数据库创建一个pdf文件,我被困在图像部分,因为Apache pdf框只接受物理文件,而我数据库中的图像是blob格式。

PDXObjectImage image = new PDJpeg(doc,rs.getBlob("image"));

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

将blob转换为InputStream并将其传递给PDJpeg:

Blob imageBlob = rs.getBlob("image");
try (InputStream imageInputStream = imageBlob.getBinaryStream()) {
    PDXObjectImage image = new PDJpeg(doc, imageInputStream);
}

答案 1 :(得分:0)

首先将图像保存到驱动器中,然后将此保存图像添加到报告中。

     Connection      con = null;
     Statement       st = null;
     ResultSet       rs = null;
     InputStream     is = null;
     OutputStream    os = null;
     try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.
                getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>
                    ,"user","password");
        st = con.createStatement();
        rs = st.executeQuery("select student_img from student_profile where id=101");
        if(rs.next()){
            is = rs.getBinaryStream(1);
        }
        is = new FileInputStream(new File("Student_img.jpg"));
        os = new FileOutputStream("resourec/std_img.jpg");
        byte[] content = new byte[1024];
        int size = 0;
        while((size = is.read(content)) != -1){
            os.write(content, 0, size);
        }
    }