2017-05-03 57 views
2

我想以编程方式打印一些pdf文档。我现在想花几个小时来使this PDFkit library与webpack一起工作。如何在webpack中使用PDFkit库?

我已经走了形式:

Can't resolve 'fs' in ..

fs.readFileSync is not a function

然后警告

[BABEL] Note: The code generator has deoptimised the styling of "E:/MyProjects/accountingsystem/node_modules/brotli/dec/dictionary-da 
ta.js" as it exceeds the max of "500KB". 

然后

require is not defined - 我卡在这里。所有这些错误都来自图书馆本身。

我刚才一个文件 - app.js只有代码一行,这就是:

const PDFDocument = require('pdfkit');

我最后webpack.config.js看起来是这样的:

module.exports = { 
// devtool: 'source-map', 

entry: './src/app.js', 
output: { 
    path: path.resolve (__dirname, "dist"), 
    filename: "bundle.js" 
}, 

// node: { 
//  console: true, 
//  fs: 'empty', 
//  net: 'empty', 
//  tls: 'empty' 
// }, 

// i've added 'target' - following the advice form some github comments. 
target: 'node', 
module : { 
    rules : [ 
     { test: /\.js$/, loader: 'babel-loader' }, 
     { 
      test : /\.html$/, 
      use : [ 'html-loader' ] 
     }, 

     // then i've added this 2 loaders also: 
     { test: /\.json$/, loader: 'json-loader' }, 
     { test: /pdfkit|png-js/, loader: "transform-loader?brfs" } 
    ] 
}, 
plugins:[ 
    new HtmlWebpackPlugin ({ 
      template : `src/app.html` 
    }) 
], 

};

这实际上是一个1行的应用程序,我现在打了几个小时的墙。我看到很多用户在使用fs核心模块和webpack时遇到了问题 - 我尝试了所有我能找到的解决方案。它能有多难?这里究竟发生了什么?感谢任何见解。

回答

1

这有可能使其与工作的WebPack。

pdfmake是围绕pdfkit的包装与运作的WebPack。正如你所看到的,webpack.config.js是一种哈克:

https://github.com/bpampuch/pdfmake/blob/master/webpack.config.js

就个人而言,我结束了pdfmake,它提供了一个内置的js文件的工作。 我你这样做,你必须调整你的webpack.config,使用resolve.alias场指向pdfmake建js文件。