On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • 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.

    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=`user@website.co.uk`
    	&emailSubject=`New application`
    	&emailFrom=`website@website.co.uk`
    	&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.

    [ed. note: lkfranklin last edited this post 2 years, 4 months ago.]
      ■ email: lk@alienbuild.uk | ■ website: https://alienbuild.uk

      The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
    • Fantastic. Thank you for documenting it here. Bookmarking this now.
        If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

        email: andy@andytough.com | website: https://andytough.com
      • No problem.

        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;
        [ed. note: lkfranklin last edited this post 2 years, 4 months ago.]
          ■ email: lk@alienbuild.uk | ■ website: https://alienbuild.uk

          The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
        • Great job!
            GEL STUDIOS
            MODX Professional | MODX Ambassador

            Website | Email | Twitter | Facebook
          • Very nice! smiley
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
            • That's great ! thank you for sharing.

              DigitalOcean SiteGround iPage [ed. note: amelia last edited this post 1 year, 7 months ago.]