看起来像这样question https://github.com/bpampuch/pdfmake/issues/642已经asked https://stackoverflow.com/questions/41314411/create-a-link-in-a-pdf-with-pdfmake-in-server-side-with-node很少几个times https://stackoverflow.com/questions/35677412/server-side-fonts-directory-in-pdfmake-in-meteor-app使用旧版本的 PDFMake,但尚未更新为最新的目录结构。另外,将字体复制到根“fonts”文件夹中也不是很好。
我到底如何获得在 Node.js 上运行的 PDFMake 的服务器端版本 ("pdfmake": "^0.1.31")使用包含的 vfs_fonts.js 文件?
在命令行上使用 npm 安装
npm install pdfmake fs --save
使用以下命令启动 Node.js 应用程序 index.js:
var fonts = {
Roboto: {
normal: 'fonts/Roboto-Regular.ttf',
bold: 'fonts/Roboto-Medium.ttf',
italics: 'fonts/Roboto-Italic.ttf',
bolditalics: 'fonts/Roboto-MediumItalic.ttf'
}
};
var PdfPrinter = require('pdfmake/src/printer');
var printer = new PdfPrinter(fonts);
var dd = {
content: [
'First paragraph',
'Another paragraph'
]
}
var pdfDoc = printer.createPdfKitDocument(dd);
pdfDoc.pipe(fs.createWriteStream('basics.pdf')).on('finish',function(){
//success
});
pdfDoc.end();
打跑并击中:
/usr/local/bin/node index.js
fs.js:640
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open 'fonts/Roboto-Regular.ttf'
at Error (native)
at Object.fs.openSync (fs.js:640:18)
问题似乎在于 font/Roboto... 文件的位置。在客户端,这是通过包含 vfs_fonts.js 文件来解决的。服务器端,我不确定。没有字体文件夹或 .ttf 文件 https://github.com/bpampuch/pdfmake/issues/381包括。流星框架example https://github.com/daupawar/MeteorAsyncPdfmake我发现似乎不适用。
有任何想法吗?全部是官方的examples https://github.com/bpampuch/pdfmake/tree/master/examples引用 src/fonts 文件夹。对于 npm install 服务器模块来说这不是一个好方法。