zoukankan      html  css  js  c++  java
  • springboot 使用itextpdf 框架实现多个图片合成一个pdf文件

    以下两个方法引入头

    import com.lowagie.text.*;
    import com.lowagie.text.pdf.PdfWriter;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.rendering.ImageType;
    import org.apache.pdfbox.rendering.PDFRenderer;
    import org.springframework.stereotype.Component;
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.List;

    1、图片转pdf文件

    pom 文件引入

        <dependency>
                <groupId>com.lowagie</groupId>
                <artifactId>itext</artifactId>
                <version>2.1.7</version>
            </dependency>
     public File imageToPdf(List<String> imageUrllist, String mOutputPdfFileName) {
    
            String TAG = "PdfManager";
            Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
            try {
                PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
                doc.open();
                for (int i = 0; i < imageUrllist.size(); i++) {
                    doc.newPage();
    //                doc.add(new Paragraph("简单使用iText"));
                    Image png1 = Image.getInstance(imageUrllist.get(i));
                    float heigth = png1.getHeight();
                    float width = png1.getWidth();
                    int percent = getPercent2(heigth, width);
                    png1.setAlignment(Image.MIDDLE);
                    png1.scalePercent(percent+3);// 表示是原来图像的比例;
                    doc.add(png1);
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                doc.close();
    
            }
            File mOutputPdfFile = new File(mOutputPdfFileName);
            if (!mOutputPdfFile.exists()) {
                mOutputPdfFile.deleteOnExit();
                return null;
            }
            return mOutputPdfFile;
        }

    /**
    * 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
    *
    * @param h
    * @param w
    * @return
    */
    private int getPercent(float h, float w) {
    int p = 0;
    float p2 = 0.0f;
    if (h > w) {
    p2 = 297 / h * 100;
    } else {
    p2 = 210 / w * 100;
    }
    p = Math.round(p2);
    return p;
    }

    /**
    * 第二种解决方案,统一按照宽度压缩 这样来的效果是,所有图片的宽度是相等的,自我认为给客户的效果是最好的
    *
    * @param
    */
    private int getPercent2(float h, float w) {
    int p = 0;
    float p2 = 0.0f;
    p2 = 530 / w * 100;
    p = Math.round(p2);
    return p;
    }
     

    2、pdf 转图片

    pom 引入

    <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>pdfbox</artifactId>
                <version>2.0.1</version>
            </dependency>
     //经过测试,dpi为96,100,105,120,150,200中,105显示效果较为清晰,体积稳定,dpi越高图片体积越大
        //一般电脑显示分辨率为96
        public static final float DEFAULT_DPI=105;
        /**pdf转图片
         * @param pdfPath
         */
        public  String pdfToImage(String pdfPath, HttpServletResponse res, HttpServletRequest request){
            try{
                if(pdfPath==null||"".equals(pdfPath)||!pdfPath.endsWith(".pdf"))
                    return null;
                //图像合并使用参数
                int width = 0; // 总宽度
                int[] singleImgRGB; // 保存一张图片中的RGB数据
                int shiftHeight = 0;
                BufferedImage imageResult = null;//保存每张图片的像素值
                //利用PdfBox生成图像
                PDDocument pdDocument = PDDocument.load(new File(pdfPath));
                PDFRenderer renderer = new PDFRenderer(pdDocument);
                //循环每个页码
                for(int i=0,len=pdDocument.getNumberOfPages(); i<len; i++){
                    BufferedImage image=renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);
                    int imageHeight=image.getHeight();
                    int imageWidth=image.getWidth();
                    if(i==0){//计算高度和偏移量
                        width=imageWidth;//使用第一张图片宽度;
                        //保存每页图片的像素值
                        imageResult= new BufferedImage(width, imageHeight*len, BufferedImage.TYPE_INT_RGB);
                    }else{
                        shiftHeight += imageHeight; // 计算偏移高度
                    }
                    singleImgRGB= image.getRGB(0, 0, width, imageHeight, null, 0, width);
                    imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 写入流中
                }
                pdDocument.close();
                String outPath = pdfPath.replace(".pdf", "_"+DEFAULT_DPI+".jpg");
                File outFile = new File(outPath);
                ImageIO.write(imageResult, "jpg", outFile);// 写图片
    
                return outPath;
            }catch (Exception e) {
                e.printStackTrace();
            }
              return null;
        }
  • 相关阅读:
    Android应用四大组件和应用程序的生命周期
    DOCTYPE html PUBLIC 指定了 HTML 文档遵循的文档类型定义
    Web前端灰框技术
    用JS实现在页面关闭或刷新时触发特定的事件
    style.display的全部属性值
    Android SDK更新的问题
    页面灰框模式精彩实现
    解决sql 语句中truncate语句不支持变量的问题
    获取网页中图片链接的路径的正则表达式
    web页面自动保存本页面的内容到本地
  • 原文地址:https://www.cnblogs.com/memoryXudy/p/8709931.html
Copyright © 2011-2022 走看看