docx 转 pdf
1.maven 添加
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
2.下载openOffice
官方网站: http://www.openoffice.org/download/
选择好对应系统后点击 Download full installation 按钮进行下载
3.安装
linux
tar -zxvf 对应的压缩包名字
cd 进入解压后的 ./zh-CN/RPMS
yum localinstall *.rpm
下面是安装可视化相关可以安装
cd desktop-integration
rpm -ivh openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm
安装好后配置配置文件(yml格式):
jodconverter:
local:
enabled: true
office-home: /opt/openoffice4
max-tasks-per-process: 10
port-numbers: 8324
Mac:
打开拖入Applications即可
安装好后配置配置文件(yml格式):
jodconverter:
local:
enabled: true
office-home: /Applications/OpenOffice.app/Contents
max-tasks-per-process: 10
port-numbers: 8345
window跟Mac同理,这里就不再赘述了。
最后加上附上代码
/**
* PDF文件转PNG图片,全部页数
* @param dstImgFolder 文件存放文件夹路径
* @param pdfFilePath pdf完整路径
* @param dpi dpi越大转换后越清晰,相对转换速度越慢
* @date 2020/8/13
*/
public static String pdf2Image(String pdfFilePath, String dstImgFolder, int dpi) {
File file = new File(pdfFilePath);
PDDocument pdDocument;
String imgFolderPath;
try {
String imgPdfPath = file.getParent();
int dot = file.getName().lastIndexOf('.');
// 获取图片文件名
String imagePdfName = file.getName().substring(0, dot);
if ("".equals(dstImgFolder)) {
// 获取图片存放的文件夹路径
imgFolderPath = imgPdfPath + File.separator + imagePdfName;
} else {
imgFolderPath = dstImgFolder + File.separator + imagePdfName;
}
if (FileAndDirectoryUtil.createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
/* dpi越大转换后越清晰,相对转换速度越慢 */
PdfReader reader = new PdfReader(pdfFilePath);
int pages = reader.getNumberOfPages();
StringBuffer imgFilePath;
for (int i = 0; i < pages; i++) {
String imgFilePathPrefix = imgFolderPath + File.separator ;
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append((i + 1));
imgFilePath.append(".png");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, "png", dstFile);
}
log.info("PDF文档转PNG图片成功!");
return imgFolderPath;
} else {
log.error("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
return null;
}
} catch (IOException e) {
e.printStackTrace();
log.error("PDF文档转PNG图片错误");
return null;
}
}