Converting dom node to pdf?

do you know of a crate or set of crates to accomplish that?

I've looked into a closely related problem. I found nothing but pain out there.

Your best bet might be to just print to PDF through automation, but then it's browser dependant, and it must be the whole document, which can be really awkward.

If you can render to SVG, there's a bunch of dubious SVG to PDF convertersthat don't really work (either app or library) with some not all that fancy examples I tried

You can snapshot the node with a canvas to get a bitmap image, and then use that, with the obvious problems that has.

The solution I ended up with was to skip the HTML and build a custom subset of what we needed to do dynamically, and use static PDFs for the static content. Most PDF editing libraries are good enough for that to be pretty easy, though you'll probably need to do text wrapping yourself.

1 Like

You could use headless-chrome.

What are you trying to accomplish with this?

If you are trying to create a PDF document with dynamic content, then don't bother converting from HTML. Most likely, you are looking for a real, proper typesetting system.

Luckily, you don't have to go through all the pain of using LaTeX anymore, since a next-generation typesetting engine is being actively developed in Rust: Typst.

It is available as a library (designed upfront with modularity and embedding in mind) as well as a command line tool. I am using it at $JOB and it works surprisingly well for its age.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.