png转化为pdf

代码
package com.test;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.util.ObjectUtils;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.*;

public class PNGToPDFConverter {

static ExecutorService POOL = new ThreadPoolExecutor(10, 30, 0L,TimeUnit.SECONDS, new LinkedBlockingQueue<>());

public static void main(String[] args) throws InterruptedException {
    long start = System.currentTimeMillis();
    System.out.println("开始合成图片并发" + start);
    CountDownLatch countDownLatch = new CountDownLatch(50);
    for (int i = 0; i < 50; i++) {
        MakePicture makePicture = new MakePicture();
        makePicture.setCountDownLatch(countDownLatch);
        POOL.execute(makePicture);
    }
    countDownLatch.await();
    long end = System.currentTimeMillis();
    System.out.println("总耗时" + (end - start));
}

static class MakePicture implements Runnable{

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    private CountDownLatch countDownLatch;

    static Random r = new Random();

    public static String getRandomStringInRange(int min, int max) {
        return String.valueOf(r.ints(min, (max + 1)).limit(1).findFirst().getAsInt());
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();
        String[] imageFiles = {"1.png", "2.png", "3.png", "4.png","5.png","6.png"}; // 将要转换的PNG文件名列表
        String outputFilename = "outPdf/outPdf" + countDownLatch.getCount() + ".pdf"; // 输出PDF文件名
        String randomStringInRange = getRandomStringInRange(5, 10);
        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();
            Document document = new Document();
            PdfWriter.getInstance(document, os);
            document.open();
            for (Integer i = 0; i < Integer.valueOf(randomStringInRange); i++) {
                Image image = Image.getInstance(imageFiles[i%5]);
                // 设置图像大小
                float width = PageSize.A4.getWidth() - document.leftMargin() - document.rightMargin();
                float scaleFactor = width / image.getWidth();
                image.scalePercent(scaleFactor * 100f);
                document.add(image);
            }
            document.close();
            new FileOutputStream(outputFilename).write(os.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (!ObjectUtils.isEmpty(os)) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread() + "制图耗时---------"  + (end - start) + "制图数量--------" + randomStringInRange);
        countDownLatch.countDown();
    }
}

}

pom
在这里插入图片描述
核心代码
在这里插入图片描述
注意先调用document.close()方法 然后再去通过ByteArrayOutputStream字节流去上传小文件或者写到本地 否则写到本地的文件格式是损坏的!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值