SpringBoot word文件转pdf

Posted 早起的年轻人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot word文件转pdf相关的知识,希望对你有一定的参考价值。

1 首先是引入 jar 包

jar 包资源在这里 大家可以下载使用链接描述-这个是需要积分的 大家可以回复小编 发给大家


maven 中引用外包的jar包,在你的 pom 文件中加载 jar 内容

  <dependency>
      <groupId>com.aspose</groupId>
      <artifactId>aspose-words-jdk16</artifactId>
      <version>15.8.0</version>
      <scope>system</scope>
      <systemPath>$basedir/src/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
  </dependency>

当使用 maven 来打包jar 包发布时,需要注意配置将外部的jar包进行打包 includeSystemScope 标签配置为 true

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
           
        </plugins>
    </build>

2 word 转 pdf 核心

    /**
     * 
     * @param inPath 输入的 word 文件地址 
     * @param outPath 输出的 pdf 文件地址
     * @throws Exception
     */
    public void doc2pdf(String inPath, String outPath) throws Exception 
        
        if (!getLicense()) 
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            throw new Exception("验证License 不通过");
        
      
        long old = System.currentTimeMillis();

        // 创建 文档 Document
        Document doc = new Document(inPath);
        // 新建一个空白pdf文档
        File file = new File(outPath);
        //输出流
        FileOutputStream os = new FileOutputStream(file);
        // 将文档保存为 pdf 格式的文件
        doc.save(os, SaveFormat.PDF);
        // EPUB, XPS, SWF 相互转换
        long now = System.currentTimeMillis();
        logger.error("转换完成 共耗时 " + ((now - old) / 1000.0) + "秒");
    

3 签名文件信息加载

    public boolean getLicense() 
        boolean result = false;
        try 
            Resource resource = new ClassPathResource("pdf/license.txt");
            License aposeLic = new License();
            aposeLic.setLicense(resource.getInputStream());
            result = true;
         catch (Exception e) 
            e.printStackTrace();
            throw new RRException("加载验证文件失败 " + e.getMessage());
        
        return result;
    

license.txt中保存着签名文件

<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>

以上是关于SpringBoot word文件转pdf的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot word文件转pdf

记一下Linux环境SpringBoot 用OpenOffice Word转PDF

Word怎么转成pdf格式,word转pdf的小技巧

[pdf转word]PDF转Word 不会转?最优PDF转换成Word方法在这里

[word转pdf]Word怎么转换成PDF?这个Word转PDF方法一定不要错过

怎样将pdf的文件转成word文件