使用nodejs和jspdf在pdf中添加png图像

时间:2017-04-09 16:33:45

标签: node.js jspdf

我对p很新,我正在尝试在服务器上创建一个带nodeJs的pdf。

我使用jsPdf

安装了jspdf

由于nodeJs doest支持jspdf中使用的npm install jspdf --save,我添加了

atob并添加了

npm install atob --save

var atob = require('atob'); var PNG = require('png-js'); 中添加png文件需要png-js。 当我尝试使用以下代码

将PNG文件添加到pdf时
jspdf

触发以下错误

let imgData=request.payload.canvasobj;
let pdfContent = new jsPDF();
pdfContent.addImage(imgData, 'PNG',  40, 20, 0, 0);
let data = pdfContent.output('arraybuffer');
let buffer = Buffer.from(data);
let arraybuffer = Uint8Array.from(buffer);
fs.appendFile('./canvas.pdf', new Buffer(arraybuffer), function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log("PDF created");
    }
});

我错过了什么?它适用于JPEG Image。

来自日志的图像数据

Debug: internal, implementation, error
    TypeError: Uncaught error: Cannot read property 'buffer' of undefined
    at Object.jsPDFAPI.processPNG (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\jspdf\dist\jspdf.debug.js:8796:80)
    at Object.jsPDFAPI.addImage (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\jspdf\dist\jspdf.debug.js:4696:50)
    at canvas (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\requestHandler.js:105:16)
    at Object.internals.handler (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\hapi\lib\handler.js:99:36)
    at request._protect.run (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\hapi\lib\handler.js:30:23)
    at internals.Protect.run (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\hapi\lib\protect.js:59:12)
    at exports.execute (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\hapi\lib\handler.js:24:22)
    at each (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\hapi\lib\request.js:382:16)
    at iterate (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\hapi\node_modules\items\lib\index.js:36:13)
    at done (C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\hapi\node_modules\items\lib\index.js:28:25)
Debug: internal, implementation, error
    TypeError: fn is not a function
    at C:\LearningPro\NodeJsProjects\pdfexport-nodejs\node_modules\png-js\png-node.js:246:16
    at Inflate.onEnd (zlib.js:227:5)
    at emitNone (events.js:91:20)
    at Inflate.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)

2 个答案:

答案 0 :(得分:2)

使用过jsPDF库并偶然发现了无效的imageData输入格式的错误,我建议你添加日志以确定imageData的格式,如下所示:

let imgData=request.payload.canvasobj;
console.log(imgData);

确保格式为:

data:image/png;base64,...

其中......是用base64编码的图像数据。

修改 我在本地设置了jsPDF。为了让jspdf包(来自npm)在Node.JS 4.x中工作,我必须执行以下操作:

global.PNG = require('png-js');
global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.atob = require('atob');

然后继续成功创建jsPDF对象。

var fs = require('fs');
var jsPDF = require('jspdf');
var imgData = fs.readFileSync('imgdata').toString();
var pdfContent = new jsPDF();
var ret = pdfContent.addImage(imgData, 'PNG', 40, 20, 0, 0);
var data = new Buffer(pdfContent.output('arraybuffer'));

fs.appendFile('./canvas.pdf', data, function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log("PDF created");
    }
});

我认为我无法复制您的原始问题,因为设置需要在非浏览器环境中进行一些调整。

答案 1 :(得分:0)

wire:click