1.添加aspose-words破解版依赖

        <dependency>
            <groupId>cn.hiacent</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.8.0-jdk16</version>
            <classifier>jdk16</classifier>
        </dependency>

2.在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>


3.新建PdfUtil工具类

public static boolean getLicense() {
        boolean result = false;
        try {
            // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static void doc2Pdf(InputStream inputStream, OutputStream outputStream) throws Exception {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }
​
        if(!(System.getProperty("os.name").toUpperCase().indexOf("WINDOWS")>=0)){
            log.info("---------------设置字体路径--------------------------");
            //这是重点
        FontSettings.setFontsFolder(File.separator + "usr"
                    + File.separator + "share" + File.separator + "fonts" +File.separator + "ttf-dejavu", true);
        }
​
​
        long old = System.currentTimeMillis();
        // 新建一个空白pdf文档
​
        // Address是将要被转化的word文档
        Document doc = new Document(inputStream);
        // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
        doc.save(outputStream, SaveFormat.PDF);
        long now = System.currentTimeMillis();
        log.info("生成共耗时:" + ((now - old) / 1000.0) + "秒");
        outputStream.close();
        inputStream.close();
​
    }

4. 在Dockerfile中添加word文件中用到的字体(例如增加宋体、黑体)

FROM openjdk:8-jdk-alpine
​
RUN echo "http://mirrors.aliyun.com/alpine/v3.6/main" > /etc/apk/repositories \
    && echo "http://mirrors.aliyun.com/alpine/v3.6/community" >> /etc/apk/repositories \
    && apk update upgrade \
    && apk add --no-cache procps unzip curl bash tzdata \
    && apk add ttf-dejavu \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
​
RUN mkdir -p /jeecg-boot
​
WORKDIR /jeecg-boot
​
EXPOSE 8080
​
ADD ./target/jeecg-boot-module-system-2.4.6.jar ./
​
ADD ./fonts/simhei.ttf /usr/share/fonts/ttf-dejavu/
​
ADD ./fonts/simsun.ttc /usr/share/fonts/ttf-dejavu/
​
ENV JAVA_OPTS=""
​
CMD java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar jeecg-boot-module-system-2.4.6.jar
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