Simple PHP mail merge html to PDF using TCPDF


SUBMITTED BY: Guest

DATE: Jan. 20, 2013, 4:13 a.m.

FORMAT: PHP

SIZE: 3.9 kB

HITS: 2106

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

comments powered by Disqus