SpringBoot word文件转pdf
  TEZNKK3IfmPf 2023年11月14日 24 0

1 首先是引入 jar 包

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

SpringBoot word文件转pdf
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>

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   22天前   48   0   0 java
TEZNKK3IfmPf