【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)

猜你喜欢

转载自blog.csdn.net/Kandy0125/article/details/121620665