Library for Building PDFs

I'd like to generate some documents as PDFs, e.g. bills with a header and footer, a company logo, some tables with numbers etc.

I found genpdf, but it looks quite dead to me (no commits in almost 3 years).

Does anybody have any experience in this field with Rust libraries? I used to work with the Report Lab PDF Toolkit with Python, but would like to use Rust in the future for this purpose.

1 Like

I really recommend typst. It's great. It supports loading structured data you can use to fill out your invoices and such.

A more general recommendation, if I'm looking for a crate I like to check lib.rs' keywords (i.e. in your case pdf or pdf-file), they are usually more effective for finding suitable crates that crates.io's search in my experience.

5 Likes

I have a crate here:

https://crates.io/crates/pdf-min

It doesn't do images yet (or tables for that matter), it is currently text only, it takes simple HTML input and generates a PDF file from it, but that reminds me, I should do images, so I will probably do it in the next few days. I don't think it is a big job... (famous last words!!).

1 Like

Maybe crates.io: Rust Package Registry is another crate that might suit your needs.

1 Like

I very much second typst - I went from templating LaTeX (and running it as an external command) to running typst through the API, and it's nothing short of awesome. Depending on where/how you get your input data, you might want to look at sys.inputs, which removes the need to go through a textural representation - no quoting, ever!

3 Likes

Thanks, I'll follow the development of your crate!

My issue is the automation of document preparation, not creating individual documents. But good to know anyway!

I don't think I can help you, but can you specify what "automation of document preparation" is as opposed to "creating individual documents"? It kinda feels like both boil down to "I have data in a rust app, and I need to make a document containing this data"... maybe if you clarify that, others have ideas :wink:

I'd also very much like to have a high-level library that can rearrange pdfs easily, but I don't think this exists in rust right now.

I agree, that looks pretty good. I think I changed my mind about adding image support to my crate, it is quite a big job converting image formats. Not sure what I am going to do with my very minimal crate. I did want something very light weight, and once you add image support that is not really possible.

[ I was just wondering, is there a "PDF image" file format, that is a file that is easily embedded in a PDF without any conversion needed, and then you could have tools to convert to/from this format from other image file formats like PNG, JPEG etc,etc. ]

I suppose I could just support PNG, maybe that isn't too much work.

I have a C# PNG reader which is only 600 lines or so, I could convert that:

It looks like PDF supports the compression algorithms for several standard raster image formats, like PNG and JPEG. So you might be able to write something that just converts the framing information and leaves the pixel data untouched.

PDF itself is a kind of vector graphics format, so you can just save your image as PDF. Alternatively, it's quite easy to extract bitmap images (PNG, JPEG) from PDFs.

Recently used genpdf and really liked it. It is rather limited but also easy to use. A header with logo is supported but no footer, unfortunately.

[Edit] Did find a link to a custom made footer: https://todo.sr.ht/~ireas/genpdf-rs/65

1 Like

Just in case you're not aware of it: Image — Rust image library // Lib.rs is that standard crate for common image formats.

1 Like