java实现PDF转图片 (因第三方平台签署章,vue组件不支持)

本文介绍了如何使用Java将PDF转换为一页长图,包括引入必要的jar依赖和具体转换步骤,以便于在不支持PDF签署的Vue组件中处理。
摘要由CSDN通过智能技术生成

1.需引入jar依赖

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.11</version>
</dependency>

2.将PDF转成一页长图

public static BufferedImage pdfChangeImage(InputStream inputStream) {
        //图像合并使用参数
        int width = 0; // 总宽度
        int[] singleImgRGB; // 保存一张图片中的RGB数据
        int shiftHeight = 0;
        BufferedImage imageResult = null;//保存每张图片的像素值
 
        try {
            //利用PdfBox生成图像
            PDDocument pdDocument = PDDocument.load(inputStream);
            PDFTextStripper text = new PDFTextStripper();
            String text1 = text.getText(pdDocument);
            logger.info("读取PDF文字:" + text1);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            //循环每个页码
            for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
                //dpi参数越大,越清晰
                BufferedImage image = renderer.renderImageWithDPI(i, 300, 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();
        } catch (Exception e) {
            logger.error(" 从PDF转换JPG格式异常!", e);
        }
        return imageResult;
    }
public static void main(String[] args) {
    InputStream inputStream = null;
    BufferedImage bufferedImage = null;
    String url = "";
    try {
        //pdf转img
        bufferedImage = PDFUtils.pdfChangeImage(new URL(url));
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", os);
        inputStream = new ByteArrayInputStream(os.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

得到流后就可以下载到本地或者转存其他空间

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值