Java PDF转图片 pdfbox

JAVA 专栏收录该内容
5 篇文章 0 订阅

引入Maven依赖

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.15</version>
        </dependency>

代码示例

pdfStream:pdf文件的输入流

imgPath:导出图片的路径

返回文件的全路径集合

    public static List<String> pdfToImages(InputStream pdfStream, String imagePath) {
        List<String> result = new ArrayList<>();
        try {
            PDDocument readPdf = PDDocument.load(pdfStream);
            PDFRenderer pdfRenderer = new PDFRenderer(readPdf);
            int totalPage = readPdf.getNumberOfPages();
            int startPage = 0;
            String savePath;
            for (int i = startPage; i < totalPage; i++) {
                String fileName = StringUtil.UUID() + ".jpg";
                savePath = imagePath + fileName;
                BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(i, 144);
                ImageIO.write(bufferedImage, "JPG", new File(savePath));
                result.add(fileName);
            }
            readPdf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

pdf中文乱码解决方案Linux,针对字体库缺失导致的乱码

2021-03-01 09:59:03.590  WARN 380170 --- [   scheduling-1] o.a.pdfbox.pdmodel.font.PDCIDFontType0   : Using fallback LiberationSans for CID-keyed font AdobeKaitiStd-Regular
2021-03-01 09:59:03.591  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3206 (CID 0c86) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.592  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 1875 (CID 0753) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.593  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3684 (CID 0e64) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.594  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 4162 (CID 1042) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.595  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 1599 (CID 063f) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.596  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3024 (CID 0bd0) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.597  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 2131 (CID 0853) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.597  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 4549 (CID 11c5) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.598  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 4451 (CID 1163) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.599  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 2117 (CID 0845) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.599  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3493 (CID 0da5) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.600  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3840 (CID 0f00) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.600  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 4665 (CID 1239) in font AdobeKaitiStd-Regular
2021-03-01 09:59:03.600  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 2309 (CID 0905) in font AdobeKaitiStd-Regular

出现上方类似日志,则表示缺少对应字体,上方日志表示为缺少AdobeKaitiStd-Regular这个字体。从百度或其他方式找打对应字体文件,在linux执行以下操作

#cd /usr/share/fonts/   // 进入系统自带的字体目录
#mkdir myfonts  // myfonts 是你自己随便取得文件夹名字,一定要有这一步
#将字体文件拷贝到这个文件夹下,在cd /usr/share/fonts/目录下执行以下命令
#mkfontscale   
#mkfontdir
#fc-cache -fv           //更新字体缓存
#fc-list    // 查看系统中所有得字体,可用于测试是否安装字体成功

注:项目需重新启动

展开阅读全文
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值