[Java] SpringBoot back-end format conversion: convert PPT to PDF

Add dependencies:

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

Specific code:

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;
    }
}

Error [Solved, welcome to discuss]:

The line of Presentation reports an error:com.aspose.slides.exceptions.ArgumentException: Culture Name: zh-CN-#Hans is not a supported culture

Operating system: Mac
has run this code on a previous project without reporting an error, but this time it reports an error, and importing Windows fonts into the library still does not work...


The problem has been solved, just add the code in the first line of ppt2pdf:

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

Related code link:
[Back-end learning] The back-end of the SpringBoot website converts Word into pdf and then converts it into pictures on a page-by-page basis for display on the front end (linux)

Guess you like

Origin blog.csdn.net/Kandy0125/article/details/121620665