Jasmine.MarkdownToPdf 6.0.2

dotnet add package Jasmine.MarkdownToPdf --version 6.0.2
                    
NuGet\Install-Package Jasmine.MarkdownToPdf -Version 6.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Jasmine.MarkdownToPdf" Version="6.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Jasmine.MarkdownToPdf" Version="6.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Jasmine.MarkdownToPdf" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Jasmine.MarkdownToPdf --version 6.0.2
                    
#r "nuget: Jasmine.MarkdownToPdf, 6.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Jasmine.MarkdownToPdf@6.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Jasmine.MarkdownToPdf&version=6.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Jasmine.MarkdownToPdf&version=6.0.2
                    
Install as a Cake Tool

Jasmine.MarkdownToPdf

HTML 转 PDF 转换库核心模块,提供两种主要使用方式。

两种使用方式

方式一:JasmineConverter(推荐)

适用场景:快速将 HTML 字符串转换为 PDF,无需手动构建文档结构。

特点:

  • 简单易用,一行代码完成转换
  • 自动解析 HTML 并构建文档模型
  • 适合大多数日常场景

基本用法:

using Jasmine.MarkdownToPdf;

// 创建转换器(默认宋体,11pt)
var converter = new JasmineConverter();

// HTML 内容
string html = @"
<p style='text-align: center;'><strong>标题</strong></p>
<p style='text-indent: 2em; line-height: 1.5;'>正文内容</p>
<table>
    <tr><td>单元格 1</td><td>单元格 2</td></tr>
</table>
";

// 同步转换
byte[] pdfBytes = converter.ConvertHtmlToPdf(html);
System.IO.File.WriteAllBytes("output.pdf", pdfBytes);

// 异步转换
byte[] pdfBytes = await converter.ConvertHtmlToPdfAsync(html);

自定义字体:

// 使用微软雅黑,12号字
var converter = new JasmineConverter("Microsoft YaHei", 12f);

// 使用黑体
var converter = new JasmineConverter("SimHei");

直接保存到文件:

// 同步保存
converter.ConvertHtmlToPdfToFile(html, "output.pdf");

// 异步保存
await converter.ConvertHtmlToPdfToFileAsync(html, "output.pdf");

方式二:PdfConverter(进阶)

适用场景:需要精细控制文档结构,手动构建文档内容。

特点:

  • 完全控制文档结构
  • 支持复杂表格(colspan/rowspan)
  • 适合生成结构化报告

基本用法:

using Jasmine.MarkdownToPdf.Converters;
using Jasmine.MarkdownToPdf.Models;

// 创建 PDF 转换器
var pdfConverter = new PdfConverter("Microsoft YaHei", 12f);

// 手动构建文档
var document = new JasmineDocument();

// 添加段落
var paragraph = new JasmineParagraph
{
    Alignment = TextAlignment.Center,
    FirstLineIndent = 0,
    LineSpacing = 1.5,
    TextRuns = new List<JasmineTextRun>
    {
        new JasmineTextRun { Text = "文档标题", IsBold = true, FontSize = 16f }
    }
};
document.Paragraphs.Add(paragraph);

// 添加正文
var body = new JasmineParagraph
{
    Alignment = TextAlignment.Justify,
    FirstLineIndent = 22,
    LineSpacing = 1.5,
    TextRuns = new List<JasmineTextRun>
    {
        new JasmineTextRun { Text = "正文内容..." }
    }
};
document.Paragraphs.Add(body);

// 添加表格
var table = new JasmineTable();
// ... 添加表格行和单元格
document.Tables.Add(table);

// 转换并保存
pdfConverter.Convert(document, "output.pdf");

复杂表格示例(支持 colspan 和 rowspan):

var table = new JasmineTable();

// 合并单元格示例
var row1 = new JasmineRow();
row1.Cells.Add(new JasmineCell 
{ 
    ColSpan = 3,
    Paragraphs = new List<JasmineParagraph> 
    { 
        new JasmineParagraph { TextRuns = new List<JasmineTextRun> { new JasmineTextRun { Text = "跨3列表头", IsBold = true } } } 
    } 
});
table.Rows.Add(row1);

var row2 = new JasmineRow();
row2.Cells.Add(new JasmineCell { RowSpan = 2, Paragraphs = ... });
row2.Cells.Add(new JasmineCell { Paragraphs = ... });
row2.Cells.Add(new JasmineCell { Paragraphs = ... });
table.Rows.Add(row2);

document.Tables.Add(table);

项目结构

