question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jsPDF Server Side html() function generates empty pdf

See original GitHub issue

I have a scenario, where I need to create a pdf file from html source code on server side. I tried the solution from issue #2248 , and the doc.text() function worked, but the doc.html() function creates an empty pdf and I got the following error message:

html2canvas not loaded.
jspdf.node.min.js:150

(node:7560) UnhandledPromiseRejectionWarning: ReferenceError: document is not defined
warning.js:27
    at r (\node_modules\jspdf\dist\jspdf.node.min.js:150:335)
    at Promise.<anonymous> (\dist\node_modules\jspdf\dist\jspdf.node.min.js:150:1413)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:7560) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

I found this solution, that solved the “html2canvas not loaded” error, but I still get an empty pdf and an UnhandledPromiseRejectionWarning:

(node:7136) UnhandledPromiseRejectionWarning: ReferenceError: document is not defined
warning.js:27
    at r (\node_modules\jspdf\dist\jspdf.node.min.js:150:335)
    at Promise.<anonymous> (\node_modules\jspdf\dist\jspdf.node.min.js:150:1413)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:7136) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
warning.js:27
(node:7136) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This is the code that I tried to run:

global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};

const fs = require('fs')
const html2canvas = require('html2canvas');
window.html2canvas = html2canvas;
const jsPDF = require('jspdf/dist/jspdf.node.min')

// Default export is a4 paper, portrait, using milimeters for units
var doc = new jsPDF()
doc.html('<p>Hello world!</p>');

fs.writeFileSync('./output.pdf', doc.output())

delete global.window;
delete global.navigator;
delete global.btoa;

Is there any way to solve this issue?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:16

github_iconTop GitHub Comments

8reactions
manuelamariacommented, Sep 1, 2020

Thanks for the answer!

Unfortunately I get more errors from jsdom: Error: Not implemented: window.computedStyle(elt, pseudoElt), Error: Not implemented: window.scrollTo

This is my code:

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { jsPDF } = require("jspdf");

var myhtml = `<div><p>test me</p></div>`;
const htmldom = new JSDOM(myhtml);

global.window = htmldom.window;
global.document = window.document;
global.Node = window.Node;

var doc = new jsPDF('p', 'mm', 'a4');
doc.html(document.body, {
    callback: function(d) {
        d.save();
    }
});
doc.save("a4.pdf");
4reactions
muskan3006commented, Feb 5, 2021

I am still facing this issue, do we have a solution now?

Read more comments on GitHub >

github_iconTop Results From Across the Web

jsPDF Server Side html() function generates empty pdf #2805
I have a scenario, where I need to create a pdf file from html source code on server side. I tried the solution...
Read more >
jsPDF Server Side html() function generates empty pdf
I have a scenario, where I need to create a pdf file from html source code on server side. I tried the solution...
Read more >
jsPDF is downloading EMPTY PDF - javascript - Stack Overflow
I want to download that div as a pdf when user click on download button. For this purpose i'm using jsPDF Library. But...
Read more >
HTML to PDF in Javascript using jsPDF with Example Download
Example of JavaScript PDF creation on the client-side using jsPDF JavaScript library by reading and supplying the HTML file content.
Read more >
Component generates PDF-file with one blank page. What's ...
I have added the PDF Generator (jsPDF) component to our Web. I have followed the How to of the component but when I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found