【完整可用】使用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>
相关推荐
2501_928094651 天前
PDF Reader 编辑阅读工具(Mac中文)
macos·pdf·mac
非凡ghost1 天前
PDF24 Creator:免费的多功能PDF工具
学习·pdf·生活·软件需求
IT周小白1 天前
Apache PDFBox 与 spire.pdf for java 使用记录
java·pdf
CodeCraft Studio1 天前
借助 TX Text Control:在 .NET C# 中使用 PDF/A-3b 创建可信文档容器
大数据·pdf·数字签名·tx text control·pdf/a-3b·pdf文档开发
嘀咕博客1 天前
PDF.AI-与你的PDF文档对话
人工智能·pdf
私人珍藏库1 天前
[Windows] PDF工具箱 PDF24 Creator 11.28.0
windows·pdf
星河顾盼2 天前
【微信小程序预览文件】(PDF、DOC、DOCX、XLS、XLSX、PPT、PPTX)
微信小程序·pdf·powerpoint
猫头虎2 天前
猫头虎AI分享:无需OCR,基于ColQwen2、Qwen2.5和Weaviate对PDF进行多模态RAG的解决方案
microsoft·ai·pdf·aigc·ocr·ai编程·ai-native
Metaphor6922 天前
Java 压缩 PDF 文件大小:告别臃肿,提升效率!
java·经验分享·pdf
bpmh2 天前
.vsdx文件转pdf、word、ppt等文件在线分享(免费版)
pdf