【Java】SpringBoot后端格式转换:把PPT转成PDF

添加依赖:

<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-slides</artifactId>
  <version>19.3</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/src/main/resources/jar/aspose.slides-19.3.jar</systemPath>
</dependency>

具体代码:

public class ConvertUtil {

    public static boolean getLicense() {
        boolean result = false;
        try {
            if(!isWindows())
            {
                FontSettings.getDefaultInstance().setFontsFolder("/Library/Fonts",true);
            }
            InputStream license = ConvertUtil.class.getClassLoader().getResourceAsStream("license.xml");
            com.aspose.slides.License aposeLic = new License();
            aposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean isWindows()
    {
        String os = System.getProperty("os.name");

        if(os.toLowerCase().startsWith("win"))
        {
            return true;
        }
        return false;
    }

    public static boolean ppt2Pdf(String pptPath, String pdfPath) {
        /*
        Locale locale = new Locale("zh", "cn");
        Locale.setDefault(locale);
        */
        // 验证License
        if (!getLicense()) {
            return false;
        }
        FileOutputStream fileOS = null;
        try {
            File file = new File(pdfPath);
            Presentation pres = new Presentation(pptPath);
            fileOS = new FileOutputStream(file);
            pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
            fileOS.close();
        } catch (Exception e) {
            if(fileOS != null) {
                try {
                    fileOS.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
        return true;
    }
}

报错【已解决,欢迎讨论】:

Presentation那一行报错: com.aspose.slides.exceptions.ArgumentException: Culture Name: zh-CN-#Hans is not a supported culture

操作系统:Mac
在之前的项目上运行过这段代码没有报错过,但是这次却报错了,把Windows字体导入库中也仍然不行…


问题已经解决了,在ppt2pdf的第一行添加代码即可:

Locale locale = new Locale("zh", "cn");
Locale.setDefault(locale);

相关代码链接:
【后端学习】SpringBoot网站后端将Word转成pdf再按页转成图片在前端展示(linux)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值