Introduction
This is one of those frustrations post where I just spent hours working on something and I finally managed to have a workin...
For further actions, you may consider blocking this person and/or reporting abuse
Lolly Post.
I understand your frustration.
In my case, I spent "One freaking Week" coding a serverless microservice that takes
{html,options}
and returns back the buffer, a generic solution.The challenge was that I needed to embed puppeteer in 50mb which is the maximum size a serverless function can take on Now.
Second challenge was debugging why the f**** HTML didn't render at all.
After ours of trial and error I found that for some reason, if the html string contains the # character, it becomes somehow "invalid" and puppeteer fails silently 0_0
Third, in my particular case, the HTML uses bootstrap and just by using
puppeteer.setContent
didn't wait for it to load correctly. I needed to use a workaround bypuppeteer.go('text/html://${html}')
which do waits for external resources to load.Four, (and this one is the reason why I almost throws the computer through the window), the HTML markup in my case was rendered at runtime using react Dom and the go to hell react-inline-css library, which ALWAYS WRAPS the generated CSS
with this selector
#ReactInlineCss ...
.See the # character there? Goto point #2 above (x_x)
Well, but thanks god the pain is gone. If you wanna learn how to make it serverless like I did, hop to now's blog (zeit.co/blog)
Sorry, that made me laugh 😁
Hey Vladimir, how were you able to fix the second point?
I've gone through something similar a few days ago... and it took me a while to figure it out.
Here's my approach, without using puppeteer.
I'm using Vue in this case, but I'm pretty sure the concept is applicable in other cases as well.
html-pdf uses phantomjs under the hood. Where is your app hosted? It didn't work for me on Docker based cloud providers like Now
The app isn’t hosted (yet), it is all on localhost. I haven’t tried docker nor used cloud providers too often, but I’m really curious about why this approach wouldn’t work.
Please let me know if you find a solution!
We've had success hosting a similar Puppeteer-based converter using Google Cloud Functions (I don't work for Google): github.com/Courtsite/shuttlepdf. There is a bit of latency, but it is a reasonable trade-off for ease of deployment, scalability, and reliability.
Well, in my case, phantomjs wasn't found by the library on my docker based hosting.
There is a dockerized phantom available but you should have full access the deployment Dockerfile in order to tell it to install on the process.
Overall I would recommend going away from html-pdf because it's not maintained anymore
Generally speaking, you should probably avoid Phantomjs. With headless Chromium, there really isn't any need for it. Indeed, I think it is no longer maintained.
Sweet lord thank you for this.
I was using the html-pdf package and was having no issues until I to either deploy to azure app service or run it in a docker container.
Didn't realize that phantomJS is pretty much no longer supported so this was a life saver.
Also, if anyone was curious, I was having issues running puppeteer in a docker container. Found this gem in order to make chromium work in the container:
Interesting post, thanks for sharing!
I have a question: where is the code in which you inject the data into the template?
I mean, I can see where you load the HTML template and render it into a PDF, and I can see the front-end sending some data to customise the PDF like invoiceDetails, clientDetails etc. but how do you actually put that data inside the template?
I have a "react" template on the front end, the one you see.
There is an Html template on the back, with placeholders for the future data. After that, to populate the html template on the back, it's just String.replace() functions
Thanks for sharing, @ IDURAR , we use are using node.js react.js & redux
Here Tutorial about : 🚀 Building and Generate Invoice PDF with React.js , Redux and Node.js : dev.to/idurar/building-an-invoice-...
And it even works on aws lambda ;-)
please provide the complete code
Bootstrap is not working in my pdf.html file.
if there is any solution please let me know
Hey man, this you find a solution to this?
I am stuck here too.