Create/modify an interactive PDF within/in Rust?

Hello,

So I've been writing a Rust program to replace a Python program to spread the gospel of Rust to my local community.

I've hit a roadblock however, that is that the original program produces an interactive PDF (a PDF with editable form fields basically) with some of the fields prefilled based on input from the user.

I've seen various PDF creation/manipulation library offerings on crates.io, but none of them explicitly appear to offer interactive/forms support.

So I had a couple ideas:

  • I could take an existing PDF form template file, and somehow figure out my way into somehow inserting text into the fields, and writing the file back out. This could be easier then pure creation from scratch. (Any guidance?)
  • investigate a C/C++ library that can do this, write a C/C++ program that does this, somehow statically link it to my Rust program and just call it from within Rust

What do you think I should do here? Bare in mind, I'm not the most advanced hacker/programmer who ever lived. :slight_smile:

What does the Python program do?

Various things, but mainly just take a bunch of text input from the user create a PDF and a Word Doc from it.

Yes, but does it use a PDF template or does it use a C++ library? Or does it do something else?

The PDF library I used in Python is Reportlab. From the looks of it, the library is pure Python.

I used it to build the interactive PDF from scratch, pulling in some variables from Python as it does so.

But there's no reason I can't just run it once to generate myself an empty PDF template, and then from there do some Rusty dark arts to hack in some Strings from Rust.

So both options are available :slight_smile:

Is this library actually manipulating the PDF? It seems to be a commercial service with a web API. If the Python library is just an API client, then it may be relatively easy to make one in Rust, but also it would be rather pointless, because that doesn't really require a language like Rust.

Yeah, there is a commercial and free version, which is the one you get if you use pip (Python's cargo), I don't know about the commercial version but can confirm the free version generates PDFs when offline.

The pdf crate is now at a level where this could be possible.
Especially if you start with a template and just search and replace some strings and update the trailer.

I have yet to look at how forms are implemented, so I can't promise it working yet.

I looked at forms in the PDF specification. I don't think they will be supported any time soon... in the renderer.
Adding the required types is fairly easy due to the pdf_derive crate.

Are you saying though that taking an existing interactive PDF and just adding some text to some of the form fields should be currently possible and rather straightforward in Rust?

Could you point me to any documentation/guide/etc or a particular library you have in mind?

If you are familiar with pdf forms, it may be possible to add support in a day or so.
The library in question is

It is probably less work to just use a write-only crate like lopdf if you can generate the entire contents.

It might also be worth looking at:

1 Like

Wow okay, using an existing template and Emulator000/pdf_form worked like a charm! Much easier than I anticipated. God Rust makes things so good.

A note for future finders of this post:

The version linked by @s3bk is a fork of pdf_form on crates.io. But you will want this forked version over the original because the original uses lopdf 0.25.0 to do the work, but 0.25.0 will crash. not to worry the forked version uses 0.26.0, but it can also be found at crates.io/crates/pdf_forms. Note the extra 's' which is not present in the example code but is necessary.

2 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.