RedHat Linux下PDFMake无法显示重音字符配置排查求助
RedHat Linux下PDFMake生成PDF无法显示葡萄牙语重音字符排查
我使用RedHat Linux系统,系统语言为葡萄牙语,已配置pt_br.UTF-8编码,但通过PDFMake生成PDF时,带重音的葡萄牙语字符无法正常显示。相同的PDF生成脚本在Windows系统上运行完全正常,Linux环境下却出现问题。请帮忙排查是否遗漏了PDFMake的相关配置?
代码头部如下:
let docDefinition = { pageOrientation: 'portrait', pageSize: 'A4', pageMargins: [20, 20, 20, 35], header: function(page, pages) { return { columns: [ { text: 'Página: ' + page.toString() + ' de ' + pages.toString(), encoding: 'utf-8', width: '*', style: { fontSize: 8, alignment: 'right' } } ], margin: [25, 10] } }, styles: { tableExample: { fontSize: 6, margin: [0, 0, 0, 0], border: 0 }, tableHeader: { bold: true, fontSize: 6, color: 'black' } }, content: [ { style: 'tableReport', table: { // 1 1b 2 3 4 5 6 7 8 9 10 11 12 widths: [50, 33, 19, 55, 39, 35, 35, 35, 35, 20, 30, 20, 30], // TOTAL: 414 headerRows: 2, body: [] }, style: { fontSize: 8 } } ], styles: { tableReport: { margin: [0, 0, 0, 0], } } }; rows.push( [ { image: pathLogo, colSpan: 5, width: 85, alignment: 'left', border: [true, true, false, false] },{},{},{},{}, { colSpan: 6, rowSpan: 2, text: [ { text: '\nCotação de preços', encoding: 'utf-8', style: {fontSize: 20} } ], alignment: 'center', style: {bold: true}, border: [true, true, false, false] }, {},{},{},{},{}, { text: [ { text: `Nº\n `, encoding: 'utf-8', fontSize: 12, //16 alignment: 'center' }, { text: (modelRes['ENTNUM'] || ''), encoding: 'utf-8', alignment: 'center', style: { bold: true, fontSize: 12 //16 } }, { text: `\n\nEmissão: ${emiCotacao}`, encoding: 'utf-8', alignment: 'center', style: { bold: true, fontSize: 10 } } ], colSpan: 2, rowSpan: 2, border: [true, true, true, false] },{} ], ); //[... here I create the rows and columns] pdfStream(docDefinition, resolve, reject);
排查方向及解决办法
- 字体缺失:Windows默认预装支持葡萄牙语重音的字体(如Arial),但RedHat Linux通常没有这些字体。PDFMake必须使用包含目标语言字符集的字体才能正确渲染。
- 在RedHat上安装支持UTF-8的通用字体,比如DejaVu Sans:
yum install dejavu-sans-fonts - 在PDFMake的文档定义中指定该字体:
在docDefinition里添加默认样式或在自定义样式中指定字体:let docDefinition = { // 原有配置... defaultStyle: { font: 'DejaVuSans' // 对应安装的字体名称 }, styles: { tableReport: { margin: [0, 0, 0, 0], font: 'DejaVuSans' }, tableHeader: { bold: true, fontSize: 6, color: 'black', font: 'DejaVuSans' } // 其他样式同理 } };
- 在RedHat上安装支持UTF-8的通用字体,比如DejaVu Sans:
- 冗余编码配置:代码中添加的
encoding: 'utf-8'完全多余,PDFMake默认支持UTF-8编码,建议删除所有此类配置,避免潜在冲突。 - 字体路径权限:如果使用自定义字体文件,需确保Linux系统中程序能读取到该文件,路径设置正确且文件权限符合要求。
内容的提问来源于stack exchange,提问作者will The J
相关产品推荐
相关产品推荐

