linux下ppt转图片的方法

上篇介绍了windows上将ppt转换为图片的方法,但是在linux平台上确并无法使用,这里再介绍一下自己开发过程中使用的linux上ppt转图片的方法!
同样的,使用POI会有之前存在的清晰度以及内存的问题,所以介绍另一种工具——libreoffice!
ppt等文件是office的,linux平台没有微软的office,所以我们可以使用其他的office处理软件来转化文件的格式!
这里介绍的方法就是通过libreoffice将ppt转换为pdf,然后在使用pdfbox将pdf转换为高清图片(其实转换为pdf已经可以在前端展示了)

1、安装libreoffice

可以参考官方网站的安装说明,说得很清楚,看清楚系统复制命令就可以(我这里从官网搬运一下)。

Debian/Ubuntu系统 (.deb包):
$ cd ~/下载/ /* 切换到安装包所在的目录 *
$ sudo dpkg -i ./LibreOffice_4.x.x_Linux_x86_deb/DEBS/*.deb  /* 安装主安装程序的所有deb包 */
$ sudo dpkg -i ./LibreOffice_4.x.x_Linux_x86_deb_langpack_zh-CN/DEBS/*.deb  /* 安装中文语言包中的所有deb包 */
$ sudo dpkg -i ./LibreOffice_4.x.x_Linux_x86_deb_helppack_zh-CN/DEBS/*.deb  /* 安装中文离线帮助文件中的所有deb包 */
Fedora/SUSE/Mandriva系统 (.rpm包):
$ cd ~/下载/ /* 切换到安装包所在的目录 *
$ sudo yum install ./LibreOffice_4.x.x_Linux_x86_rpm/RPMS/*.rpm  /* 安装主安装程序的所有rpm包 */
$ sudo yum install ./LibreOffice_4.x.x_Linux_x86_rpm_langpack_zh-CN/RPMS/*.rpm  /* 安装中文语言包中的所有rpm包 */
$ sudo yum install ./LibreOffice_4.x.x_Linux_x86_rpm_helppack_zh-CN/RPMS/*.rpm  /* 安装中文离线帮助文件中的所有rpm包 */

我安装的版本是6.4,大家可以自己选择!检验是否安装成功:

libreoffice6.4 -help

在这里插入图片描述
出现一堆说明就证明已经安装成功了,就可以愉快地进行各种格式转换了。

2、使用libreoffice将ppt转化为pdf
# filePath 是要进行转换的ppt文件路径
# outputPath 是转换完成后PDF文件输出的目录路径
libreoffice6.4 --convert-to pdf:writer_pdf_Export  filePath --outdir   outputPath

在转换的时候如果使用xshell监控服务器,那么会出现弹窗:Xmanager软件来处理X11转发需求…

在这里插入图片描述
这个烦人的弹窗如果我们不点,那么转换就不会开始。我们可以使用两种方法解决这个问题:

  • 第一就是根据提示安装一个xmanage,弹窗自然会消失
  • 第二种方法就是修改xshell的设置,不转发这种请求
    在这里插入图片描述
3、使用pdfbox将转换好的pdf转换为图片

依赖:

<!--pdf 转图片支持-->
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>2.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>fontbox</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>jempbox</artifactId>
			<version>1.8.11</version>
		</dependency>
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>xmpbox</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>preflight</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox-tools</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.13.1</version>
		</dependency>

工具类:

public class PptToImg {
    public static Integer pdfBoxConvert(String filePath,String outputPath) throws IOException {
        File outputDir = new File(outputPath);
        if (!outputDir.exists())
            outputDir.mkdirs();
        String type="png";
        //Loading an existing PDF document
        File file = new File(filePath);
        PDDocument doc = PDDocument.load(file);
        //Instantiating the PDFRenderer class
        PDFRenderer renderer = new PDFRenderer(doc);
        //Rendering an image from the PDF document
        //BufferedImage image = renderer.renderImage(0);
        //Writing the image to a file
        int pageCount = doc.getNumberOfPages();
        for (int i = 0; i < pageCount; i++) {
            BufferedImage tempImage = renderer.renderImageWithDPI(i, 144); // Windows native DPI
            // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
            //ImageIO.write(tempImage, type, new File(outputPath+"\\"+(i+1)+"."+type));
            ImageIO.write(tempImage, type, new File(outputPath+"/"+(i+1)+"."+type));
        }
        //ImageIO.write(image, "JPEG", new File(outputPath));
        System.out.println("Image created");
        //Closing the document
        doc.close();
        return pageCount;
    }
}

直接调用工具类中的方法,然后传入对应的文件路径和输出路径就可以了。图片的效果也是很好的!
在这里插入图片描述
另外这个libreoffice可以将excle、word等各种类型也转换成pdf,所以有在网页预览的功能需求就可以使用这个工具转换成pdf,然后在前端展示!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值