【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 //更新字体缓存

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

小结

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

附例子代码

相关推荐
罗政3 小时前
[附源码]超简洁个人博客网站搭建+SpringBoot+Vue前后端分离
vue.js·spring boot·后端
Java小白笔记6 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
小哇6666 小时前
Spring Boot,在应用程序启动后执行某些 SQL 语句
数据库·spring boot·sql
luoluoal9 小时前
java项目之企业级工位管理系统源码(springboot)
java·开发语言·spring boot
蜜桃小阿雯9 小时前
JAVA开源项目 校园美食分享平台 计算机毕业设计
java·jvm·spring boot·spring cloud·intellij-idea·美食
计算机学姐10 小时前
基于SpringBoot+Vue的篮球馆会员信息管理系统
java·vue.js·spring boot·后端·mysql·spring·mybatis
程序员大金10 小时前
基于SpringBoot+Vue+MySQL的智能物流管理系统
java·javascript·vue.js·spring boot·后端·mysql·mybatis
徐同保11 小时前
vue 在线预览word和excel
vue.js·word·excel
customer0812 小时前
【开源免费】基于SpringBoot+Vue.JS在线文档管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Flying_Fish_roe12 小时前
Spring Boot-版本兼容性问题
java·spring boot·后端