-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jsPDF Server Side html() function generates empty pdf #2805
Comments
With the new release planned, running jsPDF in node will work much more seamless. You can try the pull request #2804. However, html2canvas requires some DOM APIs in order to work. You could try jsdom. It would be very great, if you could test the pull request together with jsdom and see if that works. If so, we can add a node/html2canvas example to the examples. |
Thanks, I'll try it. |
I tried the pull request and jsdom, but they didn't solve the issue. Using jsdom gave the following error (even though I manually imported html2canvas):
While the jspdf from the pull request gave an error (despite the |
Did you install the dependencies? The dependencies are now
You need to include dompurify and html2canvas in your own package.json in order to be installed. In theory, you can reduce your snippet to this:
|
I'm also trying to add html to pdf on the server side with jspdf v2.1.0 and node v14.9.0. Is this supposed to work out of the box, or do I still need to add jsdom? [I get I tried wrapping my html with jsdom, and then passing it to the
Thanks for any input! |
You need to set the document globally:
And then use something like |
Thanks for the answer! Unfortunately I get more errors from jsdom: This is my code:
|
Well, seems like jsdom doesn't implement all required APIs. What you might try is running jsPDF in a headless browser on the backend instead. |
@manuelamaria @HackbrettXXX Hi! Have you managed to solve this? I cant make the html function to work. I am using the latest version of jspdf. Thank you! |
@mountiny we ended up creating a node service where we use puppeteer to render html and then save as pdf. Something like this: https://blog.risingstack.com/pdf-from-html-node-js-puppeteer/#option3 |
@manuelamaria Thank you very much, that seems quite complicated for us now since we are in time/budget pressure. |
This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant. |
I am still facing this issue, do we have a solution now? |
Yep, me too. Would be interested in an answer. |
There is simply no easy way to convert html to PDF without a working DOM API. So a headless browser on the backend is probably the best you can do. |
I've tried this solution with puppeteer but cannot get it to work. It errors with the error "document is not defined" // Dependencies
import * as fs from "fs"
import * as puppeteer from "puppeteer"
import { jsPDF } from "jspdf"
// Create a base PDF
const PDF = new jsPDF()
// Load a browser
const browser = await puppeteer.launch()
const page = await browser.newPage()
// Load the SVG and expose the function we need
const InputSVGPath = "svgs/MARI42DF/page-0001.svg"
const SVGImage = fs.readFileSync(InputSVGPath, "utf-8")
await page.exposeFunction("addSvgAsImage", PDF.addSvgAsImage)
// So we have access to the DOM
await page.evaluate((PDF, SVGImage) => {
// So the addSvgAsImage can use it
globalThis.document = document
// Attempt to add the SVG
return addSvgAsImage.call(PDF, SVGImage, 0, 0, 100, 100)
}, PDF, SVGImage)
// Save
PDF.save("test.pdf")
// Close the browser
await browser.close()
// So TypeScript does not error
const addSvgAsImage = PDF.addSvgAsImage |
@Stefanuk12 , please try this as this worked for me:-
|
source: parallax/jsPDF#2805 probably best to go with puppeteer approach. there are some medium articles written about that
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:
I found this solution, that solved the "html2canvas not loaded" error, but I still get an empty pdf and an UnhandledPromiseRejectionWarning:
This is the code that I tried to run:
Is there any way to solve this issue?
The text was updated successfully, but these errors were encountered: