Arclab® Web Form Builder
How to Insert an Image, Header or Logo using TCPDF and FPDF
Execute
Custom PHP Code
Build Custom PHP Web Forms
for Your Own Website
check PHP Forms for your own website
check Design your forms using a visual environment
check Execute custom PHP code
check Create PDF attachments using TCPDF and FPDF
check Send the form data as email
check Supports file uploads (attachments)
check Send custom email messages in text and/or HTML format
check Works with PHP 5.3.2++, PHP 7 and PHP 8
Insert Image using TCPDF or FPDF
Important: To insert an image into the PDF document, you cannot simply
insert an <IMG> tag into your HTML code, you must use the TCPDF/FPDF
"Image" function.
This is because the PDF Library does not support all
HTML elements.
- First create a graphic in JPG or PNG format (PNG recommended) and upload it to your website in the same directory as the PHP file.
- Then add the "$pdf->Image" function under "pdf->AddPage" to the PHP code.
- Adjust the coordinates of the HTML cell (text) below the image so that the image and text do not overlap.
- Resize the image accordingly. The easiest way to achieve this is to resize the image accordingly using your image editor.
"Image" Function Parameters:
(simplified)
Image(file, x, y, w = 0, h = 0, type = '');
- file : Name of the file containing the image.
- x : x - coordinate from the upper left corner (in mm by default)
- x : y - coordinate from the upper left corner (in mm by default)
- w : Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
- h : Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
- type : Image format. Possible values are (case insensitive): JPG, JPEG, PNG. If not specified, the type is inferred from the file extension.
- If you want to use other parameters, please use the function reference of the PDF library used.
Sample Code:
The code for TCPDF and FPDF is identical here since both libraries are
related.
The following example code inserts a graphic at a distance of
x=10mm and y=10mm from the upper left edge.
// Sample 1:
// upload
the image header.png to the same folder as the php/form file!
$pdf->Image('header.png',10,10,0,0,'PNG');
// Sample 2:
// upload the image sample.jpg
to the same folder as the php/form file!
$pdf->Image('sample.jpg',10,10);
// Sample 3:
// upload the image sample.png
to the same folder as the php/form file!
$pdf->Image('sample.png',10,10);
TCPDF Sample Code:
$tcpdfinclude = 'tcpdf/tcpdf.php';
if(!stream_resolve_include_path($tcpdfinclude))ErrorCancel('Invalid
TCPDF include path');
require_once($tcpdfinclude);
$pdf = new
TCPDF();
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
// upload the
image header.png to the same folder as the php/form file!
// insert image
$pdf->Image('header.png',10,10,0,0,'PNG');
$html
= '<p>
First_Name: '.ToHtml($ID39).'<br>
Last_Name: '.ToHtml($ID44).'<br>
</p>';
// adjust the coordinates of the HTML cell
(text) below the image so that the image and text do not overlap!
//
in this sample (10,60) is x and y
$pdf->writeHTMLCell(0, 0,
10, 60, $html);
$att_content_data = $pdf->Output('ignored',
'S');
FPDF Sample Code:
$fpdfinclude = 'fpdf/fpdf.php';
if(!stream_resolve_include_path($fpdfinclude))ErrorCancel('Invalid
FPDF include path');
require_once($fpdfinclude);
$pdf = new FPDF();
$pdf->AddPage();
// upload the image
header.png to the same folder as the php/form file!
// insert image
$pdf->Image('header.png',10,10,0,0,'PNG');
// adjust the coordinates of the cell (text)
below the image so that the image and text do not overlap!
$pdf->SetFont('Arial','',12);
$pdf->Cell(0,10,"First_Name: ".ToAnsi($ID39));
$pdf->Ln();
// line break
$pdf->Cell(0,10,"Last_Name: ".ToAnsi($ID44));
$att_content_data = $pdf->Output('S');