问题描述
使用 npm 安装 pdfmake
后:
After installing pdfmake
using npm:
npm install pdfmake --save-dev
并使用 webpack
编译包我在运行时出错:
and compiling bundles with webpack
I get an error when running:
pdfmake = require 'pdfmake'
pdfmake.createPdf(doc_definition).download('test.pdf')
说:
pdfmake.createPdf is not a function
我已阅读this 建议,安装了脚本-loader 并将要求更改为:
I've read this suggestion, installed the script-loader and changed the requirement to:
pdfmake = require 'script!pdfmake'
但这会导致更多错误.此外,我不知道需要什么脚本加载器.有什么建议吗?
But that gave even more errors. Besides, I wouldn't know what that script loader would be needed. Any suggestions?
编辑如此处所述,似乎 NPM 安装了 的节点(服务器端)版本pdfmake
而不是浏览器端版本,两者具有完全不同的 API.这应该是浏览器端的正确流程:
EDITAs described here it seems as if NPM installs the node (serverside) version of pdfmake
instead of the browserside version, both of which have completely different API's. This should be the correct flow for the browserside:
npm install pdfmake --save-dev
pdfmake = require 'pdfmake'
pdfmake.createPdf(doc_definition).download('test.pdf')
推荐答案
不支持服务器端方法 createPdf.
On server side method createPdf is not supported.
服务器端 pdfmake Nodejs - TypeError: pdfmake.createPdf 不是函数 #1355
但是我尝试了 var PDF = require('pdfmake/build/pdfmake')
并且错误消失了.
BUT I tried var PDF = require('pdfmake/build/pdfmake')
and the error is gone.
另外我建议你也获取 vfs_fonts
Also I suggest you to get the vfs_fonts
too
var PDF_Fonts=require('pdfmake/build/vfs_fonts')
并像这样设置pdfmake.vfs
PDF.vfs = PDF_Fonts.pdfMake.vfs;
这篇关于PDFmake:pdfmake.createPdf 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!