/**
     * 将doc转换为pdf
     *
     * @param docAbsolutedPath 传入word绝对路径
     * @param pdfAbsolutedPath 传入生成的pdf绝对路径
     * @return
     */
    public String  convertWordToPdf(String docAbsolutedPath, String pdfAbsolutedPath) {
        boolean suc = true;
        Document document = null;
        try {
            document = new Document(docAbsolutedPath);
            // 增加,测试字体转换时报错的情况,20200406
            FontSettings fontSettings = new FontSettings();
//       fontSettings.setFontsFolder("C:\\Users\\a\\Desktop\\字体", true);
            IWarningCallback callback = info -> {
                // We are only interested in fonts being substituted.
                if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) {
                    System.out.println("Font substitution: " + info.getDescription());
                }
            };
            document.setWarningCallback(callback);
            // 必须要有这句,否则转pdf时中文会因为字体问题而显示为方块。
            // 另外服务器需要上传中文字体到/usr/share/fonts目录(复制windows C:\Windows\Fonts目录下的字体文件即可)
            document.setFontSettings(fontSettings);
            document.save(pdfAbsolutedPath);

        } catch (Exception e) {
            suc = false;
            //e.printStackTrace();
        } finally {
            if (document != null)
                document = null;
        }
        //System.out.println("topdf---------------------------------------"+pdfAbsolutedPath);
        return pdfAbsolutedPath;
    }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