How To Fill A Fillable PDF Form with PHP using PDFtk

PHP PDF Form Library

PSPDFKit for Web is a JavaScript library for filling, reading, creating, and editing PDF AcroForms. It is fully compatible with PHP and offers developers an API for programmatic access to PDF form objects, as well as a beautiful UI for form filling in any PHP-based web app.

PSPDFKit SDKs are deployed in some of the world’s most popular applications, such as those made by Autodesk, Disney, DocuSign, Dropbox, IBM, and Lufthansa.

Key Capabilities

  • Fill forms — With the UI or programmatically
  • Capture form data — Export, submit, or embed into the PDF
  • Form designer — Drag & drop fields to turn PDFs into fillable forms
  • Creation — APIs to generate forms and fields from scratch
  • Appearance — Style colors, backgrounds, borders, and widths
  • Validation — Execute JavaScript to validate form data
  • Form events — Trigger workflows or automations
  • Extendable — Add signing, annotation, editing, and more

Guides for Forms

What Are Forms?
Learn about the basics of PDF forms

Supported Form Fields
Learn about the types of form fields supported by PSPDFKit

Form Data Formats
Learn about using external files to manage form data

Programmatic Form Fill
Learn the different approaches to programmatically filling form fields

Import Form Data from PSPDFKit Server
How to import form data from the optional PSPDFKit Server

Import Form Data from Instant JSON Files
How to import form data from Instant JSON files

Import Form Data from XFDF Files
How to import form data from XFDF files

Import Form Data from a Database
How to import form data from a database

Fill Form Fields by Using the UI
Learn how end users can fill form fields with the built-in UI

Attach a File to a PDF
How to attach a file to a PDF as an image annotation

Add an Image to a PDF
How to add an image to a PDF as an image annotation

Detect User Form Field Input
How to subscribe to form events to detect form field input

Form Field Permissions
How to configure form fill permissions

Extract Form Data
How to extract form field values present in a PDF document

Read Form Fields
How to read all form objects present in a PDF document

Submit or Save Form Data to PSPDFKit Server
How to submit or save form data to the optional PSPDFKit Server

Читайте также:  Datetime получить месяц php

Submit or Save Form Data to an External Source
How to submit or save form data to an external source

Embed Form Data into a PDF
How to embed form data into a PDF document

Auto Save Forms
How to configure the way forms are automatically saved

Create a Fillable Form
How to programmatically create a fillable PDF form

Add a Signature Field
How to add a signature form field to a PDF document

Edit Form Fields
How to update a form field or widget annotation

Remove Form Fields
How to remove a form field from a PDF document

Form Creator
How to build an app where fields can be dragged & dropped onto a PDF

Form Field Flags
How to configure form behavior and capabilities

Flatten Forms
How to flatten forms and burn them into the PDF

PDF Actions Support
Learn about the PDF actions supported by PSPDFKit

JavaScript Validation
How to validate form field values using JavaScript

Free Trial

Enjoy unlimited trial usage of all our products. Get guidance and tech support from developers who built the product, and get started within minutes.

Источник

Author Box

We are Microsoft Gold partner with its presence across the United States and India. We are a dynamic and professional IT services provider that serves enterprises and startups, helping them meet the challenges of the global economy. We offer services in the area of CRM Consultation and implementation, Application development, Mobile application development, Web development & Offshore Development.

Fill Out PDF Forms with PHP and PDFtk

How to Fill Out PDF Forms with PHP and PDFtk?

Step 1) Go to the above PDFtk server Link and install the PDFtk server according to your operating system.

After installing the software open the command prompt and run Command:

Step 2) Create a folder for the script on your local server, folder name can be anything. In this case, we have created a folder named: “fillable-pdf”, now create index.php in fillable-pdf folder.

www/public_html/ fillabe-pdf/index.php

        

Generate Fillable PDF Dynamically

Fill the form and your data will fill in a PDF

Create style.css file for form looks good. www/public_html/ fillabe-pdf/style.css

body < padding: 5rem 0; >.btn-block < display: block; width: 10%; >.btn-success < color: #fff; background-color: #0062cc; border-color: #007bff; >.btn-success:hover

Step 3) Create composer.json file for installing PDFtk library.

www/public_html/ fillabe-pdf/composer.json

After this, run this command:

Composer require mikehaertl/php-pdftk, this command creates a vendor folder and downloads the PDFtk library.

Step 4) Check the sample fillable pdf file fields and map fields with handler.php file. Then open fillable pdf on any editor software to check input fields with mapped fields names. You can use this https://www.pdffiller.com/ site to get field’s name.

Step 5) Create a handler file to handle post requests, mapping pdf fields value to post form value.

www/public_html/ fillabe-pdf/handler.php

 define('ACCESSCHECK', TRUE); require_once 'vendor/autoload.php'; use Classes\GeneratePDF; $data = [ 'name_field' => $_POST['fname'] .' ' . $_POST['lname'], 'email_field' => $_POST['email'], 'phone_field' => $_POST['phone'], 'enquiry_field' => $_POST['message'] ]; $pdf = new GeneratePdf; $response = $pdf->generate($data); print_r($response); 

