【完整可用】使用openhtmltopdf生成PDF(带SVG)

文章目录

前言

AI和网上都是跑不起来或者版本过低的,还有各种BUG的。本文都是查阅官方文档得出的。如果你能跑起来请给个大大的赞

OpenHTMLToPDF 简介

OpenHTMLToPDF 是一个纯 Java 库,基于 Flying Saucer 和 Apache PDFBox 2,支持将格式良好的 XML/XHTML(甚至一些 HTML5)文档渲染为 PDF 文件。​它支持 CSS 2.1 及其后续标准进行布局和格式化,并能够处理 SVG 图像。​

maven配置依赖

xml 复制代码
<!-- OpenHTMLToPDF 核心库 -->
        <dependency>
            <groupId>at.datenwort.openhtmltopdf</groupId>
            <artifactId>openhtmltopdf-core</artifactId>
            <version>1.1.4</version>
        </dependency>
        <!-- PDFBox 渲染器 -->
        <dependency>
            <groupId>at.datenwort.openhtmltopdf</groupId>
            <artifactId>openhtmltopdf-pdfbox</artifactId>
            <version>1.1.4</version>
        </dependency>
        <dependency>
            <groupId>at.datenwort.openhtmltopdf</groupId>
            <artifactId>openhtmltopdf-svg-support</artifactId>
            <version>1.1.4</version>
        </dependency>
        <!-- Batik 核心库 -->
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-dom</artifactId>
            <version>1.14</version> <!-- 与 OpenHTMLToPDF 兼容的版本 -->
        </dependency>

        <!-- Batik 常量库 -->
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-constants</artifactId>
            <version>1.14</version> <!-- 与 OpenHTMLToPDF 兼容的版本 -->
        </dependency>

        <!-- Batik SVG 生成器 -->
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-svggen</artifactId>
            <version>1.14</version> <!-- 与 OpenHTMLToPDF 兼容的版本 -->
        </dependency>

字体文件

阿里可商用字体下载路径

HTML使用字体文件

html 复制代码
<body style=\"font-family: 'PuHuiTi'\">

demo代码

java 复制代码
public class SvgHtmlToPdfConverter {
    //分页 <div style='page-break-before: always;'></div>
    public static void convertHtmlToPdf(String htmlContent, String pdfPath) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(pdfPath)) {
            PdfRendererBuilder builder = new PdfRendererBuilder();
            // 正确写法:类路径绝对路径(需以 "/" 开头)
            // 1. 获取资源 URL
            builder.useFont(
                    () -> SvgHtmlToPdfConverter.class.getResourceAsStream("/fonts/Alibaba-PuHuiTi-Regular.ttf"),
                    "PuHuiTi", 12, PdfRendererBuilder.FontStyle.NORMAL, true
            );
            builder.toStream(fos);
            builder.useFastMode();
            builder.withHtmlContent(
                    htmlContent,
                    null);
            builder.useSVGDrawer(new BatikSVGDrawer());
            // 4. 输出到 PDF
            builder.run();

        }

    }
    public static void main(String[] args) {
        try {
            convertHtmlToPdf("html代码.........", "D:\\output.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 }

其他

资源放置截图

防止maven编译字体文件

xml 复制代码
<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<excludes>
					<exclude>**/*.ttf</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<includes>
					<include>**/*.ttf</include>
				</includes>
			</resource>
		</resources>
相关推荐
阿幸软件杂货间3 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
reembarkation3 天前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
reembarkation3 天前
vue-pdf 实现blob数据的预览
javascript·vue.js·pdf
Light603 天前
领码方案|Linux 下 PLT → PDF 转换服务超级完整版:异步、权限、进度(一气呵成)
linux·spring boot·pdf·gpcl6/ghostpcl·s3/oss·权限与审计·异步与进度
伟贤AI之路3 天前
【分享】中小学教材课本 PDF 资源获取指南
人工智能·pdf
东风西巷4 天前
PDFgear:免费全能的PDF处理工具
前端·pdf·软件需求
Sunny_yiyi4 天前
Java根据模版导出PDF文件
java·开发语言·pdf
小*-^-*九5 天前
php 使用html 生成pdf word wkhtmltopdf 系列2
pdf·html·php
千册5 天前
pyside6 的pdf显示测试 -- 01
开发语言·python·pdf
qq_172805595 天前
Go 语言 PDF 生成库综合比较与实践指南
开发语言·golang·pdf