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

文章目录

概要

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文件增加依赖
xml 复制代码
<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>
java 复制代码
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 复制代码
java.lang.ClassNotFoundException: com.aspose.words.Document

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

xml 复制代码
<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中,然后安装即可

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

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

小结

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

附例子代码

相关推荐
cainiao08060510 分钟前
《Spring Boot 4.0新特性深度解析》
java·spring boot·后端
呆萌很1 小时前
基于 Spring Boot 瑞吉外卖系统开发(十二)
spring boot
爱吃零食的白糖1 小时前
word换行符和段落标记
word
计算机学姐1 小时前
基于SpringBoot的小区停车位管理系统
java·vue.js·spring boot·后端·mysql·spring·maven
小鸡脚来咯2 小时前
请求参数:Header 参数,Body 参数,Path 参数,Query 参数分别是什么意思,什么样的,分别通过哪个注解获取其中的信息
java·spring boot·后端
geovindu2 小时前
vue3: pdf.js 3.4.120 using javascript
开发语言·javascript·vue.js·pdf
添砖Java中3 小时前
深入剖析缓存与数据库一致性:Java技术视角下的解决方案与实践
java·数据库·spring boot·spring·缓存·双写一致性
幽络源小助理4 小时前
懒人美食帮SpringBoot订餐系统开发实现
java·spring boot·后端·美食
源码云商6 小时前
基于Spring Boot + Vue的母婴商城系统( 前后端分离)
java·spring boot·后端
还听珊瑚海吗10 小时前
基于SpringBoot的抽奖系统测试报告
java·spring boot·后端