java spring把 docx doc ppt pptx xlsx xls 转成pdf

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;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值