Create PDF using FPDF and Add it as Email Attachment

Arclab® Web Form Builder



FPDF is a "PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. F from FPDF stands for Free".
FPDF can be downloaded from the manufacturer and unzipped into a folder on your web server or installed with "composer".
Homepage: open_in_new


FPDF Installation:



Add a new "Submission Task"


Switch to the "Email and Database" tab and select the type of task you want to add under "Add Submission Task":
Please note that this functionality is only available in the "Test Version" or in the "Developer Edition".


Execute Custom php Code



Add the Code to Create the PDF Document


An example PHP code is automatically generated, which contains 2 fields from your form (if available).
Of course, you can also simply copy your existing PHP code into the editor to create a PDF document with FPDF. In this case, please make sure that you change the parameter of the FPDF "Output" function as described below and assign the result to the PHP variable $att_content_data.


Insert code to create the pdf document


Sample Code:

// Include the FPDF library. The library must be installed on your web server.
// Please note that you may have to adjust the path so that it refers to the include file

$fpdfinclude = 'fpdf/fpdf.php';

if(!stream_resolve_include_path($fpdfinclude))ErrorCancel('Invalid FPDF include path');


// Example code for generating the PDF document with FPDF
// Replace the example code with your own code

$pdf = new FPDF();


// In the following example code, 2 text outputs with values are inserted into the PDF document.
// The form data is accessed using the variables ($ID..) displayed on the right under "Form Field Variables".
// FPDF does not support utf-8, so the form data must be converted to ANSI using "ToAnsi($ID ..)".

$pdf->Cell(0,10,"First_Name: ".ToAnsi($ID39));
$pdf->Ln(); // line break
$pdf->Cell(0,10,"Last_Name: ".ToAnsi($ID44));


// The code below outputs the PDF document as string and assigns it to $att_content_data.
// The parameter 'S' returns the document as a string.

$att_content_data = $pdf->Output('S');



Code Details


First the FPDF library has to be included. This can be done e.g. via the code:


In the example above, the code has been slightly modified to display an error message if the path is incorrect:

$fpdfinclude = 'fpdf/fpdf.php';
if(!stream_resolve_include_path($fpdfinclude))ErrorCancel('Invalid FPDF include path');

In this example the include file of the FPDF library is in the subdirectory "fpdf".
Please note that you have to adapt the path according to your FPDF installation.
The path is case-sensitive! Sample: fpdf/fpdf.php is not the same as FPDF/fpdf.php (!)
See also: Adjust the Path of the PHP "include" File for external PHP libraries


The contents of the form fields can be accessed via the php variables displayed on the right:


Form fields


A php variable is assigned to each form field, which contains the value of the field, e.g. the user input/selection.
The variable name corresponds to the ID in the form.
FPDF does not support utf-8, so the form data must be converted to ANSI using "ToAnsi($ID ..)".




Return the PDF document as a string and assign it to $att_content_data.

To do this, you have to change the parameters of the function "Output" so that it returns the PDF document as a string to the variable $att_content_data:

$att_content_data = $pdf->Output('S');

The parameter must be changed to 'S'. This means that the PDF document is not sent directly to the browser, but returned as a string.

If you want to change the file name of the attachment, click on "Email File Attachment" in the menu or in the right area on "File Name".



Change the Order of the Tasks



Please note that all tasks are executed the order shown under "Email and Database". The file attachment must have been created before the email and will then be attached to all subsequent emails. You can use the "Move Up" and "Move Down" buttons to change the order of the tasks accordingly.


Change Task Order