Surely you can figure out how to use existing libraries for this task, or is there something you’re stuck on?
In a production web app I use Gotenberg. It’s definitely overkill for the task at hand, but if you find yourself doing this often I would highly recommend it. It’s dead easy to convert HTML (and I imagine XHTML) to PDF.
Pretty sure calibre makes this easy if you don't wanna reinvent the wheel
came here to say calibre! it works and the converting is super simple. takes a little while for pdf files since its a big file but it works
Oh, I already have that installed. I'll try it.