我有下面的代码用于测试驱动PDFmake。我对字体文件的位置有一个问题。我看到的文档似乎表明,在调入vfs_fonts文件之后,我应该能够看到它们。然而,对我来说,情况并非如此。
function createPdf(assessmentId: string): string {
const pdfMake = require('pdfmake/build/pdfmake.js');
const pdfFonts = require('pdfmake/build/vfs_fonts.js');
pdfMake.vfs = pdfFonts.pdfMake.vfs;
//required font setup, requires that you link to the fonts shipped with npm
const fontDescriptors = {
Roboto: {
normal: 'Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-Italic.ttf',
}
};
const termpaper = new PdfLayout();
const docDefinition = termpaper.layout
const printer = new Printer(fontDescriptors);
//get a reference to the PdfKit instance, which is a streaming interface
const pdfDoc = printer.createPdfKitDocument(docDefinition);
return "pdflocation";
}当执行这段代码时,我会得到这个错误。
错误: 没有这样的文件或目录,在错误处打开“Roboto-Medium.ttf”(本机)在(/user_code/node_modules/pdfmake/node_modules/fontkit/index.js:43:19) at Function.PDFFont.open (/user_code/node_modules/pdfmake/node_modules/pdfkit/js/font.js: at Object.fontkit.openSync Object.fs.readFileSync (fs.js:642:18) at Object.fs.readFileSync (fs.js:510:33)14:24)在(/user_code/node_modules/pdfmake/node_modules/pdfkit/js/mixins/fonts.js:39:28) at FontProvider.provideFont (/user_code/node_modules/pdfmake/src/fontProvider.js:49:58) at /user_code/node_modules/pdfmake/src/textTools.js:258:27 at Array.forEach (原生) at计量(/user_code/node_modules/pdfmake/src/textTools.js:240:13)
要正确找到这些字体文件,我需要做些什么?
发布于 2018-02-26 13:09:36
我也和这个做过斗争。我找到的解决方案实际上是在我的项目中包含字体文件。列出的文件位于项目根目录下的“字体”文件夹中,我的代码通过uri引用它们:
const fonts = {
Roboto: {
normal: 'fonts/Roboto-Regular.ttf',
bold: 'fonts/Roboto-Medium.ttf',
italics: 'fonts/Roboto-Italic.ttf',
bolditalics: 'fonts/Roboto-MediumItalic.ttf'
}
};我从这里下载了字体文件。
希望这能有所帮助。
发布于 2019-12-27 11:24:43
您也可以这样使用,在我的例子中,我的字体,控制器,助手目录在工作空间目录中。
const path = require('path'); // path is give you a working directory path.resolve() and you can give your font file path.
const fontDescriptors = {
Roboto: {
normal: path.resolve('./fonts/Roboto-Regular.ttf'),
bold: path.resolve('./fonts/Roboto-Medium.ttf'),
italics: path.resolve('./fonts/Roboto-Italic.ttf'),
bolditalics: path.resolve('./fonts/Roboto-MediumItalic.ttf')
}
}发布于 2018-02-26 12:31:09
有类似的问题,有一次,你需要给出准确的字体位置。
它还在字体之前标记了通知。
//必需的字体设置,要求链接到npm附带的字体
在以下内容上尝试完整的目录路径:
normal: '/Exampledir/pdfmake/Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-Italic.ttf',或者是缺少了
const pdfFonts =需要量(“pdfmake/
例如:
const pdfFonts =需要量(“/pdfmake/
它很简单,因为error表示它无法从那个位置找到字体,用简单的cd ./ /然后是cd /pdfmake/ etc和ls来检查这个位置。比较配置文件并使其正常工作。
https://stackoverflow.com/questions/48835055
复制相似问题