FillPDF is a golang library to easily fill PDF forms. This library uses the pdftk utility to fill the PDF forms with fdf data. Currently this library only supports PDF text field values. Feel free to add support to more form types.
This repo is cloned from https://github.com/desertbit/fillpdf, but changed the API
FillPDF needs 2 different things.
- we need java standalone binary
- we need tool called mcpdf from here - https://github.com/m-click/mcpdf - download link here - https://oss.sonatype.org/content/repositories/releases/aero/m-click/mcpdf/0.2.4/mcpdf-0.2.4-jar-with-dependencies.jar
First, you need to create an executor, with paths to the 2 pre-requisities mentioned above.
executor, err := fillpdf.NewExecutor(fillpdf.Config{
Java: "java",
McPDF: "/Users/karelbilek/Downloads/mcpdf-0.2.4-jar-with-dependencies.jar",
})
if executor != nil {
panic(err)
}
Then, you need to load the source PDF. Either from file, from bytes, or any reader.
fill, cleanup, err := executor.CreateFromFile("form.pdf")
if err != nil {
panic(err)
}
defer cleanup()
// or, for example with embedded PDF
// go:embed input.pdf
var input []bytes
// ...
fill, cleanup, err := executor.CreateFromBytes(input)
if err != nil {
panic(err)
}
defer cleanup()
Note:
- you can reuse the same
fill
in whole program;Fill
is concurrency-safe, you can call it concurrently and many times - call
cleanup
only when you no longer needfill
, as it will delete the temporary directory
Then, you fill the form with data. Note that text data and button data are separate. Currently, this library does not support more.
Note, that all inputs are checked if they exist in the form and if they are the correct type.
err := fill.FillToFile("out_form.pdf", err = fillpdf.FormData{
TextValues: map[string]string{"foo": "bar"},
ButtonValues: fill.AllButtonsTrue(),
}, false)
if err != nil {
// handle error
panic(err)
}
The editable
value is important; with the value, the PDF is still editable by user; but, it can produce subtle errors
on some viewers (Apple Preview, pdfjs) in some cases.
You can call FillToFile
, FillToBytes
or just Fill
to any io.writer.
There are helper functions fill.DefaultTextValues
that will fill all fields to its name, so you can save
the form and see which field is which. Also fill.AllButtonsTrue()
that will fill all buttons to true.
There is an example in the sample directory.
Apache 2.0
(C) Roland Singer roland.singer@desertbit.com
(C) 2022 Karel Bilek kb@karelbilek.com
Note, however, that mcpdf is Affero GPL.