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

エラー [解決済み、議論歓迎]:

プレゼンテーションの行でエラーが報告されます。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 Web サイトのバックエンドは Word を PDF に変換し、それをページごとに画像に変換してフロントエンドで表示します (Linux)

おすすめ

転載: blog.csdn.net/Kandy0125/article/details/121620665