<?php
// Simple PHP mail merge html to PDF using TCPDF
// This is provided as an example only. Extending it to work for your requirements is up to you.
// TODO : Include your relevant session headers etc
// This array provides the list of the mail merge fields and the values that will replace them
// Obviously this is fake data, the real data would be retrieved from your database
// and used to populate this array of fields.
$merge_data = array(
'[contactfirstname]' => 'Joe',
'[contactlastname]' => 'Bloggs',
'[contactfullname]' => 'Joe Bloggs',
'[companyname]' => 'Fake Company',
'[userfirstname]' => 'My',
'[userlastname]' => 'Lastname',
'[someotherfield]' => 'Some other data',
':)' => '<img src="http://www.veryicon.com/icon/16/System/Oxygen%201/Emotes%20face%20smile.png">',
);
// Get the html template that we are merging
// This could either be stored as a file on the web server or the html could be stored in
// your database for easy modification. If you have stored the html in your database then
// I assume you know enough to get it out again. To keep things simple I will just load
// from a file in a protected folder.
$merge_html = file_get_contents('/some/protected/folder/with/permissions/template1.html');
// Other options could include :
// $merge_html = file_get_contents('get_template.php?template=template1');
// MERGE the html with the data in the fields
foreach($merge_data as $key => $value) {
$merge_html = str_replace($key, $value, $merge_html); // Replace any occurance of the field ($key) with the data ($value)
}
// Convert the html to a PDF document using TCPDF
// TCPDF can be found at : http://www.tcpdf.org/
// NOTE : Modify the paths for the include file to suit your project and where you located TCPDF
require('../lib/tcpdf/config/lang/eng.php');
require_once('../lib/tcpdf/tcpdf.php');
// Extend TCPDF in order to be able to specifically set or blank the document Header and Footer
class PDF extends TCPDF {
public function Footer() {} // No header
public function Header() {} // No footer
}
// Start the new PDF document
$pdf=new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// Set document information and metadata
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('My Fullname'); // Sets the PDF metadata for the author
$pdf->SetTitle('Document title here'); // Sets the PDF metadata for the title
$pdf->SetSubject('Document subject here if required'); // Sets the PDF metadata for the subject
$pdf->SetKeywords(''); // Sets the PDF metadata for the keywords
$pdf->setJPEGQuality(90);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, 13, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(1);
$pdf->SetFooterMargin(2);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// Add a starting page to the document
// NOTE : We set auto page breaks in the settings above, so if required
// TCPDF will automatically add new pages.
$pdf->AddPage();
// Use TCPDF to convert the html into the PDF format
$pdf->writeHTML($merge_html, true, false, true, false, '');
// Output the document inline to the web browser
// On most systems, this will cause the PDF to be displayed within the browser page/frame
// To force a download or to save the file to the server filesystem you can use : D or F in place of I
$pdf->Output('mailmerge.pdf', 'I');