Forem

Cover image for HTML to PDF JavaScript – Example with Code

HTML to PDF JavaScript – Example with Code

Awan Shrestha on February 25, 2025

In this blog we will see how we can convert the HTML site to PDF using JavaScript. For a project, I needed to convert some HTML to PDF using JavaS...
Collapse
 
thomasbnt profile image
Thomas Bnt

Hello ! Don't hesitate to put colors on your codeblock like this example for have to have a better understanding of your code 😎

console.log('Hello world!');
Enter fullscreen mode Exit fullscreen mode

Example of how to add colors and syntax in codeblocks

Collapse
 
aayyusshh_69 profile image
Aayush Pokharel

Good advice.

Collapse
 
gyftunez profile image
Stephen Uchenna

I remember trying to send a resumee I took all day writing the code and applying css to the html but external... Printed it but got all plain color only... I nearly ran mad, because my employer was waiting to get it as soon as possible... I had it submitted like that with it dividing each section horizontally 😂😂...

Collapse
 
mahmud-r-farhan profile image
Mahmudur Rahman

Great post! 🚀 If you want to explore another approach, check out this GitHub repository. It converts images to PDFs efficiently and might offer some useful insights for similar projects. I would love to hear your thoughts on it!

Collapse
 
agarshan profile image
agarshan

Yeah on that we cant select the text if it converts images to pdfs

Collapse
 
perisicnikola37 profile image
Nikola Perišić

Nice post! If you want to see parsing HTML using React.js and TypeScript, take a look of this GitHub repository.
It's custom logic parsing HTML content and already has 14 stars.

Collapse
 
agarshan profile image
agarshan

What happens if you want to break the content to multiple pages? Did u try?
And also try with some images, if images exceed the page the images will break.
Just wanted information you on these limitations
You have to fix them for yourself

Collapse
 
fstrube profile image
Franklin Strube

Is this just creating an image in a PDF? That's not great for PDF accessibility (yes, it's a thing, just like accessibility on the web).

Collapse
 
cc_f9f91ece754f4e626078c2 profile image
cc44599 • Edited

nice!

Collapse
 
asjadansari07 profile image
Asjed Ansari • Edited

Is it possible to download pdf without rendering html or without using an Iframe?

Collapse
 
skyhayato profile image
SKY-HaYaTo

Wow! Nice code! Thank you!