aspose word转pdf

356 阅读2分钟

关于word转pdf因为项目需要,网上找了很多的教程发现在windows上可以转换,linux上转换不成功。 以下为linux下 docker启动springboot项目转换的过程。

  • 1.将windows下面的字体复制到linux目录下 windows目录为C:\Users\JO-09\AppData\Local\Microsoft\Windows\Fonts 如果word字体为楷体就找到simkai.ttf复制到linux的目录下,我这里复制到/tmp/sign/fonts
  • 2.docker命令添加项目启动时的文件映射 -v /tmp/sign/fonts:/usr/share/fonts/
  • 3.maven的导包
  <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.3</version>
        </dependency>

        <!-- Apache POI -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.3</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.apache.poi</groupId>-->
<!--            <artifactId>poi-ooxml-schemas</artifactId>-->
<!--            <version>5.2.3</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.5.0</version>
<!--            <scope>system</scope>-->
<!--            <systemPath>${project.basedir}/lib/com.aspose.words-15.5.0.jar</systemPath>-->
        </dependency>
        <!-- poi-tl -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.10.0</version>
        </dependency>
        <!-- Apache PDFBox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.25</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-full -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-full</artifactId>
            <version>5.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>2.0.3</version>
        </dependency>

        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
            <version>1.0.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
<!--                <exclusion>-->
<!--                    <groupId>org.apache.poi</groupId>-->
<!--                    <artifactId>ooxml-schemas</artifactId>-->
<!--                </exclusion>-->
            </exclusions>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>
  • 4.resource里面增加一个文件license.xml
<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

5.utils 转换工具WordToPdfUtils

package com.jo.system.utils;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.xiaominge.exception.ParameterRuntimeException;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

/**
 * @program: system_platform
 * @description: word 转pdf
 * @author: xiaominge
 * @create: 2020-12-30 14:50
 **/
@Slf4j
public class WordToPdfUtils {
    private WordToPdfUtils() {
    }

    private static WordToPdfUtils wordToPdfUtils;

    public static WordToPdfUtils getUtils() {
        if (wordToPdfUtils == null) {
            synchronized (WordToPdfUtils.class) {
                if (wordToPdfUtils == null) {
                    wordToPdfUtils = new WordToPdfUtils();
                }
            }
        }
        return wordToPdfUtils;
    }


    /**
     * 多克斯为pdf
     *
     * @param wordInputStream 字输入流
     * @param pdfOutputStream pdf输出流
     *                        当前方法有一个问题 空字符串会被主动加入空格  源码问题
     *                           Chunk chunk = createTextChunk( textContent.isEmpty() ? " " : sbuf.toString(), pageNumber, chunkFont,
     *                                        underlinePatterns, backgroundColor );
     */

    public void docxToPdf(InputStream wordInputStream, OutputStream pdfOutputStream) {
        if (wordInputStream == null) {
            ParameterRuntimeException.throwException("word文件流为空");
        }
        try {
            XWPFDocument xwpfDocument = new XWPFDocument(wordInputStream);
          PdfConverter.getInstance().convert(xwpfDocument, pdfOutputStream, PdfOptions.create());

            wordInputStream.close();
            pdfOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            ParameterRuntimeException.throwException("读取word文件失败,只支持docx文件");
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        WordToPdfUtils utils = WordToPdfUtils.getUtils();
       // utils.docxToPdf(new FileInputStream("D:/桌面/卷内文件1002247741864148992.docx"), new FileOutputStream("D:/桌面/test.pdf"));
        utils.docxToPdf(new FileInputStream("D:\jieou\projectCode\letter-open-api\letter-open-system\src\main\resources\jh522nc.docx") ,
                new FileOutputStream("D:\jieou\projectCode\letter-open-api\letter-open-system\src\main\resources\jh522nc222.pdf"));
    }

    public  boolean getLicense() {
        boolean result = false;
        try {
            // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 转换
     * // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
     *
     * @param saveTo       SaveFormat {@link SaveFormat }
     * @param inputStream  输入流
     * @param outputStream 输出流
     */
    public  void conversion(InputStream inputStream, OutputStream outputStream,int saveTo ) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }
        try {
            // Address是将要被转化的word文档
            Document doc = new Document(inputStream);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(outputStream, saveTo);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw ParameterRuntimeException.throwException("word转pdf失败");
        }
    }

}

6.windows测试

public static void main(String[] args) throws FileNotFoundException {
    WordToPdfUtils utils = WordToPdfUtils.getUtils();
   // utils.docxToPdf(new FileInputStream("D:/桌面/卷内文件1002247741864148992.docx"), new FileOutputStream("D:/桌面/test.pdf"));
    utils.docxToPdf(new FileInputStream("D:\jieou\projectCode\letter-open-api\letter-open-system\src\main\resources\jh522nc.docx") ,
            new FileOutputStream("D:\jieou\projectCode\letter-open-api\letter-open-system\src\main\resources\jh522nc222.pdf"));
}

7.正常打包部署到docker 8.可能遇到的问题:a.aspose.jar下载不了。b.乱码问题(乱码大概率是字体没有导入到linux导致的)

项目源码引用自:gitee.com/xiaomingewo…