• 【将图片链接中的图片合并成PDF】


    将图片链接中的图片合并成PDF

    1.需要使用到pdfbox

    老规矩,加依赖先

            
            
                org.apache.pdfbox
                pdfbox
                2.0.25
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    2.上代码
        /**
         * 单个图片合并成PDF
         *
         * @param pdDocument pdf文档对象
         * @param imageUrl 图片的url
         */
        public static PDDocument mergeImageToPdf(PDDocument pdDocument, String imageUrl) throws IOException {
            //从url加载图像到内存
            BufferedImage image = ImageIO.read(new URL(imageUrl));
            //创建页面
            PDPage page = new PDPage(PDRectangle.A4);
            pdDocument.addPage(page);
            //创建内容流
            try (PDPageContentStream pdPageContentStream = new PDPageContentStream(pdDocument, page)){
                float imageWidth = page.getMediaBox().getWidth();
                float imageHeight = page.getMediaBox().getHeight();
    
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ImageIO.write(image, "png", byteArrayOutputStream);
                byte[] result= byteArrayOutputStream.toByteArray();
    
                //将内存中的图像添加到页面内容流中
                PDImageXObject pdImageXObject = PDImageXObject.createFromByteArray(pdDocument, result, null);
                // 确保图片按比例缩放以适合页面大小
                float scale = Math.min(imageWidth / image.getWidth(), imageHeight / image.getHeight());
                //添加到PDF上
                pdPageContentStream.drawImage(pdImageXObject, 0, 0, image.getWidth()* scale, image.getHeight() * scale);
    
    //          pdPageContentStream.close();//在try中任务结束后会释放资源
            } catch (IOException e) {
                e.printStackTrace();
            }
            return pdDocument;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
        /**
         * N图片合并
         *
         * @param imageUrls
         * @return
         */
        public static PDDocument imageToPdfMerger(List imageUrls) {
    
            //创建新的PDF文档
            PDDocument pdDocument = new PDDocument(MemoryUsageSetting.setupMainMemoryOnly());
            //创建线程池
            //ExecutorService executor = Executors.newFixedThreadPool(imageUrls.length);
            for (String imageUrl : imageUrls) {
                try {
                    mergeImageToPdf(pdDocument, imageUrl);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return pdDocument;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    3.如果需要保存至本地则直接 pdDocument.save(“文件名称”)
    4.如果需要上传到某个文件系统,可转化为字节流进行操作
  • 相关阅读:
    如何扩展及优化CI/CD流水线?
    Swift中和C语言数据类型转换
    【Golang】结构体详解
    python(一)
    MySQL安装
    亿万级别Kafka演进之路:可靠性+事务+消息中间件+源码+日志
    apache log4j漏洞复现
    PageHelper基础知识
    Git撤销本地commit(转)
    babel原理
  • 原文地址:https://blog.csdn.net/weixin_45430750/article/details/136689765