How to Convert HTML to PDF in Python with WeasyPrint (Step-by-Step Guide)
Learn how to convert HTML and CSS to PDF in Python using WeasyPrint. Create professional reports, invoices, and documents with simple, clean code.
Making polished PDFs from HTML and CSS can feel like a headache, but that’s exactly where WeasyPrint helps out.
It’s a Python library that takes the web skills you already know—HTML and CSS—and turns them into clean, print-ready PDFs.
Instead of dealing with clunky PDF tools or writing long, messy scripts, you can build a document just like you’d build a web page, then save it as a PDF with only a few lines of Python.
Welcome to Weasyprint
. Check out other 3 Random Articles here.
Imagine you're subscribed to a newsletter called 3 Randoms. Each week, it introduces you to three lesser-known Python tools that can make your coding better. It's like expanding your toolbox and discovering new tricks.
Think of it as having your own little publishing press inside Python. Whether you’re putting together invoices, reports, resumes, or documents for clients, you get to focus on the layout and design while WeasyPrint handles the hard part of turning it into a finished file.
The nice part is you don’t need to learn anything new—if you can write HTML and CSS, you already have what you need.
I can now take layouts I already have and get clean, professional PDFs without wasting hours adjusting formatting.
In this article, I’ll show you how to install WeasyPrint, make your first PDF, and use CSS to style documents exactly how you want them. By the end, you’ll see how useful this library is if you ever need sharp, professional PDFs as part of your workflow.
pip3 install weasyprint
P.S - If you are on a mac like me, make sure you have homebrew set up.
👉 Thank you for allowing me to continue to do work that I find meaningful. This is my full time job so I hope you will support my work. You can now subscribe on the substack app!
My premium readers get access to so much more, like monthly Python projects, in-depth weekly articles, this here '3 Randoms' series, and my complete archive!
👉 If you get value from my work, please leave it a ❤️ and share it. This helps more people discover this newsletter, which helps me out immensely!
Let’s get started and turn your HTML and CSS into something you’ll actually want to print.
This Week’s Weasyprint Tips
Keep reading with a 7-day free trial
Subscribe to The Nerd Nook to keep reading this post and get 7 days of free access to the full post archives.