jsPDF server side (node.js) usage using node-jspdf

前端 未结 5 536
眼角桃花
眼角桃花 2020-12-10 16:38

I am actually trying to include jspdf in server side and then use it for simple pdf generation(simply the text \"Hello world!\")(Go to the url- get the pdf localhost:8080).

5条回答
  •  情书的邮戳
    2020-12-10 17:03

    In extension to the answer provided by Simon Bengtsson:

    I managed to handle even latin-1 characters by sending the output of jsPdf to encoding:

    global.window = {document: {createElementNS: () => {return {}} }};
    global.navigator = {};
    global.btoa = () => {};
    
    var fs = require('fs');
    var jsPDF = require('jspdf');
    var encoding = require('encoding')
    var doc = new jsPDF();
    doc.text("HelloäöüßÄÖÜ©µ®", 10, 10);
    var data = doc.output()
    var buffer = encoding.convert(data, "Latin_1") 
    
    fs.writeFileSync('./document.pdf', buffer);
    
    delete global.window;
    delete global.navigator;
    delete global.btoa;
    

提交回复
热议问题