QuestPDF 是一个基于 .Net 开发的开源 PDF 生成工具,允许用户高效生成、管理 PDF文件(截止 2022.12.6 版本,自 2023.4.0 版本后使用需要授权,详情见: https://www.questpdf.com/pricing.html)。QuestPDF 生成PDF的底层逻辑依赖于SkiaSharp(Skia 的 .Net 绑定),而 Skia 在生成PDF报告时会自动将相关字体文件一同嵌入到PDF文件内,因此生成的PDF文件都较大。
Skia 本身支持了字体裁剪方案,由 HalfBuzz 提供。但 Mono 组织在开发 HalfBuzzSharp(HalfBuzz 的 .Net 绑定)时并未支持 HalfBuzz 的字体裁剪功能(详见:PDF output size),因此目前所有由 QuestPDF 生成的 PDF 报告体积,尤其是中文报告体积都远超用户预期。
经过查找,目前可行的方案为在打印报告前先获取到报告内的所有文本信息,使用字体裁剪工具进行裁剪,然后将裁剪后的字体作为目标字体输出。即可在最大程度上降低 QuestPDF 体积问题(由于字体裁剪仅降低字体文件的体积,PDF 报告依然会包含字体文件,因此相较于不包含字体文件的 PDF 报告,体积依旧较大)。实现方案只要有以下几种:
1. 使用 SfntTool 工具
SfntTool 是 Sfntly 内置的工具,是一个 Jar 包,在使用前需要确保用户环境下已安装 Java 运行环境。基础语句如下:
java -jar sfnttool.jar -s '所需文本内容' source.ttf subset.ttf
实际使用时可搭配 Process 类或 CliWarp 类库使用,由于该方法会造成每次都要重复加载解析原始字体,无法高效重复利用。因此不推荐使用,亦不展开说明。
2 使用 Sfntly 类库
Sfntly 是由 Google 开发的开源库,用于使用、编辑和创建基于sfnt容器的字体(例如 OpenType、TrueType)。该类库有 Java 版本和 C++ 版本,在实际使用中会发现 C++ 版本生成的字体文件体余 Java 版本所生成的字体文件相比,前者的体积要远大于后者,这是由于:java版本生成的是一个全新的字体文件,只包含所需字符的各种属性。而C++版本仍然保留源文件的框架,去掉了不要的字符glyph,保留了需要的字符glyph(详情见C++版本的sfntly库使用示例)。
由于 Sfntly C++ 版本使用需要同时掌握 C++ 与 CppSharp(C++/CLI 仅支持 Windows 平台,不建议使用),综合成本较高,同时 Github 上 已有用户上传 Sfntly Java 版的 .Net 翻译版本,综合而言使用 IKVM.Net 将 Java 版 Sfntly 移植到 .Net 平台更为便捷。
2.1 下载编译相关类库
下载相关工程,使用 Visual Studio 生成编译,源工程下载地址:https://github.com/Dogwei/sfntly.net。
2.2 新建裁剪函数类
sfntly.net 默认的裁剪函数方法:SfntTool.subsetFontFile()
, 每次使用都需要重新解析源字体,通常情况下源字体是固定不变的。为了高效复用,减少重复加载源字体库的性能损失,不直接使用该函数,新建以下类对象代替:
public class SfntToolEx
{
private IList<CMapTable.CMapId> cmapIds = null;
private Subsetter subsetter = null;
private FontFactory fontFactory = null;
private Font sourceFont = null;
public SfntTool(string inputFile)
{
using (var fis = File.OpenRead(inputFile))
{
byte[] fontBytes = new byte[(int)fis.Length];
fis.Read(fontBytes);
Font[] fontArray = null;
FontFactory fontFactory = FontFactory.getInstance();
fontArray = fontFactory.loadFonts(fontBytes);
sourceFont = fontArray[0];
}
fontFactory = FontFactory.getInstance();
cmapIds = new List<CMapTable.CMapId>
{
CMapTable.CMapId.WINDOWS_BMP
};
subsetter = new RenumberingSubsetter(sourceFont, fontFactory);
subsetter.setCMaps(cmapIds, 1);
ISet<Integer> removeTables = new HashSet<Integer>
{
// Most of the following are valid tables, but we don't renumber them yet, so strip
Tag.GDEF,
Tag.GPOS,
Tag.GSUB,
Tag.kern,
Tag.hdmx,
Tag.vmtx,
Tag.VDMX,
Tag.LTSH,
Tag.DSIG,
Tag.vhea,
// AAT tables, not yet defined in sfntly Tag class
Tag.intValue(new char[] { 'm', 'o', 'r', 't' }),
Tag.intValue(new char[] { 'm', 'o', 'r', 'x' }),
Tag.fpgm,
Tag.prep,
Tag.cvt,
Tag.hdmx,
Tag.VDMX,
Tag.LTSH,
Tag.DSIG,
Tag.vhea
};
subsetter.setRemoveTables(removeTables);
}
public void subsetFontFile(string outputFile, string subsetString)
{
var directoryPath = Path.GetDirectoryName(outputFile);
if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); }
using (var stream = File.Create(outputFile))
{
IList<Integer> glyphs = GlyphCoverage.getGlyphCoverage(sourceFont, subsetString);
subsetter.setGlyphs(glyphs);
var newFont = subsetter.subset().build();
fontFactory.serializeFont(newFont, stream);
}
}
}
2.3 调用生成裁剪字体文件
var sfntTool = new SfntTool("C:/Windows/Fonts/simFang.ttf");
sfntTool.subsetFontFile(@$"C:/Users/administrator/Desktop/trim.ttf", "自定义文本");
2.4 设置 QuestPDF 引用自定义字体库
2.4.1 首先注册自定义字体文件:
using (var steam = File.OpenRead("C:/Users/administrator/Desktop/trim.ttf"))
{
QuestPDF.Drawing.FontManager.RegisterFontWithCustomName("trim", steam);
// 较低版本使用以下语句
// QuestPDF.Drawing.FontManager.RegisterFontType("trim", steam);
}
2.4.2 生成 PDF 文件
using (var steam = File.Create("C:/Users/administrator/Desktop/test.pdf"))
{
Document.Create(document =>
{
document.Page(page =>
{
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
page.PageColor(Colors.White);
page.DefaultTextStyle(x => x.FontFamily("trim").FontSize(10)); //设置字体
// ******
}
}).GeneratePdf(steam);
}