php-tcpdfHow do I add a header to a PDF file using PHP and TCPDF?
Using the TCPDF library, you can add a header to a PDF file using PHP. Here is an example code block to get you started:
<?php
// Include the TCPDF library
require_once('tcpdf_include.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('My Name');
$pdf->SetTitle('My PDF Title');
$pdf->SetSubject('My PDF Subject');
$pdf->SetKeywords('My, PDF, Keywords');
// set header and footer
$pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// Add a page
$pdf->AddPage();
// Print a text
$pdf->Write(0, 'My PDF header', '', 0, 'C', true, 0, false, false, 0);
// Close and output PDF document
$pdf->Output('example_001.pdf', 'I');
This code will generate a PDF file with a header.
The code is composed of the following parts:
- Include the TCPDF library:
require_once('tcpdf_include.php');
- Create a new PDF document:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
- Set document information:
$pdf->SetCreator(PDF_CREATOR);
- Set header and footer:
$pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);
- Set margins:
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
- Set auto page breaks:
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
- Set image scale factor:
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
- Add a page:
$pdf->AddPage();
- Print a text:
$pdf->Write(0, 'My PDF header', '', 0, 'C', true, 0, false, false, 0);
- Close and output PDF document:
$pdf->Output('example_001.pdf', 'I');
Helpful links
More of Php Tcpdf
- How do I install TCPDF using Yum on a PHP server?
- How do I add a watermark to PDFs using PHP and TCPDF?
- How can I generate a PDF from XML data using PHP and TCPDF?
- How do I use the setfont function in PHP TCPDF?
- How can I use PHP and TCPDF to read a PDF?
- How do I install TCPDF with Composer in a PHP project?
- How can I use TCPDF's writeHTML() function in PHP?
- How do I use the setxy function in PHP TCPDF?
- How can I generate a PDF from data stored in a MySQL database using TCPDF and PHP?
- How can I generate a QR code using PHP and TCPDF?
See more codes...