<![CDATA[ Formit2pdf - My Forums]]> https://forums.modx.com/thread/?thread=103805 <![CDATA[Formit2pdf]]> https://forums.modx.com/thread/103805/formit2pdf#dis-post-558295
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.

Requirements:

You NEED 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.
https://www.pdflabs.com/docs/install-pdftk-on-redhat-or-centos/

You will also need pdftk pro (small premium of like £3 / $5)
https://www.pdflabs.com/tools/pdftk-pro/

Next you'll need to download php pdftk from here:
https://github.com/mikehaertl/php-pdftk

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.

<?php

// 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 

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf($modx->getOption('core_path') . 'components/php-pdftk/yourForm.pdf'); // Path to your blank template PDF
$pdf->fillForm([
	// This is where you need to map PDF fields to Web Fields
    'First Name' => $hook->getValue('first-name'),
    'Last Name' => $hook->getValue('last-name')
    ])
->needAppearances()
->execute();

// Grab the temp PDF file that we just dynamically created
$attachment = $pdf->getTmpFile();

// Add the PDF as an email attachment
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->mailer->AddAttachment($attachment);

return true;


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.

[[!FormIt?
	&hooks=`formit2pdf, email, redirect`
	&emailTo=`[email protected]`
	&emailSubject=`New application`
	&emailFrom=`[email protected]`
	&emailTpl=`myEmailTpl`
	&redirectTo=`2`
]]


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

]]>
lkfranklin Apr 30, 2018, 10:39 AM https://forums.modx.com/thread/103805/formit2pdf#dis-post-558295
<![CDATA[Re: Formit2pdf]]> https://forums.modx.com/thread/103805/formit2pdf#dis-post-563662
DigitalOcean SiteGround iPage]]>
amelia Jan 19, 2019, 09:45 PM https://forums.modx.com/thread/103805/formit2pdf#dis-post-563662
<![CDATA[Re: Formit2pdf]]> https://forums.modx.com/thread/103805/formit2pdf#dis-post-558308 ]]> BobRay May 01, 2018, 03:49 AM https://forums.modx.com/thread/103805/formit2pdf#dis-post-558308 <![CDATA[Re: Formit2pdf]]> https://forums.modx.com/thread/103805/formit2pdf#dis-post-558299 gelstudios Apr 30, 2018, 01:11 PM https://forums.modx.com/thread/103805/formit2pdf#dis-post-558299 <![CDATA[Re: Formit2pdf]]> https://forums.modx.com/thread/103805/formit2pdf#dis-post-558298
Just to add on a little bit if you dont want to attach the form as an email but just save it somewhere on the server then you can just edit the snippet to this. You may want to give the filename a unique name for each submission though.

<?php

// 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 
 
use mikehaertl\pdftk\Pdf;
 
// Fill form with data array
$pdf = new Pdf($modx->getOption('core_path') . 'components/php-pdftk/yourForm.pdf'); // Path to your blank template PDF
$pdf->fillForm([
    // This is where you need to map PDF fields to Web Fields
    'First Name' => $hook->getValue('first-name'),
    'Last Name' => $hook->getValue('last-name')
    ])
->needAppearances()
->saveAs($modx->getOption('core_path') . 'components/php-pdftk/populatedForm.pdf'); // Save the populated form here
 
return true;
]]>
lkfranklin Apr 30, 2018, 12:30 PM https://forums.modx.com/thread/103805/formit2pdf#dis-post-558298
<![CDATA[Re: Formit2pdf]]> https://forums.modx.com/thread/103805/formit2pdf#dis-post-558296 andytough Apr 30, 2018, 11:51 AM https://forums.modx.com/thread/103805/formit2pdf#dis-post-558296