pluginGino-mpdf
 All Data Structures Namespaces Files Functions Variables Pages
plugin.mpdf.php
Go to the documentation of this file.
1 <?php
428 namespace Gino\Plugin;
429 
436 define('_MPDF_URI', SITE_WWW.'/lib/mpdf/');
437 
438 require_once(LIB_DIR.OS."mpdf".OS."mpdf.php");
439 require_once(LIB_DIR.OS."func.mpdf.php");
440 
453 class gino_mpdf {
454 
455  protected $_registry;
456 
462  protected $_html;
463 
469  protected $_pdf;
470 
481  function __construct($options=array()) {
482 
483  $this->_html = \Gino\gOpt('html', $options, false);
484 
485  $this->_registry = \Gino\registry::instance();
486  $this->_pdf = null;
487  }
488 
494  public static function defineBasicOptions() {
495 
496  $options = array(
497  'debug'=>false,
498  'css_file' => array('css/mpdf.css'),
499  'title' => 'Pdf document',
500  'author' => 'Otto Srl',
501  'creator' => 'Marco Guidotti',
502  'landscape'=>false,
503  'top-margin'=>20,
504  'bottom-margin'=>30,
505  'progressBar'=>false,
506  'progbar_heading'=>"Generazione pdf - Stato di avanzamento",
507  );
508 
509  return $options;
510  }
511 
520  public function header($options=array()) {
521 
522  return null;
523  }
524 
533  public function footer($options=array()) {
534 
535  return null;
536  }
537 
544  public function content($options=array()) {
545 
546  return null;
547  }
548 
559  protected function defaultHeader($options=array()) {
560 
561  $text_left = \Gino\gOpt('text_left', $options, null);
562  $text_right = \Gino\gOpt('text_right', $options, null);
563  $title = \Gino\gOpt('title', $options, null);
564 
565  $header = "<table width=\"100%\"><tr>
566  <td style=\"font-size: 8pt; text-align:left;\">".$this->mText($text_left)."</td>
567  <td style=\"font-size: 8pt; text-align:right;\">".$this->mText($text_right)."</td>
568  </tr></table>";
569 
570  if($title) $header .= "<div class=\"title_header\">$title</div>";
571 
572  return $header;
573  }
574 
584  protected function defaultFooter($options=array()) {
585 
586  $text1 = \Gino\gOpt('text1', $options, null);
587  $text2 = \Gino\gOpt('text2', $options, null);
588 
589  $footer = "<div style=\"border-top:1px solid #666; padding-top:3mm;\">";
590  $footer .= "<table width=\"100%\"><tr>";
591 
592  $width_sx = $text2 ? 20 : 80;
593 
594  $footer .= "<td width=\"".$width_sx."%\" style=\"text-align:left; font-size:6pt;\">$text1</td>";
595  if($text2) $footer .= "<td width=\"60%\" style=\"text-align:center; font-size:6pt;\">$text2</td>";
596 
597  $footer .= "<td width=\"20%\" style=\"text-align:right; font-size:6pt;\">"._("Pagina")." _NUMPAGE_ "._("di")." _TOTPAGE_</td>";
598  $footer .= "</tr></table>";
599  $footer .= "</div>";
600 
601  return $footer;
602  }
603 
613  protected function setFileName($options=array()) {
614 
615  $name = \Gino\gOpt('name', $options, 'doc');
616  $date = \Gino\gOpt('date', $options, false);
617 
618  if($date)
619  {
620  $date = date("Ymd");
621  $name .= '-'.$date;
622  }
623  $name .= '.pdf';
624 
625  return $name;
626  }
627 
634  protected function frontpage($title) {
635 
636  $buffer = "<section>";
637  $buffer .= "<div class=\"cover\">".$this->mText($title)."</div>";
638  $buffer .= "</section>";
639 
640  return $buffer;
641  }
642 
651  protected function repeatChar($string, $num, $break=null) {
652 
653  $buffer = '';
654  if($num)
655  {
656  $count = 1;
657  for($i=1; $i<=$num; $i++)
658  {
659  $buffer .= $string;
660 
661  if($count == $break)
662  {
663  $buffer .= "<br />";
664  $count = 1;
665  }
666  else $count++;
667  }
668  }
669  else $buffer = $string;
670 
671  return $buffer;
672  }
673 
700  public function pdfFromPage($content, $opts=array()) {
701 
702  $link_return = \Gino\gOpt('link_return', $opts, null);
703  $output = \Gino\gOpt('output', $opts, 'inline');
704  $debug = \Gino\gOpt('debug', $opts, null);
705 
706  $css_file = \Gino\gOpt('css_file', $opts, null);
707  $css_html = \Gino\gOpt('css_html', $opts, null);
708  $img_dir = \Gino\gOpt('img_dir', $opts, null);
709  $save_dir = \Gino\gOpt('save_dir', $opts, null);
710  $filename = \Gino\gOpt('filename', $opts, null);
711 
712  // Def options
713  $options = gino_mpdf::defineBasicOptions();
714 
715  $options['output'] = $output;
716  if(is_bool($debug)) $options['debug'] = $debug;
717 
718  if($css_file) $options['css_file'] = $css_file;
719  if($css_html) $options['css_html'] = $css_html;
720  if($img_dir) $options['img_dir'] = $img_dir;
721  if($save_dir) $options['save_dir'] = $save_dir;
722  if($filename) $options['filename'] = $filename;
723 
724  $options['content'] = \Gino\htmlToPdf($content);
725  // /Def
726 
727  $pdf = $this->create($options);
728 
729  if($this->_html)
730  {
731  return $pdf;
732  }
733 
734  if($link_return)
735  {
736  $this->redirect($link_return);
737  }
738  return null;
739  }
740 
793  public function create($options=array()) {
794 
795  $output = array_key_exists('output', $options) ? $options['output'] : null;
796  $debug = array_key_exists('debug', $options) ? $options['debug'] : false;
797 
798  $filename = \Gino\gOpt('filename', $options, 'doc.pdf');
799  $img_dir = \Gino\gOpt('img_dir', $options, null);
800  $save_dir = \Gino\gOpt('save_dir', $options, '');
801  $css_html = \Gino\gOpt('css_html', $options, null);
802 
803  $save_dir = (substr($save_dir, -1) != '/' && $save_dir != '') ? $save_dir.'/' : $save_dir;
804 
805  $pdf = new plugin_mpdf(
806  array(
807  'output'=>$output,
808  'debug'=>$debug
809  )
810  );
811 
812  $this->_pdf = $pdf;
813  $options['object'] = $this;
814 
815  // Html
816  if($this->_html)
817  {
818  $content = \Gino\gOpt('content', $options, null);
819  if(!$content) $content = $this->content($options);
820 
821  if(is_array($content))
822  $content = implode("<br />", $content);
823 
824  if($css_html)
825  $this->_registry->addCss($css_html);
826 
827  return $content;
828  }
829  // /Html
830 
831  if($output == 'file')
832  {
833  if(!is_dir($save_dir)) mkdir($save_dir, 0777, true);
834  $file = $save_dir.$filename;
835  }
836  else $file = $filename;
837 
838  $res = $pdf->makeFile($file, $options);
839  return $res;
840  }
841 
850  protected function redirect($link) {
851 
852  echo "<script type=\"text/javascript\">window.location.href='".$link."';</script>";
853  exit();
854  }
855 
866  protected function mText($string, $options=array()) {
867 
868  if($this->_html)
869  {
870  $type = \Gino\gOpt('type', $options, 'text');
871 
872  if($type == 'textarea')
873  return \Gino\htmlCharsText($string);
874  elseif($type == 'editor')
875  return \Gino\htmlChars($string);
876  else
877  return \Gino\htmlChars($string);
878  }
879  else return $this->_pdf->text($string, $options);
880  }
881 
892  protected function convertHtmlToPdf($html, $exit=true) {
893 
894  if($this->_html)
895  {
896  return $html;
897  }
898  else
899  {
900  return $this->_pdf->htmlCreate($html, $exit);
901  }
902  }
903 
910  protected function breakpage() {
911 
912  if(is_object($this->_pdf) && !$this->_html)
913  {
914  return $this->_pdf->breakpage();
915  }
916  else return '';
917  }
918 
935  protected function printTable($data=array(), $header=array(), $options=array()){
936 
937  $class = array_key_exists('class', $options) ? $options['class'] : '';
938  $style = array_key_exists('style', $options) ? $options['style'] : '';
939  $autosize = array_key_exists('autosize', $options) ? $options['autosize'] : 1;
940  $border = array_key_exists('border', $options) ? $options['border'] : 0;
941 
942  if($class) $class = " class=\"$class\"";
943  if($style) $style = " style=\"$style\"";
944  if($autosize) $autosize = " autosize=\"$autosize\"";
945  if($border) $border = " border=\"$border\"";
946 
947  $buffer = "<table".$autosize.$border.$style.$class.">";
948  $buffer .= "<thead>";
949  $buffer .= "<tr>";
950  if(sizeof($header) > 0)
951  {
952  foreach($header AS $value)
953  {
954  $buffer .= $value;
955  }
956  }
957  $buffer .= "</tr>";
958  $buffer .= "</thead>";
959 
960  $buffer .= "<tbody>";
961  if(sizeof($data) > 0)
962  {
963  foreach($data AS $record)
964  {
965  $buffer .= "<tr>";
966  if(sizeof($record) > 0)
967  {
968  foreach($record AS $field)
969  {
970  $buffer .= "<td valign=\"top\">".$field."</td>";
971  }
972  }
973  $buffer .= "</tr>";
974  }
975  }
976  $buffer .= "</tbody>";
977  $buffer .= "</table>";
978 
979  return $buffer;
980  }
981 
999  protected function arrangeTable($items, $selected, $options=array()) {
1000 
1001  $cols = \Gino\gOpt('cols', $options, 2);
1002  $field = \Gino\gOpt('field', $options, null);
1003  $separator = \Gino\gOpt('separator', $options, null);
1004  $table_class = \Gino\gOpt('table_class', $options, null);
1005  $td_class = \Gino\gOpt('td_class', $options, null);
1006  $add_rows = \Gino\gOpt('add_rows', $options, null);
1007 
1008  $table_class = $table_class ? " class=\"$table_class\"" : '';
1009  $td_class = $td_class ? " class=\"$td_class\"" : '';
1010 
1011  $buffer = '';
1012 
1013  if(count($items))
1014  {
1015  $items_for_col = ceil(count($items)/$cols);
1016 
1017  $i = 1;
1018  $col1 = $col2 = $col3 = array();
1019  foreach($items AS $item)
1020  {
1021  if($cols == 3)
1022  {
1023  if($i <= $items_for_col)
1024  $col1[] = $item;
1025  elseif($i <= $items_for_col*2 && $i > $items_for_col)
1026  $col2[] = $item;
1027  elseif($i > $items_for_col*2)
1028  $col3[] = $item;
1029  }
1030  elseif($cols == 2)
1031  {
1032  if($i <= $items_for_col)
1033  $col1[] = $item;
1034  elseif($i > $items_for_col)
1035  $col2[] = $item;
1036  }
1037 
1038  $i++;
1039  }
1040 
1041  $buffer .= "<table".$table_class.">";
1042 
1043  for($i=0, $end=$items_for_col; $i<=$end; $i++)
1044  {
1045  $buffer .= "<tr>";
1046 
1047  if(isset($col1[$i]))
1048  {
1049  $i_col1 = $col1[$i];
1050  $checked = in_array($i_col1->id, $selected) ? "checked=\"checked\"" : '';
1051 
1052  $text = $this->parseFieldForArrangeTable($i_col1, $field, array('separator'=>$separator));
1053  $buffer .= "<td".$td_class."><input type=\"checkbox\" $checked /> ".$this->mText($text)."</td>";
1054  }
1055  else
1056  {
1057  $buffer .= "<td".$td_class."></td>";
1058  }
1059 
1060  if(isset($col2[$i]))
1061  {
1062  $i_col2 = $col2[$i];
1063  $checked = in_array($i_col2->id, $selected) ? "checked=\"checked\"" : '';
1064 
1065  $text = $this->parseFieldForArrangeTable($i_col2, $field, array('separator'=>$separator));
1066  $buffer .= "<td".$td_class."><input type=\"checkbox\" $checked /> ".$this->mText($text)."</td>";
1067  }
1068  else
1069  {
1070  $buffer .= "<td".$td_class."></td>";
1071  }
1072 
1073  if(isset($col3[$i]))
1074  {
1075  $i_col3 = $col3[$i];
1076  $checked = in_array($i_col3->id, $selected) ? "checked=\"checked\"" : '';
1077 
1078  $text = $this->parseFieldForArrangeTable($i_col3, $field, array('separator'=>$separator));
1079  $buffer .= "<td".$td_class."><input type=\"checkbox\" $checked /> ".$this->mText($text)."</td>";
1080  }
1081  else
1082  {
1083  $buffer .= "<td".$td_class."></td>";
1084  }
1085  $buffer .= "</tr>";
1086  }
1087  if($add_rows) $buffer .= $add_rows;
1088 
1089  $buffer .= "</table>";
1090  }
1091 
1092  return $buffer;
1093  }
1094 
1107  private function parseFieldForArrangeTable($obj, $field, $options=array()) {
1108 
1109  $separator = \Gino\gOpt('separator', $options, ' ');
1110 
1111  if(is_string($field))
1112  {
1113  return $obj->$field;
1114  }
1115  elseif(is_array($field))
1116  {
1117  $text = '';
1118  $i = 1;
1119  $end = count($field);
1120 
1121  foreach($field AS $f)
1122  {
1123  $text .= $obj->$f;
1124 
1125  if($i < $end) $text .= $separator;
1126 
1127  $i++;
1128  }
1129  return $text;
1130  }
1131  else return null;
1132  }
1133 }
1134 
1142 class custom_mpdf extends \mPDF {
1143 
1147  function StartProgressBarOutput($mode=1) {
1148 
1149  ob_end_flush();
1150 
1151  // must be relative path, or URI (not a file system path)
1152  if (!defined('_MPDF_URI')) {
1153  $this->progressBar = false;
1154  if ($this->debug) { $this->Error("You need to define _MPDF_URI to use the progress bar!"); }
1155  else return false;
1156  }
1157  $this->progressBar = $mode;
1158  if ($this->progbar_altHTML) {
1159  echo $this->progbar_altHTML;
1160  }
1161  else {
1162  echo '<html>
1163  <head>
1164  <title>mPDF File Progress</title>
1165  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1166  <link rel="stylesheet" type="text/css" href="'._MPDF_URI.'progbar.css" />
1167  </head>';
1168 
1169  echo '<body>
1170  <div class="main">
1171  <div class="heading">'.$this->progbar_heading.'</div>
1172  <div class="demo">';
1173 
1174  if($this->progressBar==2)
1175  {
1176  echo '<table width="100%">';
1177 
1178  echo '<tr><td style="width: 50%;">
1179  <span class="barheading">Writing HTML code</span> <br/>
1180  <div class="progressBar">
1181  <div id="element1" class="innerBar">&nbsp;</div>
1182  </div>
1183  <span class="code" id="box1"></span>
1184  </td>';
1185  echo '<td style="width: 50%;">
1186  <span class="barheading">Autosizing elements</span> <br/>
1187  <div class="progressBar">
1188  <div id="element4" class="innerBar">&nbsp;</div>
1189  </div>
1190  <span class="code" id="box4"></span>
1191  <br/><br/>
1192  <span class="barheading">Writing Tables</span> <br/>
1193  <div class="progressBar">
1194  <div id="element7" class="innerBar">&nbsp;</div>
1195  </div>
1196  <span class="code" id="box7"></span>
1197  </td></tr>';
1198 
1199  echo '<tr><td><br /><br /></td><td></td></tr>';
1200  echo '<tr><td style="width: 50%;">';
1201  }
1202 
1203  echo '<span class="barheading">Writing PDF file</span> <br/>
1204  <div class="progressBar">
1205  <div id="element2" class="innerBar">&nbsp;</div>
1206  </div>
1207  <span class="code" id="box2"></span>';
1208 
1209  if($this->progressBar==2)
1210  {
1211  echo '</td>';
1212  echo '<td style="width: 50%;">
1213  <span class="barheading">Memory usage</span> <br/>
1214  <div class="progressBar">
1215  <div id="element5" class="innerBar">&nbsp;</div>
1216  </div>
1217  <span id="box5">0</span> '.ini_get("memory_limit").'<br />
1218  <br/><br/>
1219  <span class="barheading">Memory usage (peak)</span> <br/>
1220  <div class="progressBar">
1221  <div id="element6" class="innerBar">&nbsp;</div>
1222  </div>
1223  <span id="box6">0</span> '.ini_get("memory_limit").'<br />
1224  </td></tr>';
1225 
1226  echo '</table>';
1227  }
1228  echo '<br/><br/>
1229  <span id="box3"></span>
1230  </div>';
1231  }
1232  ob_flush();
1233  flush();
1234  }
1235 }
1236 
1245 
1251  private $_output;
1252 
1258  private $_debug;
1259 
1273  function __construct($options=array()){
1274 
1275  if(array_key_exists('output', $options) && $options['output'])
1276  {
1277  $res = array_keys(self::outputs(), $options['output']);
1278 
1279  if($res && count($res))
1280  $this->_output = $res[0];
1281  else
1282  $this->_output = 'I';
1283  }
1284  else $this->_output = 'I';
1285 
1286  $this->_debug = array_key_exists('debug', $options) && $options['debug'] ? $options['debug'] : false;
1287  }
1288 
1294  public static function outputs() {
1295 
1296  return array('F'=>'file', 'I'=>'inline', 'D'=>'download', 'S'=>'string');
1297  }
1298 
1309  public static function setPhpParams($options=array()) {
1310 
1311  $disable_error = \Gino\gOpt('disable_error', $options, false);
1312  $memory_limit = \Gino\gOpt('memory_limit', $options, null);
1313  $max_execution_time = \Gino\gOpt('max_execution_time', $options, null);
1314 
1315  if(!is_null($memory_limit))
1316  {
1317  ini_set('memory_limit', $max_execution_time);
1318  }
1319 
1320  if(!is_null($max_execution_time))
1321  {
1322  ini_set('max_execution_time', $max_execution_time);
1323  }
1324 
1325  if($disable_error)
1326  {
1327  error_reporting(0);
1328  }
1329 
1330  return null;
1331  }
1332 
1351  public static function getMemoryUsage($options=array()) {
1352 
1353  $memory_usage = \Gino\gOpt('memory_usage', $options, false);
1354  $memory_peak_usage = \Gino\gOpt('memory_peak_usage', $options, false);
1355 
1356  if($memory_usage)
1357  {
1358  echo \Gino\convertSize(memory_get_usage(true))."<br />";
1359  }
1360 
1361  if($memory_peak_usage)
1362  {
1363  echo \Gino\convertSize(memory_get_peak_usage(true))."<br />";
1364  }
1365 
1366  return null;
1367  }
1368 
1375  private function conformFile($filename='') {
1376 
1377  if($filename)
1378  {
1379  $dirname = dirname($filename);
1380  if(!is_dir($dirname))
1381  {
1382  $filename = basename($filename);
1383  }
1384  }
1385  else $filename = '';
1386 
1387  return $filename;
1388  }
1389 
1407  public function htmlStart($options=array()){
1408 
1409  $css_file = array_key_exists('css_file', $options) ? $options['css_file'] : "css/mpdf.css";
1410  $css_style = array_key_exists('css_style', $options) ? $options['css_style'] : '';
1411  $header = array_key_exists('header', $options) ? $options['header'] : '';
1412  $footer = array_key_exists('footer', $options) ? $options['footer'] : '';
1413 
1414  $html = "<html>";
1415  $html .= "<head>";
1416 
1417  if(is_array($css_file) && count($css_file))
1418  {
1419  foreach($css_file AS $item)
1420  {
1421  $html .= "<link href=\"$item\" type=\"text/css\" rel=\"stylesheet\" />";
1422  }
1423  }
1424  else
1425  {
1426  $html .= "<link href=\"$css_file\" type=\"text/css\" rel=\"stylesheet\" />";
1427  }
1428 
1429  if($css_style)
1430  $html .= "<style>".$css_style."</style>";
1431 
1432  $html .= "</head>";
1433  $html .= "<body>\n";
1434 
1435  if(is_bool($footer) && $footer===false)
1436  {
1437  $footer = '';
1438  }
1439  elseif(is_string($footer) && $footer)
1440  {
1441  if(preg_match('#_NUMPAGE_#', $footer))
1442  $footer = preg_replace('#_NUMPAGE_#', '{PAGENO}', $footer);
1443  if(preg_match('#_TOTPAGE_#', $footer))
1444  $footer = preg_replace('#_TOTPAGE_#', '{nb}', $footer);
1445  }
1446  else
1447  {
1448  $footer = $this->defaultFooter();
1449  }
1450  $html .= "
1451 <!--mpdf
1452 <htmlpageheader name=\"myheader\">
1453 $header
1454 </htmlpageheader>
1455 
1456 <htmlpagefooter name=\"myfooter\">
1457 $footer
1458 </htmlpagefooter>
1459 
1460 <sethtmlpageheader name=\"myheader\" value=\"on\" show-this-page=\"1\" />
1461 <sethtmlpagefooter name=\"myfooter\" value=\"on\" />
1462 mpdf-->";
1463 
1464  return $html;
1465  }
1466 
1472  public function defaultFooter() {
1473 
1474  $footer = "
1475 <div style=\"border-top: 1px solid #000000; font-size: 6pt; text-align: center; padding-top: 3mm; \">
1476 "._("Pagina")." {PAGENO} "._("di")." {nb}
1477 </div>";
1478  return $footer;
1479  }
1480 
1486  public function htmlEnd(){
1487 
1488  $html = "</body>\n";
1489  $html .= "</html>\n";
1490  return $html;
1491  }
1492 
1500  public function htmlCreate($html){
1501 
1502  $html = \Gino\pdfHtmlToEntities($html);
1503  $html = utf8_encode($html);
1504 
1505  if($this->_debug)
1506  echo $html;
1507  else
1508  return $html;
1509  }
1510 
1528  public function definePage($text, $options=array()) {
1529 
1530  $css_file = \Gino\gOpt('css_file', $options, null);
1531  $header = \Gino\gOpt('header', $options, null);
1532  $footer = \Gino\gOpt('footer', $options, null);
1533  $debug_exit = \Gino\gOpt('debug_exit', $options, true);
1534 
1535  $buffer = $this->htmlStart(array('header'=>$header, 'footer'=>$footer, 'css_file'=>$css_file));
1536  $buffer .= $text;
1537  $buffer .= $this->htmlEnd();
1538  $buffer = $this->htmlCreate($buffer);
1539 
1540  if($this->_debug && $debug_exit) exit();
1541 
1542  return $buffer;
1543  }
1544 
1629  public function makeFile($filename, $options=array()){
1630 
1631  $title = \Gino\gOpt('title', $options, '');
1632  $author = \Gino\gOpt('author', $options, '');
1633  $creator = \Gino\gOpt('creator', $options, '');
1634  $watermark = \Gino\gOpt('watermark', $options, false);
1635  $watermark_text = \Gino\gOpt('watermark_text', $options, _("esempio"));
1636 
1637  $format = array_key_exists('format', $options) && $options['format'] ? $options['format'] : 'A4';
1638  $landscape = \Gino\gOpt('landscape', $options, false);
1639  $mode = array_key_exists('mode', $options) && $options['mode'] ? $options['mode'] : 'utf-8';
1640 
1641  $protection = \Gino\gOpt('protection', $options, null);
1642  $user_password = \Gino\gOpt('user_password', $options, '');
1643  $owner_password = \Gino\gOpt('owner_password', $options, '');
1644 
1645  $default_font_size = \Gino\gOpt('font_size', $options, 0);
1646  $default_font = \Gino\gOpt('font', $options, '');
1647  $orientation = \Gino\gOpt('orientation', $options, 'P');
1648  $simple_tables = \Gino\gOpt('simpleTables', $options, false);
1649  $show_stats = \Gino\gOpt('showStats', $options, false);
1650  $progress_bar = \Gino\gOpt('progressBar', $options, false);
1651  $progress_bar_heading = \Gino\gOpt('progbar_heading', $options, null);
1652  $progress_bar_alt = \Gino\gOpt('progbar_altHTML', $options, null);
1653 
1654  if($landscape) $format .= '-L';
1655 
1656  if($format == 'A4' || $format == 'A3')
1657  {
1658  $left_margin = 20;
1659  $right_margin = 15;
1660  $top_margin = 48;
1661  $bottom_margin = 25;
1662  $header_margin = 10;
1663  $footer_margin = 10;
1664  }
1665  else // Valori di default come nel costruttore della classe mPDF (MPDF/mpdf.php)
1666  {
1667  $left_margin = 15;
1668  $right_margin = 15;
1669  $top_margin = 16;
1670  $bottom_margin = 16;
1671  $header_margin = 9;
1672  $footer_margin = 9;
1673  }
1674 
1675  // Personalizzazione dei parametri
1676  if(array_key_exists('top-margin', $options) && !is_null($options['top-margin'])) $top_margin = $options['top-margin'];
1677  if(array_key_exists('header-margin', $options) && !is_null($options['header-margin'])) $header_margin = $options['header-margin'];
1678  if(array_key_exists('bottom-margin', $options) && !is_null($options['bottom-margin'])) $bottom_margin = $options['bottom-margin'];
1679  if(array_key_exists('footer-margin', $options) && !is_null($options['footer-margin'])) $footer_margin = $options['footer-margin'];
1680 
1681  $mpdf = new custom_mpdf(
1682  $mode,
1683  $format,
1684  $default_font_size,
1685  $default_font,
1686  $left_margin,
1687  $right_margin,
1688  $top_margin,
1689  $bottom_margin,
1690  $header_margin,
1691  $footer_margin,
1692  $footer_margin,
1693  $orientation
1694  );
1695 
1696  $mpdf->simpleTables = $simple_tables;
1697  $mpdf->showStats = $show_stats;
1698  $mpdf->useOnlyCoreFonts = true;
1699  if(is_array($protection)) {
1700  $mpdf->SetProtection($protection, $user_password, $owner_password);
1701  }
1702  $mpdf->SetTitle($title);
1703  $mpdf->SetAuthor($author);
1704  $mpdf->SetCreator($creator);
1705  $mpdf->SetWatermarkText($watermark_text);
1706  $mpdf->showWatermarkText = $watermark;
1707  $mpdf->watermark_font = 'DejaVuSansCondensed';
1708  $mpdf->watermarkTextAlpha = 0.1;
1709  $mpdf->SetDisplayMode('fullpage');
1710 
1711  //$mpdf->allow_charset_conversion = true;
1712  //$mpdf->charset_in = 'iso-8859-1'; // default 'utf-8'
1713  //$mpdf->shrink_tables_to_fit = 0; // prevent all tables from resizing
1714 
1715  // Progress bar
1716  if($progress_bar)
1717  {
1718  if($progress_bar_heading) $mpdf->progbar_heading = $progress_bar_heading;
1719  if($progress_bar_alt) $mpdf->progbar_altHTML = $progress_bar_alt;
1720  if($progress_bar === true)
1721  $progress_bar = 1;
1722 
1723  $mpdf->StartProgressBarOutput($progress_bar);
1724  }
1725 
1726  // Def contents
1727  $content = \Gino\gOpt('content', $options, null);
1728  $object = \Gino\gOpt('object', $options, null);
1729  $img_dir = \Gino\gOpt('img_dir', $options, null);
1730 
1731  if(!$content && is_object($object)) $content = $object->content($options);
1732 
1733  if(is_array($content))
1734  {
1735  $html = $content;
1736  }
1737  else
1738  {
1739  if(is_object($object))
1740  {
1741  $options['header'] = $object->header(array('img_dir'=>$img_dir));
1742  $options['footer'] = $object->footer(array('img_dir'=>$img_dir));
1743  }
1744 
1745  $html = $this->definePage($content, $options);
1746  }
1747  // /Def
1748 
1749  if(is_string($html))
1750  {
1751  $mpdf->WriteHTML($html);
1752  }
1753  elseif(is_array($html) AND sizeof($html) > 0)
1754  {
1755  $pages = $html;
1756  for($i=0, $end=sizeof($pages); $i<$end; $i++)
1757  {
1758  if($i==0)
1759  {
1760  $mpdf->WriteHTML($pages[$i]);
1761  }
1762  else
1763  {
1764  if(is_array($pages[$i]))
1765  {
1766  $orientation_page = array_key_exists('orientation', $pages[$i]) ? $pages[$i]['orientation'] : 'P';
1767  $html = array_key_exists('html', $pages[$i]) ? $pages[$i]['html'] : '';
1768  }
1769  else
1770  {
1771  $orientation_page = $landscape ? 'L' : 'P';
1772  $html = $pages[$i];
1773  }
1774  $mpdf->AddPageByArray(array('orientation'=>$orientation_page));
1775  $mpdf->WriteHTML($html);
1776  }
1777  }
1778  }
1779 
1780  $filename = $this->conformFile($filename);
1781 
1782  if($this->_output == 'S')
1783  {
1784  return $mpdf->Output($filename, $this->_output);
1785  }
1786  elseif($this->_output == 'I' || $this->_output == 'D')
1787  {
1788  $mpdf->Output($filename, $this->_output);
1789  exit();
1790  }
1791  else // F
1792  {
1793  $mpdf->Output($filename, $this->_output);
1794  return true;
1795  }
1796  }
1797 
1815  public function sendToEmail($mpdf_output, $filename, $options=array()){
1816 
1817  $mailto = array_key_exists('mailto', $options) ? $options['mailto'] : '';
1818  $from_name = array_key_exists('from_name', $options) ? $options['from_name'] : '';
1819  $from_mail = array_key_exists('from_mail', $options) ? $options['from_mail'] : '';
1820  $replyto = array_key_exists('replyto', $options) ? $options['replyto'] : '';
1821  $subject = array_key_exists('subject', $options) ? $options['subject'] : '';
1822  $message = array_key_exists('message', $options) ? $options['message'] : '';
1823 
1824  $content = chunk_split(base64_encode($mpdf_output));
1825 
1826  $filename = $this->conformFile($filename);
1827 
1828  $uid = md5(uniqid(time()));
1829 
1830  $header = "From: ".$from_name." <".$from_mail.">\r\n";
1831  $header .= "Reply-To: ".$replyto."\r\n";
1832  $header .= "MIME-Version: 1.0\r\n";
1833  $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
1834  $header .= "This is a multi-part message in MIME format.\r\n";
1835  $header .= "--".$uid."\r\n";
1836  $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
1837  $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
1838  $header .= $message."\r\n\r\n";
1839  $header .= "--".$uid."\r\n";
1840  $header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
1841  $header .= "Content-Transfer-Encoding: base64\r\n";
1842  $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
1843  $header .= $content."\r\n\r\n";
1844  $header .= "--".$uid."--";
1845  $is_sent = @mail($mailto, $subject, "", $header);
1846 
1847  exit();
1848  }
1849 
1856  public function dataToDB($mpdf_output) {
1857 
1858  $string = bin2hex($mpdf_output);
1859  $string = "0x".$string;
1860 
1861  return $string;
1862  }
1863 
1869  public function getToDataDB($data) {
1870 
1871  $pdf = pack("H*", $data );
1872  header('Content-Type: application/pdf');
1873  header('Content-Length: '.strlen($pdf));
1874  header('Content-disposition: inline; filename="'.$name.'"');
1875  header('Cache-Control: public, must-revalidate, max-age=0');
1876  header('Pragma: public');
1877  header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
1878  header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
1879  echo $pdf;
1880  exit;
1881  }
1882 
1888  public function breakpage(){
1889 
1890  return "<pagebreak />";
1891  }
1892 
1899  public function longText($text){
1900 
1901  if(!empty($text))
1902  $text = "<div class=\"longtext\">$text</div>";
1903 
1904  return $text;
1905  }
1906 
1922  public function text($text, $options=array()){
1923 
1924  $class = \Gino\gOpt('class', $options, '');
1925  $style = \Gino\gOpt('style', $options, '');
1926  $other= \Gino\gOpt('other', $options, '');
1927  $type = \Gino\gOpt('type', $options, 'text');
1928 
1929  if($class)
1930  $class = "class=\"$class\"";
1931  if($style)
1932  $style = "style=\"$style\"";
1933 
1934  if($type == 'textarea')
1935  $method = '\Gino\pdfChars_Textarea';
1936  elseif($type == 'editor')
1937  $method = '\Gino\pdfTextChars';
1938  else
1939  $method = '\Gino\pdfChars';
1940 
1941  $text = $method($text);
1942 
1943  if($class OR $style OR $other)
1944  {
1945  $text = "<span $class$style$other>$text</span>";
1946  }
1947 
1948  return $text;
1949  }
1950 }
1951 ?>