example44_MPDFI_yearbook.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // First write all your entries to a PDF file, forcing each entry to fit on one page
  3. include("../mpdf.php");
  4. // Define the maximum containing box width & height for each text box as it will appear on the final page (no padding or margin here)
  5. $pw = 80;
  6. $ph = 110;
  7. $minK = 0.7; // Maximum scaling factor 0.7 = 70%
  8. $inc = 0.01; // Increment to change scaling factor 0.05 = 5%
  9. $spacing = 10; // millimetres (vertically and horizonatlly between boxes in output) shrinks if boxes too big
  10. $border = 3; // millimetres round final boxes (-1 for no border)
  11. $align = 'T'; // T(op) or M(iddle) for content of final output boxes
  12. // Only change the first parameter of the next line e.g. utf-8
  13. $mpdf = new mPDF('', array(($pw*(1/$minK)),($ph*(1/$minK))), '','', 0,($pw*(1/$minK))-$pw,0,($ph*(1/$minK))-$ph,0,0);
  14. $pph = array();
  15. // FOR EACH ENTRY FOR YOUR YEARBOOK saving the page height in $pph (where $html is the HTML code for the entry):
  16. // $pph[$i] = SinglePage($html, $pw, $ph, $minK);
  17. //==============================================================
  18. // .. but we will use this for an example
  19. $html1 = '
  20. <style>
  21. div { text-align: justify; }
  22. </style>
  23. <h2>Joanne Smith 2002-2007</h2><div>This is the normal text in the div: Nulla felis erat, imperdiet eu, ullamcorper non, nonummy quis, elit. Suspendisse potenti. Ut a eros orci. Morbi feugiat pulvinar dolor. Cras odio. Donec mattis, nisi id euismod auctor, neque metus pellentesque, <img src="tiger.wmf" width="100" style="float: right; margin: 4px; " /> risus at eleifend lacus sapien et risus. Phasellus metus. Phasellus feugiat, lectus ac aliquam molestie, leo lacus tincidunt turpis, vel aliquam quam odio et sapien. Mauris ante pede, auctor ac, suscipit quis, malesuada sed,<img src="tiger.jpg" width="100" style="float: left; margin: 4px; " /> nulla. Integer sit amet odio sit amet lectus luctus euismod. Donec et nulla. Sed quis orci. </div>
  24. ';
  25. $html2 = '
  26. <style>
  27. div { text-align: justify; }
  28. </style>
  29. <h2>Tim Another 2001-2007</h2><div>This is the normal text in the div: Nulla felis erat, imperdiet eu, ullamcorper non, nonummy quis, elit. Suspendisse potenti. Ut a eros at ligula vehicula pretium. Maecenas feugiat pede vel risus. et lectus. Fusce eleifend neque sit amet erat. Integer consectetuer nulla non orci. Morbi feugiat pulvinar dolor. Cras odio. Donec mattis, nisi id euismod auctor, neque metus pellentesque, <img src="tiger.jpg" width="100" style="float: right; margin: 4px; " /> risus at eleifend lacus sapien et risus. Phasellus metus, suscipit quis, malesuada sed, nulla. Integer sit amet odio sit amet lectus luctus euismod. Donec et nulla. Sed quis orci. <br />
  30. Morbi feugiat pulvinar dolor. Cras odio. Donec mattis, nisi id euismod auctor, neque metus pellentesque risus, at eleifend lacus sapien et risus. Phasellus metus. Phasellus feugiat, lectus ac aliquam molestie, leo lacus tincidunt turpis, vel aliquam quam odio et sapien. Mauris ante pede, auctor ac, suscipit quis, malesuada sed, nulla. Integer sit amet odio sit amet lectus luctus euismod. Donec et nulla. Sed quis orci. </div>
  31. ';
  32. for($i=1; $i<=10; $i++) {
  33. // $html = $html;
  34. if ($i % 3 == 1) { $html = $html2; }
  35. else { $html = $html1; }
  36. $pph[$i] = SinglePage($html, $pw, $ph, $minK) ; // $pph saves the actual height of each page
  37. }
  38. //==============================================================
  39. // Save the pages to a file
  40. $mpdf->Output('test.pdf','F');
  41. // Now collate those pages using IMPORT - 4 pages to one page
  42. $mpdf=new mPDF();
  43. $mpdf->SetImportUse();
  44. $mpdf->SetDisplayMode('fullpage');
  45. $mpdf->SetHeader('{DATE j-m-Y}|My Yearbook 2005|{PAGENO}');
  46. $mpdf->SetFooter('|Printed using mPDF|');
  47. $pagecount = $mpdf->SetSourceFile('test.pdf');
  48. for($i=1; $i<=$pagecount; $i++) {
  49. if ($i % 4 == 1) { $mpdf->AddPage(); }
  50. $pgheight = $mpdf->h - $mpdf->tMargin - $mpdf->bMargin;
  51. $hspacing = min($spacing,($mpdf->pgwidth - $pw*2) );
  52. $vspacing = min($spacing,($pgheight - $ph*2) );
  53. $x1 = $mpdf->lMargin + ($mpdf->pgwidth/2 - $hspacing/2 - $pw)/2;
  54. $x2 = $mpdf->lMargin + $mpdf->pgwidth/2 + $hspacing/2 + ($mpdf->pgwidth/2 - $hspacing/2 - $pw)/2;
  55. $y1 = $mpdf->tMargin + ($pgheight /2 - $vspacing/2 - $ph)/2;
  56. $y2 = $mpdf->tMargin + $pgheight /2 + $vspacing/2 + ($pgheight /2 - $vspacing/2 - $ph)/2;
  57. if ($i % 4 == 1) { $x = $x1; $y = $y1; }
  58. else if ($i % 4 == 2) { $x = $x2; $y = $y1; }
  59. else if ($i % 4 == 3) { $x = $x1; $y = $y2; }
  60. else if ($i % 4 == 0) { $x = $x2; $y = $y2; }
  61. $tplIdx = $mpdf->ImportPage($i, 0,0,$pw,$pph[$i]);
  62. if ($align=='T') { $mpdf->UseTemplate($tplIdx, $x, $y, $pw, $pph[$i]); }
  63. else { $mpdf->UseTemplate($tplIdx, $x, ($y + (($ph - $pph[$i])/2)), $pw, $pph[$i]); }
  64. if ($border >= 0) { $mpdf->Rect($x-$border, $y-$border, $pw+2*$border, $ph+2*$border); }
  65. }
  66. $mpdf->Output();
  67. exit;
  68. //==============================================================
  69. function SinglePage($html, $pw, $ph, $minK=1, $inc=0.1) {
  70. // returns height of page
  71. global $mpdf;
  72. $mpdf->AddPage('','','','','','',($mpdf->w - $pw),'',($mpdf->h - $ph),0,0);
  73. $k = 1;
  74. $currpage = $mpdf->page;
  75. $mpdf->WriteHTML($html);
  76. $newpage = $mpdf->page;
  77. while($currpage != $newpage) {
  78. for($u=0;$u<=($newpage-$currpage);$u++) {
  79. // DELETE PAGE - the added page
  80. unset($mpdf->pages[$mpdf->page]);
  81. if (isset($mpdf->ktAnnots[$mpdf->page])) { unset( $mpdf->ktAnnots[$mpdf->page] ); }
  82. if (isset($mpdf->tbrot_Annots[$mpdf->page])) { unset( $mpdf->tbrot_Annots[$mpdf->page] ); }
  83. if (isset($mpdf->kwt_Annots[$mpdf->page])) { unset( $mpdf->kwt_Annots[$mpdf->page] ); }
  84. if (isset($mpdf->PageAnnots[$mpdf->page])) { unset( $mpdf->PageAnnots[$mpdf->page] ); }
  85. if (isset($mpdf->ktBlock[$mpdf->page])) { unset( $mpdf->ktBlock[$mpdf->page] ); }
  86. if (isset($mpdf->PageLinks[$mpdf->page])) { unset( $mpdf->PageLinks[$mpdf->page] ); }
  87. if (isset($mpdf->pageoutput[$mpdf->page])) { unset( $mpdf->pageoutput[$mpdf->page] ); }
  88. // Go to page before - so can addpage
  89. $mpdf->page--;
  90. }
  91. // mPDF 2.4 Float Images
  92. if (count($mpdf->floatbuffer)) {
  93. $mpdf->objectbuffer[] = $mpdf->floatbuffer['objattr'];
  94. $mpdf->printobjectbuffer(false);
  95. $mpdf->objectbuffer = array();
  96. $mpdf->floatbuffer = array();
  97. $mpdf->float = false;
  98. }
  99. $k += $inc;
  100. if ((1/$k) < $minK) { die("Page no. ".$mpdf->page." is too large to fit"); }
  101. $w = $pw * $k;
  102. $h = $ph * $k;
  103. $mpdf->_beginpage('','',($mpdf->w - $w),'',($mpdf->h - $h));
  104. $currpage = $mpdf->page;
  105. $mpdf->_out('2 J');
  106. $mpdf->_out(sprintf('%.2f w',0.1*$mpdf->k));
  107. $mpdf->SetFont($mpdf->default_font,'',$mpdf->default_font_size ,true,true); // forces write
  108. $mpdf->SetDrawColor(0);
  109. $mpdf->SetFillColor(255);
  110. $mpdf->SetTextColor(0);
  111. $mpdf->ColorFlag=false;
  112. // Start Transformation
  113. $mpdf->StartTransform();
  114. $mpdf->transformScale((100/$k), (100/$k), 0, 0);
  115. $mpdf->WriteHTML($html);
  116. $newpage = $mpdf->page;
  117. //Stop Transformation
  118. $mpdf->StopTransform();
  119. }
  120. return ($mpdf->y / $k);
  121. }
  122. ?>