pdf-rs / pdf

Rust library to read, manipulate and write PDF files.
MIT License
1.24k stars 119 forks source link

Error on filling in pdf form #205

Open buhaytza2005 opened 9 months ago

buhaytza2005 commented 9 months ago

I was very happy to read that the functionality to fill in dynamic pdfs is coming to this library.

Unfortunately, there seems to be an error, at least for the pdf I am trying to work with. This may be something very minor but I am the beginning of my journey with Rust and it could be that I am just understanding how to use the example.

I cloned the repo and added my file to the files folder then I have executed the following:


cargo build
./target/debug/form files/template_form.pdf

The output of this was:

read: files/template_form.pdf
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Try { file: "pdf/src/file.rs", line: 609, column: 23, context: Context([]), source: FromPrimitive { typ: "RcRef < Catalog >", field: "root", source: Shared { source: FromPrimitive { typ: "Option < InteractiveFormDictionary >", field: "forms", source: FromPrimitive { typ: "Vec < RcRef < FieldDictionary > >", field: "fields", source: Shared { source: MissingEntry { typ: "Rect", field: "rect" } } } } } } }', examples/src/bin/form.rs:17:54
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I am attaching the pdf I am trying to work with in the hope that someone can point me in the right direction.

Thanks! template_form.pdf

s3bk commented 9 months ago

If you want, modify the FieldDictionary struct and wrap the rect field in an Option. (And make a PR with it if it solves the problem.)

It is very much work in progress so there are definitely more bugs to be found.

buhaytza2005 commented 9 months ago

Tried the suggested fix and it appears at least part of it was taken care of.

However, now I am encountering the below:

read: files/template_form.pdf
thread 'main' panicked at examples/src/bin/form.rs:17:54:
called `Result::unwrap()` on an `Err` value: Try { file: "pdf/src/file.rs", line: 609, column: 23, context: Context([]), source: FromPrimitive { typ: "RcRef < Catalog >", field: "root", source: Shared { source: FromPrimitive { typ: "Option < InteractiveFormDictionary >", field: "forms", source: FromPrimitive { typ: "Vec < RcRef < FieldDictionary > >", field: "fields", source: Shared { source: MissingEntry { typ: "Name", field: "subtype" } } } } } } }

I can see that the InteractiveFormDictionary does not have a subtype field, tried adding it - copied one from Annotation struct and I seemed to have gotten another error now

read: files/template_form.pdf
thread 'main' panicked at examples/src/bin/form.rs:17:54:
called `Result::unwrap()` on an `Err` value: Try { file: "pdf/src/file.rs", line: 609, column: 23, context: Context([]), source: FromPrimitive { typ: "RcRef < Catalog >", field: "root", source: Shared { source: FromPrimitive { typ: "Option < InteractiveFormDictionary >", field: "forms", source: MissingEntry { typ: "Name", field: "subtype" } } } } }

Tried to poke around at what that could mean but not really got anywhere so if there are any suggestions or pointers I am more than happy to keep poking but I went down the path of macros and that is waaay over my head.

s3bk commented 9 months ago

I fixed the bugs and improved the Error display so you should be able to figure out where it needs fixing next time.

vaijira commented 6 months ago

This is another pdf that is having problems parsing the annotations:

thread 'main' panicked at examples/src/bin/form.rs:46:59:
can't load annotations: FromPrimitive { typ: "Option < MaybeRef < AppearanceStreams > >", field: "appearance_streams", source: FromPrimitive { typ: "Ref < AppearanceStreamEntry >", field: "normal", source: UnexpectedPrimitive { expected: "Reference", found: "Dictionary" } } }