I'm trying to implement jsPDF on nodejs (server side) but I found out that jsPDF was made for client side applications. I cannot install node-jspdf since it supports only UNIX systems. I also tried this method but it throws an error
'Error in function Object.: global.open is not a function'
The code I tried is fairly simple.
makepdf.js
global.window = { document: { createElementNS: () => { return {} } } };
global.navigator = {};
global.btoa = () => { };
(function ()
{
let fs = require('fs');
var http = require('http');
var firebase = require("firebase-admin");
var jsPDF = require('jspdf');
var serviceAccount = require('./makepdf.json');
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
databaseURL: "https://makepdf-58678.firebaseio.com"
});
makeDoc(jsPDF);
}());
function makeDoc(jsPDF)
{
var doc = new jsPDF();
doc.text(20, 20, 'This is the default font.')
doc.setFont('courier')
doc.setFontType('normal')
doc.text(20, 30, 'This is courier normal.')
doc.setFont('times')
doc.setFontType('italic')
doc.text(20, 40, 'This is times italic.')
var data = doc.output('dataurlnewwindow');
fs.writeFileSync('./document.pdf', data);
}
delete global.window;
delete global.navigator;
delete global.btoa;
via Sanju
No comments:
Post a Comment