如何让除HTML,PDF外的文件在web可预览-jodconverter

什么是jodconverter,GitHub上的文档是这么说的:

JODConverter,Java OpenDocument Converter,在不同的办公格式之间转换文档。 它利用Apache OpenOfficeLibreOffice,它们可以说是当今可用的OpenDocument和Microsoft Office格式的最佳免费导入/导出过滤器。

关键是免费兄弟们!!!!!!wps不会技巧的兄弟们还有开vip呢

其中有各种配置,感觉自己学不会是吧,没关系我们有 jodconverter-spring-boot-starter

JodConverter Spring Boot Starter 是一个开源的 Spring Boot 插件,用于将 Office 文档(如 Word、Excel 和 PowerPoint 等)转换为 PDF 格式。它基于 JodConverter 库实现,简化了将 JodConverter 集成到 Spring Boot 应用程序中的过程。

JodConverter 是一个用于将 Office 文档转换为 PDF、HTML、图片等格式的 Java 库。它使用 LibreOffice 或 OpenOffice 作为后端转换引擎,支持多种文档格式的转换。但是,要将 JodConverter 集成到 Spring Boot 应用程序中并不容易,需要进行大量的配置和依赖项管理。

LibreOffice:下载地址

OpenOffice:下载地址

为了简化这一过程,开发者推出了 JodConverter Spring Boot Starter。通过在 Spring Boot 应用程序中添加该插件,就可以轻松地将 JodConverter 集成到应用程序中,无需复杂的配置和依赖项管理。

首先,我们需要在 Spring Boot 应用程序的 pom.xml 文件中添加 jodconverter-spring-boot-starter 依赖项:

<properties>
		<org.jodconverter.version>4.4.7</org.jodconverter.version>
	</properties>
	<dependencies>

		<dependency>
			<groupId>org.jodconverter</groupId>
			<artifactId>jodconverter-core</artifactId>
			<version>${org.jodconverter.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jodconverter</groupId>
			<artifactId>jodconverter-spring-boot-starter</artifactId>
			<version>${org.jodconverter.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jodconverter</groupId>
			<artifactId>jodconverter-local</artifactId>
			<version>${org.jodconverter.version}</version>
		</dependency>

以及springboot相关配置

spring:
  office:
    home: /usr/lib/libreoffice
    ports: 8100
    timeout: 60000
    max-tasks-per-process: 10
    task-executor:
      core-pool-size: 2
      max-pool-size: 10
      queue-capacity: 100
  1. spring.office.home:指定 LibreOffice 或 OpenOffice 的安装目录。JodConverter 使用 LibreOffice 或 OpenOffice 作为后端转换引擎,该参数需要指定相应的安装目录。

  2. spring.office.ports:指定 LibreOffice 或 OpenOffice 的端口号。JodConverter 通过与 LibreOffice 或 OpenOffice 建立连接来执行转换任务,该参数需要指定可用的端口号。

  3. spring.office.timeout:指定转换任务的最大超时时间(以毫秒为单位)。如果转换任务在指定的超时时间内没有完成,将会被终止。

  4. spring.office.max-tasks-per-process:指定每个 LibreOffice 或 OpenOffice 进程可以处理的最大任务数。当达到最大任务数时,将会启动一个新的进程来处理额外的任务。

  5. spring.office.task-executor.core-pool-size:指定用于执行转换任务的线程池的核心线程数量。核心线程是一直保持活跃的线程,即使没有任务需要执行。

  6. spring.office.task-executor.max-pool-size:指定用于执行转换任务的线程池的最大线程数量。最大线程数量是线程池能够创建的最大线程数。

  7. spring.office.task-executor.queue-capacity:指定用于执行转换任务的线程池的任务队列容量。如果任务队列已满,新的任务将会被拒绝执行。

有的同学就会说了,不用启动吗,mysql都要我们自己启动啊

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 

命令给你们了,答案是不需要,springboot会帮我们启动,不懂得朋友可以看看源码

怎么使用那就太简单了,就这么几行。

@AllArgsConstructor
@Service
@Slf4j
public class PdfUtils {

	
	private final DocumentConverter documentConverter;
	

	public void toPDF(File file, String pdf) throws OfficeException {
		log.info("正在转换————————{}",file.getName());
		documentConverter
			.convert(file)
			.to(new File(pdf))
			.as(DefaultDocumentFormatRegistry.PDF)
			.execute();
		log.info("{}转换完成————————{}",file.getName(),pdf);
	}
}

ok,文章结束了~~~~~

但是后期小伙伴把服务弄上服务器又该出问题了。

ps:磊狗我都部署不上去你给我说出问题?

好吧帮人帮到底

写个dockerfile吧

FROM 你的基础镜像

#这个安装包上面有链接
COPY lib/Apache_OpenOffice_4.1.14_Linux_x86-64_install-deb_zh-CN.tar.gz /tmp/

COPY sources.list /etc/apt/sources.list

#这个两个是Windows上的字体,没有字体转换会出现乱码,需要的朋友私信我,我教你,就不放文件地址了你们懒得下载
COPY lib/*.ttf /usr/share/fonts/truetype/
COPY lib/*.ttc /usr/share/fonts/truetype/


RUN apt-get update \
    && apt-get install -y wget libxext6 libfreetype6 iproute2 openjdk-17-jdk \
    && rm -rf /var/lib/apt/lists/*

# 安装字体
RUN fc-cache -f -v

RUN cd /tmp \
    && tar -zxvf Apache_OpenOffice_4.1.14_Linux_x86-64_install-deb_zh-CN.tar.gz \
    && cd zh-CN/DEBS \
    && dpkg -i *.deb \
    && rm -rf /tmp/*


总之,JodConverter Spring Boot Starter 是一个非常实用的 Spring Boot 插件,可以轻松地将 JodConverter 集成到应用程序中,实现 Office 文档的格式转换。如果你正在开发一个需要将 Office 文档转换为 PDF 格式的应用程序,那么 JodConverter Spring Boot Starter 将是一个不错的选择,有任何问题欢迎私信留言。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 17:26:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 17:26:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 17:26:01       87 阅读
  4. Python语言-面向对象

    2024-01-05 17:26:01       96 阅读

热门阅读

  1. UE5.1_Python使用1

    2024-01-05 17:26:01       42 阅读
  2. Linux测试硬盘的读取速度

    2024-01-05 17:26:01       55 阅读
  3. 选择 省市区 组件数据 基于vue3 + elment-plus

    2024-01-05 17:26:01       52 阅读
  4. blender Texture Coordinate Node

    2024-01-05 17:26:01       53 阅读
  5. C++ Optins接口封装设置自动重连

    2024-01-05 17:26:01       52 阅读
  6. ArrayList 与 LinkedList 的选择与应用

    2024-01-05 17:26:01       67 阅读
  7. c++,mutex,unique_lock,recursive_mutex,shared_mutex对比分析

    2024-01-05 17:26:01       47 阅读
  8. 【微服务】微服务详解、模块化开发详解

    2024-01-05 17:26:01       56 阅读
  9. MySQL运维实战(2.3)MySQL的权限体系

    2024-01-05 17:26:01       54 阅读
  10. Hbase 的三个应用

    2024-01-05 17:26:01       47 阅读
  11. 【React】04-关于React Props的实践

    2024-01-05 17:26:01       51 阅读
  12. 如何有效使用 .gitignore 文件

    2024-01-05 17:26:01       54 阅读
  13. Spring aspect 解析

    2024-01-05 17:26:01       52 阅读