Step 6) Create Folder Classes for Create class GenratePDF.

www/public_html/ fillabe-pdf/classes/GeneratePDF.php

 use mikehaertl\pdftk\Pdf; class GeneratePDF < public function generate($data) < try < $filename = 'pdf_fillabel' . rand() . '.pdf'; $pdf = new Pdf('./test.pdf'); $pdf->fillForm($data) ->flatten() ->saveAs( './pdf_fill/' . $filename); return $filename; > catch(Exception $e) < return $e->getMessage(); > > > 

Step 7) Create a pdf_fill folder for storing fillable pdf.

Step 8) Protect your folder for security reasons, create index.php empty file in classes and pdf_fill folder.

www/public_html/ fillabe-pdf/classes/index.php or www/public_html/ fillabe-pdf/pdf_fill/index.php

Wrapping Up

With the above implementation, you can install PDFtk and learn some of its useful commands like dump_data_fields and fill_form. With all the above steps you will be able to successfully achieve the pdf fillable custom functionality.

Please note that this implementation is basic, and we have tried to keep things as simple as possible. If still, you are having some problem you can connect with our development team for any further assistance.

[sc name=»Web Development»] [add_newsletter] [add_related_page_diff_contents blog_cat = «web-application»]

Today PDF files have become one of the most common methods of sharing documents online. Using a PDF document is often the foremost option whether you need to provide your clients’ documents to third-party service providers like insurance companies or banks, or simply need to send a CV to an employer. Working with PDF files allows you to share plain as well as formatted text, hyperlinks, images, and even different fillable forms that you need.

In this blog, we will be understanding that how one can fill out PDF forms using PHP and a tremendous PDF manipulation tool known as PDFtk Server. Here we will be demonstrating how one can work dynamically by adding data in PDF form through Html forms.

A fillable PDF is a PDF document that contains some fields that are editable without PDF editor software. To fill the fillable PDF dynamically one has to create an HTML form and dynamically bind it. To accomplish this, we will create a PHP script.

Below are the following step and some software & library to fill fillable PDF.

Prerequisites:

Step 1) Go to the above PDFtk server Link and install the PDFtk server according to your operating system.

After installing the software open the command prompt and run Command:

Step 2) Create a folder for the script on your local server, folder name can be anything. In this case, we have created a folder named: “fillable-pdf”, now create index.php in fillable-pdf folder.

www/public_html/ fillabe-pdf/index.php

        

Generate Fillable PDF Dynamically

Fill the form and your data will fill in a PDF

Create style.css file for form looks good. www/public_html/ fillabe-pdf/style.css

body < padding: 5rem 0; >.btn-block < display: block; width: 10%; >.btn-success < color: #fff; background-color: #0062cc; border-color: #007bff; >.btn-success:hover

Step 3) Create composer.json file for installing PDFtk library.

www/public_html/ fillabe-pdf/composer.json

After this, run this command:

Composer require mikehaertl/php-pdftk, this command creates a vendor folder and downloads the PDFtk library.

Step 4) Check the sample fillable pdf file fields and map fields with handler.php file. Then open fillable pdf on any editor software to check input fields with mapped fields names. You can use this https://www.pdffiller.com/ site to get field’s name.

Step 5) Create a handler file to handle post requests, mapping pdf fields value to post form value.

www/public_html/ fillabe-pdf/handler.php

 define('ACCESSCHECK', TRUE); require_once 'vendor/autoload.php'; use Classes\GeneratePDF; $data = [ 'name_field' => $_POST['fname'] .' ' . $_POST['lname'], 'email_field' => $_POST['email'], 'phone_field' => $_POST['phone'], 'enquiry_field' => $_POST['message'] ]; $pdf = new GeneratePdf; $response = $pdf->generate($data); print_r($response); 

Step 6) Create Folder Classes for Create class GenratePDF.

www/public_html/ fillabe-pdf/classes/GeneratePDF.php

 use mikehaertl\pdftk\Pdf; class GeneratePDF < public function generate($data) < try < $filename = 'pdf_fillabel' . rand() . '.pdf'; $pdf = new Pdf('./test.pdf'); $pdf->fillForm($data) ->flatten() ->saveAs( './pdf_fill/' . $filename); return $filename; > catch(Exception $e) < return $e->getMessage(); > > > 

Step 7) Create a pdf_fill folder for storing fillable pdf.

Step 8) Protect your folder for security reasons, create index.php empty file in classes and pdf_fill folder.

www/public_html/ fillabe-pdf/classes/index.php or www/public_html/ fillabe-pdf/pdf_fill/index.php

Wrapping Up

With the above implementation, you can install PDFtk and learn some of its useful commands like dump_data_fields and fill_form. With all the above steps you will be able to successfully achieve the pdf fillable custom functionality.

Please note that this implementation is basic, and we have tried to keep things as simple as possible. If still, you are having some problem you can connect with our development team for any further assistance.

service icon

Web Development Services

Are you looking for a reliable web development company? Our highly skilled web developers enables us to deliver result oriented web development services. Contact our team to understand, how we can help you in achieving your business goals.

Источник

Оцените статью