Java 网络PDF转图片

本文详细介绍了使用Apache PDFBox库将PDF文件转换为图片的方法,包括依赖配置、代码实现及异常处理,适用于需要将PDF文档快速转化为图像的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

照旧是总结前人的技术

show code!

一 依赖

<!--PDF转换为图片-->
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>fontbox</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
   <groupId>org.apache.pdfbox</groupId>
   <artifactId>pdfbox</artifactId>
   <version>2.0.1</version>
</dependency>

二 代码

    public void pdfToImg(HttpServletResponse response ,String pdfUrl) {
        String imageType = "PNG";//可改成JPG
        OutputStream sos = null;
        try {
            sos = response.getOutputStream();
            PDFToImg(sos, pdfUrl, getPDFNum(pdfUrl), imageType);
            sos.flush();
        } catch (Exception e) {
            log.error("PDF转换图片异常: " + e.getMessage());
            throw new RuntimeException("PDF转换异常");
        } finally {
            try {
                sos.close();
            } catch (IOException e) {
                log.error("关闭输出流异常: " + e.getMessage());
            }
        }
    }

    /**
     * PDF转图片 根据页码一页一页转
     * @throws IOException
     * imgType:转换后的图片类型 jpg,png
     */
    private void PDFToImg(OutputStream sos,String fileUrl,int page,String imgType) throws Exception {
        PDDocument pdDocument = null;
        /* dpi越大转换后越清晰,相对转换速度越慢 */
        int dpi = 100;
        try {
            pdDocument = getPDDocument(fileUrl);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            int pages = pdDocument.getNumberOfPages();
            if (page <= pages && page > 0) {
                BufferedImage image = renderer.renderImageWithDPI(page-1,dpi);
                ImageIO.write(image, imgType, sos);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            throw new RuntimeException("PDF转换异常");
        } finally {
            if (pdDocument != null) {
                pdDocument.close();
            }
        }

    }

    private PDDocument getPDDocument(String fileUrl) throws IOException {
        InputStream inputStream = getInputStreamByUrl(fileUrl);
        return PDDocument.load(inputStream);
    }


    /**
     * 根据url地址 获取输入流
     * @param url
     * @return
     * @throws IOException
     */
    private InputStream getInputStreamByUrl(String url) throws IOException {
        URL httpurl=new URL(URLDecoder.decode(url, "UTF-8"));
        InputStream is;
        HttpURLConnection httpConn=(HttpURLConnection)httpurl.openConnection();
        httpConn.setDoOutput(true);// 使用 URL 连接进行输出
        httpConn.setDoInput(true);// 使用 URL 连接进行输入
        httpConn.setUseCaches(false);// 忽略缓存
        httpConn.setRequestMethod("GET");// 设置URL请求方法
        //可设置请求头
        httpConn.setRequestProperty("Content-Type", "application/octet-stream");
        httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
        httpConn.setRequestProperty("Charset", "UTF-8");
        httpConn.connect();
        if (httpConn.getResponseCode() >= 400 ) {
            is = httpConn.getErrorStream();
        }
        else{
            is = httpConn.getInputStream();
        }
        return is;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值