WORD转PDF 字体问题

项目中有需求需要导出PDF,

市面上现在有很多word导出PDF,比如常用的两款 docx4j 和 aspose-words

docx4j 是开源的 aspose-words是商用的 需要购买License 但是网上有破解的可以搜索到

docx4j

maven依赖

<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-export-fo</artifactId>
    <version>8.3.8</version>
</dependency>
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
    <version>8.3.8</version>
</dependency>

特别说明: 需要的是 *.ttf 格式的字体库 如果缺失会出现乱码 小方格。
1,word 导出pdf

    public void outPDF2(ByteArrayOutputStream outputStream, HttpServletResponse response, String fileName) throws Exception {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputStream);
        ExcelUtils.exportPdf(response, fileName);
        setFontMapper(wordMLPackage);
        Docx4J.toPDF(wordMLPackage, response.getOutputStream());
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(response.getOutputStream());
    }

2, 引入需要的字体

private static void setFontMapper(WordprocessingMLPackage mlPackage) throws Exception {
        Mapper fontMapper = new IdentityPlusMapper();

        //引入系统的
        fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
        fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
        fontMapper.put("等线", PhysicalFonts.get("SimSun"));
        fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
        fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
        fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
        //解决宋体(正文)和宋体(标题)的乱码问题
        PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));

        // 如果系统 自己下载的字体 引入
        PhysicalFonts.addPhysicalFonts("Microsoft Yahei", new ClassPathResource("font/Microsoft YaHei.ttf").getURI());
        fontMapper.put("Microsoft Yahei", PhysicalFonts.get("Microsoft YaHei.ttf"));
        mlPackage.setFontMapper(fontMapper);
    }

3,问题

如果word当中有表格 表格中的单元格 如果是英文和数字不会换行 中文会自动换行这是一个BUG。

aspose-words
maven依赖

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>15.8.0</version>
</dependency>

特别说明: 需要的是 *.ttc 格式的字体库 如果缺失会出现乱码 小方格。

1,word 导出pdf

    public void outPDF(ByteArrayOutputStream outputStream, HttpServletResponse response, String fileName) throws Exception {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        ExcelUtils.exportPdf(response, fileName);
        //引入字体所在的目录
        FontSettings.setFontsFolder("/tmp/fonts", true);
        //使用字体设置配置加载选项
        LoadOptions loadOptions = new LoadOptions();
        Document doc = new Document(inputStream, loadOptions);
        PdfSaveOptions options = new PdfSaveOptions();
        options.setCompliance(PdfCompliance.PDF_15);
        // 将Word转换为PDF
        doc.save(response.getOutputStream(), options);
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(response.getOutputStream());
    }

2,引入需要的字体

    static {
//        //执行字体
        Process process = null;
        try {
            //正常写 linux命令即可 、比如:mkdir xx/touch b.txt/sh start.sh/sh stop.sh

            String commend = "mkdir -p /tmp/fonts";
            process = Runtime.getRuntime().exec(commend);
            log.info("创建目录" + process.waitFor());

            log.info("拷贝字体");
            FileCopyUtils.copy(new ClassPathResource("font/Microsoft YaHei.ttc").getInputStream(), new FileOutputStream("/tmp/fonts/Microsoft YaHei.ttc"));

            commend = "chmod -R 777 /tmp/fonts";
            process = Runtime.getRuntime().exec(commend);
            log.info("授予权限" + process.waitFor());
//            setFontsSources();
        } catch (Exception e) {
            log.error("设置字体失败", e);
        }
    }

3,问题

aspose-words 是商用的 需要自己在网上找下 license.xml 。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。