Jasmine.MarkdownToPdf/
├── Converters/        # PDF 转换模块
│   ├── PdfConverter.cs        # PDF 转换核心
│   ├── PdfBuilderHelper.cs    # 构建辅助工具
│   └── FontManager.cs         # 字体管理
├── Models/            # 数据模型
│   ├── JasmineDocument.cs     # 文档模型
│   ├── JasmineParagraph.cs    # 段落模型
│   ├── JasmineTable.cs        # 表格模型
│   ├── JasmineRow.cs          # 表格行模型
│   ├── JasmineCell.cs         # 单元格模型
│   └── JasmineTextRun.cs      # 文本片段模型
├── Parsers/           # HTML 解析模块
│   └── HtmlParser.cs          # HTML 解析器
├── Fonts/             # 字体资源
│   ├── SimSun.ttf             # 宋体
│   ├── MicrosoftYaHei.ttf     # 微软雅黑
│   └── SimHei.ttf             # 黑体
├── IJasmineConverter.cs       # 转换器接口
└── JasmineConverter.cs        # 转换服务入口

核心接口

IJasmineConverter

public interface IJasmineConverter
{
    // 同步转换 HTML 到 PDF
    byte[] ConvertHtmlToPdf(string htmlContent);
    
    // 异步转换 HTML 到 PDF
    Task<byte[]> ConvertHtmlToPdfAsync(string htmlContent, CancellationToken cancellationToken = default);
    
    // 同步转换并保存到文件
    void ConvertHtmlToPdfToFile(string htmlContent, string outputPath);
    
    // 异步转换并保存到文件
    Task ConvertHtmlToPdfToFileAsync(string htmlContent, string outputPath, CancellationToken cancellationToken = default);
}

PdfConverter

public class PdfConverter
{
    // 构造函数
    public PdfConverter(string customFontFamily = null, float defaultFontSize = 11f);
    
    // 转换并保存到文件
    void Convert(JasmineDocument document, string outputPath);
    
    // 异步转换并保存
    Task ConvertAsync(JasmineDocument document, string outputPath, CancellationToken cancellationToken = default);
    
    // 转换为字节数组
    byte[] ConvertToBytes(JasmineDocument document);
    
    // 异步转换为字节数组
    Task<byte[]> ConvertToBytesAsync(JasmineDocument document, CancellationToken cancellationToken = default);
}

支持的字体

字体名称 内部标识 说明
宋体 SimSun 默认字体
微软雅黑 Microsoft YaHei 常用无衬线字体
黑体 SimHei 粗体显示

输出格式

  • 页面大小:A4
  • 页边距:2 厘米(上下左右)
  • 默认字体:宋体,11pt
  • 页面方向:纵向

选择哪种方式?

场景 推荐方式 原因
快速转换 HTML JasmineConverter 简单直接,自动解析
生成结构化报告 PdfConverter 完全控制文档结构
Web API 集成 JasmineConverter 接口简洁,易于集成
复杂表格处理 PdfConverter 支持 colspan/rowspan
批量文档生成 两者均可 根据复杂度选择

许可证

本项目采用 MIT 许可证。详情请查看项目根目录的 LICENSE 文件。

贡献指南

欢迎贡献代码!请遵循以下流程:

  1. Fork 仓库
  2. 创建功能分支
  3. 实现功能或修复问题
  4. 编写测试
  5. 提交代码(遵循约定式提交规范)
  6. 创建 Pull Request

问题反馈

如有问题或建议,请在仓库的 Issue 页面提交。提交时请包含:

  • 问题描述
  • 复现步骤
  • 环境信息
  • 相关代码示例
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.0.2 121 5/30/2026
6.0.1 137 5/29/2026 6.0.1 is deprecated because it has critical bugs.
1.0.0 152 5/28/2026 1.0.0 is deprecated because it has critical bugs.

一个轻量级的 HTML 转 PDF 转换库,基于 .NET 6.0 开发,支持段落、表格、文本样式等丰富功能。

功能特性
HTML 解析:支持解析标准 HTML 标签,包括 <p>、<table>、<strong>、<em> 等
表格支持:完整支持 colspan 和 rowspan 属性的复杂表格
文本样式:支持粗体(Bold)、斜体(Italic)、下划线(Underline)等文本样式
对齐方式:支持左对齐、居中、右对齐、两端对齐等多种对齐方式
首行缩进:支持段落首行缩进设置
行间距:支持自定义段落行间距
字符间距:支持自定义字符间距
字体扩展:支持自定义字体配置,内置宋体、微软雅黑、黑体三种中文字体
PDF 生成:基于 QuestPDF 库生成高质量 A4 格式 PDF 文档