+
95
-

pdf-lib.js使用drawText中文出错Error: WinAnsi cannot encode "方" (0x65b9)

pdf-lib.js使用drawText中文出错Error: WinAnsi cannot encode "方" (0x65b9)


网友回复

+
15
-

在使用 pdf-lib.js 库时,如果你遇到中文编码问题,比如 Error: WinAnsi cannot encode "方" (0x65b9),这是因为 WinAnsi 编码不支持中文字符。为了在 PDF 中正确显示中文字符,你需要使用支持中文的字体,比如 SimSun(宋体)或者其他支持中文的字体。

以下是一个示例代码,展示如何在 pdf-lib.js 中使用中文字体:

首先,确保你有一个支持中文的字体文件(例如 SimSun.ttf)。使用 pdf-lib.js 加载字体文件并将其嵌入到 PDF 中。使用嵌入的字体绘制中文字符。
const { PDFDocument, StandardFonts, rgb } = require('pdf-lib');
const fontkit = require('@pdf-lib/fontkit');
const fs = require('fs');

async function...

点击查看剩余70%

我知道答案,我要回答