0
点赞
收藏
分享

微信扫一扫

【word2pdf】Springboot word转pdf(自学使用)

福福福福福福福福福 15小时前 阅读 1

文章目录

概要

Springboot word 转 pdf

整体介绍

搜了一下,发现了能实现功能的方法有四种

  1. Using itext and opensagres and apache poi
  2. Using Documents4j
  3. Using openoffice nuoil
  4. Using Aspose Word(not free)

具体实现

这里只提供Aspose的实现,因为这个不区分windows还是Linux操作系统。因为试过了,Doc4j需要依赖是Windows的,所以暂时不去研究了

官网

aspose官网

由于是收费的,所以这里的就按照官网的来,百度一搜随便都是jar包。我这里不贴了,毕竟不是免费的,可以自行搜索破解版。
不废话了,上代码
代码很简单的,这里写一个直接写文件的例子

创建一个springboot项目,然后把下载下来的jar包放到resource的lib下
在这里插入图片描述
然后右键这个jar包,然后点击Add as library,点击完了才是截图这样的,否则就只是一个jar包,点击了Add as library目的是为了让项目引入这个jar包。

pom文件增加依赖
<dependency>
    <groupId>com.aspose.words</groupId>
    <artifactId>aspose-words</artifactId>
    <version>19.3</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-19.3.jar</systemPath>
</dependency>
package com.word2pdf.springbootword2pdf.controller;

import com.aspose.words.Document;
import com.aspose.words.ParagraphFormat;
import com.aspose.words.SaveFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * word2pdf
 *
 * @author Rex
 * @since 2024/4/12 11:34
 */
@RestController
@RequestMapping("/pangolin/test")
public class TestController {
    @GetMapping("/word2pdf")
    public String test() throws Exception {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("word/response.docx");
        Document doc = new Document(inputStream);
        ParagraphFormat pf=doc.getStyles().getDefaultParagraphFormat();
        pf.clearFormatting();
        File tempPdfFile = new File("./temp.pdf");
        FileOutputStream os = new FileOutputStream(tempPdfFile);
        doc.save(os, SaveFormat.PDF);
        return "1";
    }

}

遇到的问题

本地运行OK,发布到Linux报错

第一个坎儿,报错没有找到类

java.lang.ClassNotFoundException: com.aspose.words.Document

在这里插入图片描述
这个是因为不是走的maven,自己放在项目的resources的lib下,本地运行可以的,但是打包需要在pom里增加配置

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<!--打包的时候包含资源文件夹下自己的jar包-->
                   <includeSystemScope>true</includeSystemScope>
				<excludes>
					<exclude>
						<groupId>org.projectlombok</groupId>
						<artifactId>lombok</artifactId>
					</exclude>
				</excludes>
			</configuration>
		</plugin>
	</plugins>
</build>

这个为了打包的时候能给这个jar包包含进来。

还是本地OK,但是Linux能运行的,但是中文乱码

好一个中文乱码,这个问题是因为毕竟doc是基于windows的,所以这里是少字体包。
解决方式是,简单来说就是把windows的字体包拷贝到Linux中,然后安装即可

# 首先windows 字体库的路径 : C:\Windows\Fonts
# 在/usr/share/fonts/下新建文件夹 winFonts
cd /usr/share/fonts/winFonts
sudo mkfontscale
sudo mkfontdir //这两条命令是生成字体的索引信息
sudo fc-cache -fv //更新字体缓存

到此问题解决,还是需要有耐心,分析问题,解决问题,当然了也离不开运维同事的配合。

小结

遇到问题了先不要慌,冷静分析,相信自己能行。

附例子代码

举报

相关推荐

0 条评论