简体   繁体   English

在MEAN-stack项目的客户端中包含pdfmake

[英]Including pdfmake in client side of MEAN-stack project

I have a website built on the MEAN.io stack and am trying to generate PDF files on the client side with pdfmake. 我有一个基于MEAN.io堆栈构建的网站,正在尝试使用pdfmake在客户端生成PDF文件。 One can install pdfmake with bower (it looks like they used browserify to generate the client-side version). 可以使用bower安装pdfmake(看来他们使用browserify生成了客户端版本)。

I am struggling to get it to work. 我正在努力使其工作。 Either the injection of pdfmake does not work (I assume it cannot be found) or the pdfmake object is undefined (if I don't add pdfmake as a dependancy). 要么注入pdfmake不起作用(我假设无法找到),要么pdfmake对象未定义(如果我不添加pdfmake作为依赖项)。 I assume pdfmake needs to be packaged in some way to make it accessible, but I don't know how. 我认为pdfmake需要以某种方式打包以使其可访问,但我不知道如何。

In config/assets.json I added: 在config / assets.json中,我添加了:

"bower_components/pdfmake/build/pdfmake.js"

In the HTML corresponding to the JS file in which I want to use pdfmake I added: 在与要使用pdfmake的JS文件相对应的HTML中,我添加了:

<script src="bower_components/pdfmake/build/pdfmake.js"></script>
<script src="bower_components/pdfmake/build/vfs_fonts.js"></script>

I have set up a basic version on Github . 我已经在Github上设置了基本版本。 I would really appreciate it if someone could explain to me how to make it possible to make pdfmake available for use in packages/system/public/controllers/index.js, for example. 如果有人可以向我解释如何使pdfmake可以在package / system / public / controllers / index.js中使用,我将不胜感激。

I was recently have a similar problem. 我最近有一个类似的问题。 It seems to come from the fact that pdfmake isn't yet "browserfiable." 似乎是因为pdfmake尚未“可浏览浏览”。 After much troubleshooting, I was able to get it working through simply including the two client-side scripts pdfmake.min.js and vfs_fonts.js via script tags in my build directory. 经过大量的故障排除之后,我能够通过构建目录中的脚本标记简单地包括两个客户端脚本pdfmake.min.jsvfs_fonts.js来使其工作。 That's it. 而已。

Try moving the two script tags out of the bower_components directory and into your build directory. 尝试将两个脚本标记移出bower_components目录,并移入构建目录。 Make sure they're before your bundle.js , or generally whatever scripts need pdfmake. 确保它们在您的bundle.js或任何其他需要pdfmake的脚本之前。

I don't fully understand how bundled scripts are able to see the global objects created by these two files, but I think it's because they're setting pdfMake to the window object: 我不完全了解捆绑的脚本如何查看这两个文件创建的全局对象,但是我认为这是因为它们将pdfMake设置为窗口对象:

(The vfs_fonts.js file starts like this:) vfs_fonts.js文件开始如下:)

window.pdfMake = window.pdfMake || {}; window.pdfMake.vfs

Since Browserify sets the global object to window , this approach seems to work. 由于Browserify将全局对象设置为window ,因此这种方法似乎可行。 (Though I don't fully understand why... see this Github issue on their repo for more explanation and the deglobalify npm package.) (尽管我不完全理解为什么…… 在其回购中查看此Github问题 ,以获取更多说明和deglobalize npm软件包。)

Here's another relevant post I was able to find. 这是我能够找到的另一篇相关文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM