java 库 pdfbox 将 pdf 文件转换成高清图片方法
近期需要将 pdf 文件转成高清图片,使用库是 pdfbox、fontbox。可以使用 renderImageWithDPI 方法指定转换的清晰度,当然清晰度越高,转换需要的时间越长,转换出来的图片越大,越清晰。
说明:由于 adobo 软件越来越强大,支持的格式越来越多,这造成了 java 软件有些不能转换。所以对于新的格式可能会有转换问题。
1 引入依赖
- <dependency>
- <groupId>org.apache.pdfbox</groupId>
- <artifactId>pdfbox</artifactId>
- <version>2.0.</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
- <dependency>
- <groupId>org.apache.pdfbox</groupId>
- <artifactId>fontbox</artifactId>
- <version>2.0.</version>
- </dependency>
2 代码如下
- public static void convertPdf2Image(String pdfPath, String imageDirPath) {
- log.info("start convert pdf file:[{}] to image path:[{}]", pdfPath, imageDirPath);
- if (!new File(pdfPath).exists()) {
- log.info("pdfFilename:[{}] not exist", pdfPath);
- return;
- }
- if (!new File(imageDirPath).exists()) {
- log.info("imageDir:[{}] not exist", imageDirPath);
- return;
- }
- byte[] pdfContent = FileUtil.getFileContentByte(pdfPath);
- String filename = FileUtil.getFilename(pdfPath);
- float dpi = ;
- convertPdf2Image(pdfContent, filename, imageDirPath, dpi);
- log.info("convert pdf file:[{}] to image success", filename);
- }
- private static void convertPdf2Image(byte[] pdfContent, String pdfFilename, String imageDirPath, float dpi) {
- log.info("convert pdfFilename:[{}] to imageDir:[{}] with dpi:[{}]", pdfFilename, imageDirPath, dpi);
- if (ArrayUtils.isEmpty(pdfContent)) {
- return;
- }
- // 为了保证显示清除,至少 90
- if (dpi < ) {
- dpi = ;
- }
- String baseSir = imageDirPath;
- if (baseSir.endsWith("/") || baseSir.endsWith("\\")) {
- baseSir += pdfFilename + "_";
- } else {
- baseSir += File.separator + pdfFilename + "_";
- }
- PDDocument document = null;
- BufferedOutputStream outputStream = null;
- try {
- document = PDDocument.load(pdfContent);
- int pageCount = document.getNumberOfPages();
- PDFRenderer pdfRenderer = new PDFRenderer(document);
- String imgPath;
- for (int i = ; i < pageCount; i++) {
- imgPath = baseSir + i + ".png";
- outputStream = new BufferedOutputStream(new FileOutputStream(imgPath));
- BufferedImage image = pdfRenderer.renderImageWithDPI(i, dpi, ImageType.RGB);
- ImageIO.write(image, "png", outputStream);
- outputStream.close();
- log.info("convert to png, total[{}], now[{}], ori:[{}], des[{}]", pageCount, i + , pdfFilename, imgPath);
- }
- } catch (IOException e) {
- log.error("convert pdf to image error, pdfFilename:" + pdfFilename, e);
- } finally {
- IOUtil.closeSilently(outputStream);
- IOUtil.closeSilently(document);
- }
- }
- // IOUtil.closeSilently 代码
- public static void closeSilently(Closeable io) {
- if (io != null) {
- try {
- io.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
在实际使用中遇到问题
1)ERROR o.a.p.contentstream.PDFStreamEngine 911 - Cannot read JBIG2 image: jbig2-imageio is not installed
2)Cannot read JPEG2000 image: Java Advanced Imaging (JAI) Image I/O Tools are not installed
3) java.lang.IllegalArgumentException: Numbers of source Raster bands and source color space components do not match at java.awt.image.ColorConvertOp.filter
以上两个问题需要使用 JAI 插件和 jbig2 插件支持,通过引入 jai-imageio-core、jai-imageio-jpeg2000、jbig2-imageio
- <dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core -->
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-jpeg2000 -->
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-jpeg2000</artifactId>
<version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/jbig2-imageio -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jbig2-imageio</artifactId>
<version>3.0.2</version>
</dependency>
参考问题文件
https://github.com/crazyCodeLove/studentservice/blob/master/sys/src/main/resources/pdffile/000208-p1.pdf
https://github.com/crazyCodeLove/studentservice/blob/master/sys/src/main/resources/pdffile/001659-p14.pdf
https://github.com/crazyCodeLove/studentservice/blob/master/sys/src/main/resources/pdffile/main%20doc.pdf
https://github.com/crazyCodeLove/studentservice/blob/master/sys/src/main/resources/pdffile/573636.pdf
参考文献
https://stackoverflow.com/questions/42169154/pdfbox1-8-12-convert-pdf-to-white-page-image
https://stackoverflow.com/questions/20424796/pdf-box-generating-blank-images-due-to-jbig2-images-in-it
https://blog.csdn.net/qq_15801963/article/details/80746830
https://my.oschina.net/u/2345654/blog/1058192
https://stackoverflow.com/questions/18351583/illegalargumentexception-numbers-of-source-raster-bands-and-source-color-space
https://stackoverflow.com/questions/10416378/imageio-read-illegal-argument-exception-raster-bands-colour-space-components
java 库 pdfbox 将 pdf 文件转换成高清图片方法的更多相关文章
- 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件
PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...
- 怎样将PDF文件转换成Excel表格
PDF文件怎样转换成Excel表格呢?因为很多的数据信息现在都是通过PDF文件进行传输的,所以很多时候,信息的接受者都需要将这些PDF文件所传输的数据信息转换成Excel表格来进行整理,但是我们应该怎 ...
- java 用PDFBox 删除 PDF文件中的某一页
依赖: <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-app ...
- PDF文件转换成Excel表格的操作技巧
我们都知道2007以上版本的Office文档,是可以直接将文档转存为PDF格式文档的.那么反过来,PDF文档可以转换成其他格式的文档吗?这是大家都比较好奇的话题.如果可以以其他格式进行保存,就可以极大 ...
- Ghostscript 将PDF文件转换成PNG图片 问题一二
由于项目需求,需要将原来的PDF文档转换成图片文件,在网上找了一些PDF转图片的方法:测试了几个后,都有这样或那样的问题 1.PDFLibNet.dll,这个类型最初还是挺好用的,能转图片和HTML, ...
- nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件
目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...
- Java 使用PDFBox提取PDF文件中的图片
今天做PDF文件解析,遇到一个需求:提取文件中的图片并保存.使用的是流行的apache开源jar包pdfbox, 但还是遇到坑了,比如pdfbox版本太高或太低都不能用!!这个包竟然没有很好地做好兼容 ...
- 使用icepdf将pdf文件转换成照片(以及隐藏的一个bug)
首先引入依赖: <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox ...
- 01.在Java中如何创建PDF文件
1.简介 在这篇快速文章中,我们将重点介绍基于流行的iText和PdfBox库从头开始创建 PDF 文档. 2. Maven 依赖 <dependency> <groupId> ...
随机推荐
- windows 任务计划
我的需求是每天定时访问网站的某一个控制器去刷新库存 流程如下(我自己的理解) 进入任务计划页面 上图指定的bat文件内容就是访问指定的网站路径 dingshi.bat文件内容如下(这个是网上找的,可能 ...
- Oracle12C本地用户的创建和登录
1.查看sysdba下所有PDB以及服务名 select name,pdb from v$services; 2.根据PDB信息修改tnsnames.ora 3.修改listener.ora(网上相关 ...
- Windows下安装Scipy和Numpy失败的解决方案
使用 pip 安装 Scipy 库时,经常会遇到安装失败的问题 pip install numpy pip install scipy 后来网上搜寻了一番才得以解决.scipy 库需要依赖 numpy ...
- linux Crontab定时备份项目案例
首先先写好备份的脚本(拷贝的命令) #bash/bin cd /finance/tomcat8-finance/wtpwebapps tar -czf /finance/webapp_backup/* ...
- linux中>/dev/null 2>&1和2>&1 > /dev/null
转载:https://www.cnblogs.com/520playboy/p/6275022.html 背景 我们经常能在shell脚本中发现>/dev/null 2>&1这样的 ...
- Appache Flume 中文介绍(转)
Flume 是什么 Apache Flume是一个高可靠.高可用的分布式的海量日志收集.聚合.传输系统.它可以从不同的日志源采集数据并集中存储. Flume也算是Hadoop生态系统的一 ...
- CF622F——自然数幂和模板&&拉格朗日插值
题意 求 $ \displaystyle \sum_{i=1}^n i^k \ mod (1e9+7), n \leq 10^9, k \leq 10^6$. CF622F 分析 易知答案是一个 $k ...
- LeetCode 1011. Capacity To Ship Packages Within D Days
原题链接在这里:https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 题目: A conveyor belt h ...
- learning java 重定向标准输入输出
output redirectionOut: public class RedirectOut { public static void main(String[] args) throws File ...
- windows系统的快速失败机制---fastfail
windows系统的快速失败机制---fastfail,是一种用于“快速失败”请求的机制 — 一种潜在破坏进程请求立即终止进程的方法. 无法使用常规异常处理设施处理可能已破坏程序状态和堆栈至无法恢复的 ...