PDF Form Automation with Laravel and PDF Toolkit

Elian Lopez

Elian Santiago

24 October, 2024

Automatizacion Formularios

Introduction

Generating PDF files can be complicated, especially when client provide pre-rendered documents with fillable forms. While creating PDFs from blade views it’s easier and offers more control and flexibility, at times we are required to edit existing PDFs.


In this tutorial, we’ll guide you step by step on how editing existing PDFs and fill programmatically form fields.A commonly used tool is FPDF, which requires specifying exact coordinates to absolute position the text inside the document, which can take you a lot of time and is not the most amusing task.


This is where PDF Toolkit becomes an effective solution. This command-line tool provides a wide range of functionalities, allowing not only the manipulation of PDF forms created with software like Adobe Acrobat Reader but also automating the data-filling process. When designing forms, it’s possible to assign identifiers to the fields, simplifying the integration and handling of information within our systems.


In this guide, we will show you how to integrate PDF Toolkit into a backend built with Laravel or can also be easily used in a simple PHP application.

Prerequisites

- Server access: If you’re working in a production environment, make sure you have SSH access and the right permissions to install system packages.



Step 1: Installing PDF Toolkit

Production


Connect to your server via SSH and install PDF Toolkit:

sudo apt update && sudo apt install pdftk


This will start the download and installation process. If you encounter a screen like this:



Don’t worry, it's just a kernel update. Simply press "ok" for all prompts, and the package installation will finish.


Next, verify that it was installed correctly with the following command:

pdftk --version


If you see a message like this:

pdftk port to java 3.2.2 a Handy Tool for Manipulating PDF Documents
Copyright (c) 2017-2018 Marc Vinyals - https://gitlab.com/pdftk-java/pdftk
This is free software; see the source code for copying conditions.


Congratulations! The package has been installed correctly.


Development/Local

The process is similar for your local development environment. Below, I’ll show you how to install PDF Toolkit based on your operating system:

Mac

brew install pdftk-java

Linux

sudo apt update && sudo apt install pdftk


Verify the installation:

pdftk --version



Step 2: Integration with Laravel

Installing the PHP Package

To interact with PDF Toolkit from Laravel, we will use the mikehaertl/php-pdftk library. Install it via Composer:

composer require mikehaertl/php-pdftk



Basic Usage in a Controller

Here’s a basic example of how to use this library in a Laravel controller to fill a PDF form with data:

use mikehaertl\pdftk\Pdf;


class PdfController extends Controller
{
    public function generatePdf()
    {
        $generatedFilePath = 'path/to/generated_file.pdf';
        $pdfTemplatePath = 'path/to/pdf_template.pdf';
        
        $pdf = new Pdf($pdfTemplatePath);
        $pdf->fillForm([
            'field1' => 'value1', 
            'field2' => 'value2'
        ])->saveAs($generatedFilePath);
        
        return response()->download($generatedFilePath);
    }
}


Note: You must use the same field names as the ones in the PDF form to fill in the data correctly. Otherwise, the package won’t know how to identify the field.


Common Errors and Solutions

If you encounter the following error:

pdfkit' is not recognized as an internal or external command, operable program or batch file.


You may need to specify the path where PDF Toolkit is installed on your system. Here's how to do this based on your operating system.


Mac

use mikehaertl\pdftk\Pdf;

class PdfController extends Controller
{
    public function generatePdf()
    {
        $generatedFilePath = 'path/to/generated_file.pdf';
        $pdfTemplatePath = 'path/to/pdf_template.pdf';
        $pdfOptions = [  
            'command' => '/opt/homebrew/opt/pdftk-java/bin/pdftk',
            'useExec' => true,  
        ];
        
        $pdf = new Pdf($pdfTemplatePath, $pdfOptions);
        $pdf->fillForm([
            'field1' => 'value1', 
            'field2' => 'value2'
        ])->saveAs($generatedFilePath);
        
        return response()->download($generatedFilePath);
    }
}


Linux

use mikehaertl\pdftk\Pdf;

class PdfController extends Controller
{
  public function generatePdf()
  {
    $generatedFilePath = 'path/to/generated_file.pdf';
    $pdfTemplatePath = 'path/to/pdf_template.pdf';
    $pdfOptions = [  
      'command' => '/usr/bin/pdftk', // Linux
      'useExec' => true,  
    ];
     
    $pdf = new Pdf($pdfTemplatePath, $pdfOptions);
    $pdf->fillForm([
      'field1' => 'value1', 
      'field2' => 'value2'
    ])->saveAs($generatedFilePath);
     
    return response()->download($generatedFilePath);
  }
}


Conclusion

PDF Toolkit is a powerful tool for manipulating existing PDF files and automating the form-filling process in Laravel. In this tutorial, you learned how to install PDF Toolkit both in a production and development environment, how to integrate it into Laravel, and how to solve common errors.


Additional Resources

PDF Toolkit (Official Documentation)

Repository mikehaertl/php-pdftk