java word转pdf的几种方法

转载:https://blog.csdn.net/qwert678000/article/details/72770109

最近公司需要以word为模版,填充数据,然后转成pdf。做了一点点研究

1.使用xdocreport进行转(优点效率高,缺点对word格式要求较大,适合对生成pdf要求不高的情况)

 
/**
 * 将word文档, 转换成pdf
 * 宋体:STSong-Light
 *
 * @param fontParam1 可以字体的路径,也可以是itextasian-1.5.2.jar提供的字体,比如宋体"STSong-Light"
 * @param fontParam2 和fontParam2对应,fontParam1为路径时,fontParam2=BaseFont.IDENTITY_H,为itextasian-1.5.2.jar提供的字体时,fontParam2="UniGB-UCS2-H"
 * @param tmp        源为word文档, 必须为docx文档
 * @param target     目标输出
 * @throws Exception
 */
public void wordConverterToPdf(String tmp, String target, String fontParam1, String fontParam2) {
    InputStream sourceStream = null;
    OutputStream targetStream = null;
    XWPFDocument doc = null;
    try {
        sourceStream = new FileInputStream(tmp);
        targetStream = new FileOutputStream(target);
        doc = new XWPFDocument(sourceStream);
        PdfOptions options = PdfOptions.create();
        //中文字体处理
        options.fontProvider(new IFontProvider() {
            public Font getFont(String familyName, String encoding, float size, int style, Color color) {
                try {
                    BaseFont bfChinese = BaseFont.createFont(fontParam1, fontParam2, BaseFont.NOT_EMBEDDED);
                    Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null)
                        fontChinese.setFamily(familyName);
                    return fontChinese;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        });
        PdfConverter.getInstance().convert(doc, targetStream, options);
        File file = new File(tmp);
        file.delete();  //刪除word文件
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(doc);
        IOUtils.closeQuietly(targetStream);
        IOUtils.closeQuietly(sourceStream);
    }
}

2.使用dom4j进行转换,试了下效率较低,而且转换质量还不如xdoreport,故没有继续。也可能是小弟没研究清楚,代码就不粘了

3.使用libreoffice来进行转(效果好,格式便于控制,基本上转出来的pdf和用libreoffice查看看到的word样子已经非常接近了)

最开始网上查用jodconverter来调用openOffice或者libreoffice的服务来转换,试了下也是速度极慢,转一个2页的word需要10s,确实不能忍。

扫描二维码关注公众号,回复: 3883092 查看本文章

然后想到既然jodconverter能调用,那我自己执行libreoffice命令转换不可以吗,之后发现这个思路可行。

步骤:

1.安装libreoffice(linux还需要装unoconv),目前我安装的是5.3.3版本

2.黑窗口直接敲命令,windows:soffice --convert-to pdf  example.docx   linux: doc2pdf example.docx, windows需要添加path系统变量(C:\Program Files\LibreOffice 5\program),不然无法识别soffice命令

3.ok,既然可以这么玩,放到java项目就简单了

public boolean wordConverterToPdf(String docxPath) throws IOException {
    File file = new File(docxPath);
    String path = file.getParent();
    try {
        String osName = System.getProperty("os.name");
        String command = "";
        if (osName.contains("Windows")) {
            command = "soffice --convert-to pdf  -outdir " + path + " " + docxPath;
        } else {
            command = "doc2pdf --output=" + path + File.separator + file.getName().replaceAll(".(?i)docx", ".pdf") + " " + docxPath;
        }
        String result = CommandExecute.executeCommand(command);
        LOGGER.info("result==" + result);
        if (result.equals("") || result.contains("writer_pdf_Export")) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return false;
}

代码亲试可用,欢迎吐槽

写这篇文章后想到应该还有些其他的方法,比如用wps、office来进行转化,若大虾们有更合适的方式请赐教之

源码地址:

https://github.com/AryaRicky/toPdfUtils.git

猜你喜欢

转载自blog.csdn.net/xujingcheng123/article/details/82655715