【完整可用】使用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>
相关推荐
代码AI弗森8 小时前
PDF OCR + 大模型:让文档理解不止停留在识字
pdf·ocr
小周同学:1 天前
在 Vue2 中使用 pdf.js + pdf-lib 实现 PDF 预览、手写签名、文字批注与高保真导出
开发语言·前端·javascript·vue.js·pdf
Kyln.Wu1 天前
【python实用小脚本-187】Python一键批量改PDF文字:拖进来秒出新文件——再也不用Acrobat来回导
python·pdf·c#
迪尔~3 天前
Apache POI中通过WorkBook写入图片后出现导出PDF文件时在不同页重复写入该图片问题,如何在通过sheet获取绘图对象清除该图片
java·pdf·excel
忆~遂愿3 天前
Python实战教程:PDF文档自动化编辑与图表绘制全攻略
python·pdf·自动化
Rust语言中文社区3 天前
简单好用的在线工具:轻松把图片添加到 PDF
pdf
鲁班AI3 天前
pdf怎么转换成ppt?AI工具与传统方法深度对比
人工智能·pdf·powerpoint
大山运维3 天前
免费专业PDF文档扫描效果生成器
pdf
伊织code4 天前
pdftk - macOS 上安装使用
macos·pdf·pdftk
菜鸟-要努力5 天前
pdf文件转word免费使用几个工具
pdf·word