jsPDF Server Side html() function generates empty pdf
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:3
- Comments:16
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
I am still facing this issue, do we have a solution now?