使用itext把图片转成pdf文件,图片来自本地路径或者文件上传,输出pdf存在本地或者远程minio

1.使用maven添加需要的jar包

        <!-- 图片转成pdf需要的jar包 -->
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

2.图片来自本地、输出pdf保存到本地 

   public static void main(String[] args) throws IOException {
        String imgFilePath = "D:\\qingYun.png";
        String pdfFilePath = "D:\\test.pdf";
        imgToPdf(imgFilePath,pdfFilePath);
    }

public static boolean imgToPdf(String imgFilePath, String pdfFilePath)throws IOException {
        File file=new File(imgFilePath);
        if(file.exists()){
            Document document = new Document();                     //创建document对象
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(pdfFilePath);            //创建文件输出流
                PdfWriter.getInstance(document, fos);               //PDF文件打印
                document.setPageSize(PageSize.A4);                    //设置文档的类型为A4
                document.open();                         
                Image image = Image.getInstance(imgFilePath);       //根据图片路径得到图片
                float imageHeight=image.getScaledHeight();
                float imageWidth=image.getScaledWidth();
                int i=0;
                while(imageHeight>500||imageWidth>500){             //根据图片高宽设置图片的显示比例,大于500都缩放到500
                    image.scalePercent(100-i);                      //设置图片显示比例
                    i++;
                    imageHeight=image.getScaledHeight();
                    imageWidth=image.getScaledWidth();
                }
                image.setAlignment(Image.ALIGN_CENTER);             //设置图片居中显示
                document.add(image);                                //把图片添加到document中
                System.out.println("转化完成");
            } catch (DocumentException de) {
                System.out.println(de.getMessage());
            } catch (IOException ioe) {
                System.out.println(ioe.getMessage());
            }
            document.close();
            fos.flush();
            fos.close();
            return true;
        }else{
            return false;
        }
    }

3.根据上传的图片InputStream流转成pdf流保存到Minion中

ByteArrayOutputStream arrayStreamImage = null;

ByteArrayOutputStream arrayStream = null;  

InputStream returnStream = null;

try {

                arrayStreamImage = getDealInputStream(in);                        //把文件input流转成output数组流(in为InputStream流)
                arrayStreamImage.flush();                                                     //冲刷数组流
                 Image image = Image.getInstance(arrayStreamImage.toByteArray());//根据output数组流转成Image图片
                float imageHeight=image.getScaledHeight();                      //获取图片的高度
                float imageWidth=image.getScaledWidth();                        //获取图片的宽度
                int i=0;
                while(imageHeight>500||imageWidth>500){                         //根据图片的高宽设置图片的显示比例
                    image.scalePercent(100-i);                                             //设置图片显示比例
                    i++;
                    imageHeight=image.getScaledHeight();
                    imageWidth=image.getScaledWidth();
                }
                image.setAlignment(Image.ALIGN_CENTER);       //图片的居中位置
                arrayStream = new ByteArrayOutputStream();    //PdfWriter.getInstance方法需要new出来的输出流
                 Document document = new Document();           //创建一个document对象
                 PdfWriter.getInstance(document,arrayStream);  //把document对象转成pdf文件,存放到arrayStream输出流中
                 document.open();                              //打开document
                document.add(image);                          //把图片添加到document对象中
                 document.close();                             //关闭document

               returnStream = new BufferedInputStream(new ByteArrayInputStream(arrayStream.toByteArray()));   //把output流数组转成input流  
             minioClient.putObject(minion的桶名称,minio的存储路径(包含文件名),                    returnStream,returnStream.available(),"application/octet-stream");//minioClient是链接minio的客户端,这里就不给出了这是把转成PDF的文件输入流上传到minio,具体上传都哪里根据你自己的项目来定

}catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (arrayStreamImage != null){
                try {
                    arrayStreamImage.close();
                } catch (Exception e) {
                   throw new RuntimeException(e);
                }
            }
            if (returnStream != null){
                try {
                    returnStream.close();
                } catch (Exception e) {
                   throw new RuntimeException(e);
                }
            }if (arrayStream != null){
                 try {
                     arrayStream.close();
                  } catch (Exception e) {
                     throw new RuntimeException(e);
                  }
            }
            if (in != null) {
               try {
                  in.close();
                } catch (IOException e) {
                   e.printStackTrace();
                }
            }
        }

    /**
     * 
     * @Description 得到处理过后的文件输入流
     * @author 清云[zhanglizeng] Tel:18860126570
     * @param inputStream 
     * @return    
     * @throws IOException 
     * @createDate 2018年4月23日 下午3:33:06    
     */
    public ByteArrayOutputStream getDealInputStream(InputStream inputStream) throws IOException {
        ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len;    
        while ((len = inputStream.read(b)) > -1) {
            arrayStream.write(b, 0, len);
        }
        arrayStream.flush();
        return arrayStream;
    }

猜你喜欢

转载自blog.csdn.net/ZHANGLIZENG/article/details/91959700