I figured I would document this here as although quite simple it was a bit of a pain for me to get this to work. There are a couple of pdf parsers available such as mpdf, fpdf etc but unfortunately none of these actually let you fill out exisiting pdf templates AND support checkboxes, selects and radio buttons.
The purpose of this tutorial is to take a form submitted on the front end by a user and have the values of the fields be placed into a templated PDF that we've been supplied with. In my particular use case the client is a job recruitment agency who require that all candidates fill out a designed lengthy form manually. Making this process available digitally undoubtedly speeds up a lot of their admin work and saves candidates the hassle of coming down to their office to fill out a form.
to have pdftk installed on your server. The php pdftk parser that we use is just a front face for the command line tool. If you're working locally on XAMPP then installing pdftk pro (below) will be sufficient as pdftk pro is just a GUI for the processor.
You will also need pdftk pro (small premium of like £3 / $5)
Next you'll need to download php pdftk from here:
Install php pdftk somewhere accessible on your server such as the 'core/components/php-pdftk/'
Next you'll need to prepare your blank pdf template form to be filled out. You'll need Adobe Acrobat Pro so you can go to 'tools->prepare form'. For the most part it's quite clever and will setup the majority if not all of the form fields for you including correct letter spacing and such for big spaced out fields as is common with NI numbers. However it's not that great at setting up radio buttons and checkboxes for you so you may need to go in and edit these manually. Take note of the field names as you'll need to know these to map the data correctly in the hook we create next.
Once you've setup your pdf form you'll need to use pdftk to resave out your pdf. Make sure you click 'advanced' on (https://www.pdflabs.com/docs/pdftk-pro-guide/advanced-processing-option.png
) so that it can pick up the form fields correctly.
Next create a snippet called 'formit2pdf' and insert this code, replacing the path to your autoload.php file and your template pdf file (the one you just saved out using pdftk pro ^
). In the array you'll also need to map out the value of the pdf form to the value of the front end web form.
// Stuck? This code originated from the MODX Community Forums: https://tinyurl.com/ychan98k
require_once('/path/to/php-pdftk/vendor/autoload.php'); // Server path to php-pdftk
// Fill form with data array
$pdf = new Pdf($modx->getOption('core_path') . 'components/php-pdftk/yourForm.pdf'); // Path to your blank template PDF
// This is where you need to map PDF fields to Web Fields
'First Name' => $hook->getValue('first-name'),
'Last Name' => $hook->getValue('last-name')
// Grab the temp PDF file that we just dynamically created
$attachment = $pdf->getTmpFile();
// Add the PDF as an email attachment
Next include the formit2pdf we just created as a hook in your formit. You can also go ahead and setup a redirect to a thank you page if needed.
&hooks=`formit2pdf, email, redirect`
Setup your web form to match the values we used in the hook (first-name + last-name)
<input type="text" name="first-name" placeholder="first-name" value="[[!+fi.first-name]]">
<input type="text" name="last-name" placeholder="last-name" value="[[!+fi.last-name]]">
Finally setup an emailTpl called 'myEmailTpl'
Congratulations, you have a new application from: <strong>[[+first-name]] [[+last-name]]</strong>.
We've processed their form submission and attached it to this email for you.
If you've setup everything correctly then when your users submit a form you'll receive an email with the filled out pdf as an attachment.
[ed. note: lkfranklin last edited this post 2 years, 4 months ago.]