428 namespace Gino\Plugin;
436 define(
'_MPDF_URI', SITE_WWW.
'/lib/mpdf/');
438 require_once(LIB_DIR.OS.
"mpdf".OS.
"mpdf.php");
439 require_once(LIB_DIR.OS.
"func.mpdf.php");
483 $this->_html = \Gino\gOpt(
'html', $options,
false);
485 $this->_registry = \Gino\registry::instance();
498 'css_file' => array(
'css/mpdf.css'),
499 'title' =>
'Pdf document',
500 'author' =>
'Otto Srl',
501 'creator' =>
'Marco Guidotti',
505 'progressBar'=>
false,
506 'progbar_heading'=>
"Generazione pdf - Stato di avanzamento",
520 public function header($options=array()) {
533 public function footer($options=array()) {
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);
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>
570 if($title) $header .=
"<div class=\"title_header\">$title</div>";
586 $text1 = \Gino\gOpt(
'text1', $options, null);
587 $text2 = \Gino\gOpt(
'text2', $options, null);
589 $footer =
"<div style=\"border-top:1px solid #666; padding-top:3mm;\">";
590 $footer .=
"<table width=\"100%\"><tr>";
592 $width_sx = $text2 ? 20 : 80;
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>";
597 $footer .=
"<td width=\"20%\" style=\"text-align:right; font-size:6pt;\">"._(
"Pagina").
" _NUMPAGE_ "._(
"di").
" _TOTPAGE_</td>";
598 $footer .=
"</tr></table>";
615 $name = \Gino\gOpt(
'name', $options,
'doc');
616 $date = \Gino\gOpt(
'date', $options,
false);
636 $buffer =
"<section>";
637 $buffer .=
"<div class=\"cover\">".$this->mText($title).
"</div>";
638 $buffer .=
"</section>";
657 for($i=1; $i<=$num; $i++)
669 else $buffer = $string;
702 $link_return = \Gino\gOpt(
'link_return', $opts, null);
703 $output = \Gino\gOpt(
'output', $opts,
'inline');
704 $debug = \Gino\gOpt(
'debug', $opts, null);
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);
715 $options[
'output'] = $output;
716 if(is_bool($debug)) $options[
'debug'] = $debug;
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;
727 $pdf = $this->
create($options);
793 public function create($options=array()) {
795 $output = array_key_exists(
'output', $options) ? $options[
'output'] : null;
796 $debug = array_key_exists(
'debug', $options) ? $options[
'debug'] :
false;
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);
803 $save_dir = (substr($save_dir, -1) !=
'/' && $save_dir !=
'') ? $save_dir.
'/' : $save_dir;
813 $options[
'object'] = $this;
818 $content = \Gino\gOpt(
'content', $options, null);
819 if(!$content) $content = $this->
content($options);
821 if(is_array($content))
822 $content = implode(
"<br />", $content);
825 $this->_registry->addCss($css_html);
831 if($output ==
'file')
833 if(!is_dir($save_dir)) mkdir($save_dir, 0777,
true);
834 $file = $save_dir.$filename;
836 else $file = $filename;
838 $res = $pdf->makeFile($file, $options);
852 echo
"<script type=\"text/javascript\">window.location.href='".$link.
"';</script>";
866 protected function mText($string, $options=array()) {
870 $type = \Gino\gOpt(
'type', $options,
'text');
872 if($type ==
'textarea')
873 return \Gino\htmlCharsText($string);
874 elseif($type ==
'editor')
875 return \Gino\htmlChars($string);
877 return \Gino\htmlChars($string);
879 else return $this->_pdf->text($string, $options);
900 return $this->_pdf->htmlCreate($html, $exit);
912 if(is_object($this->_pdf) && !$this->_html)
914 return $this->_pdf->breakpage();
935 protected function printTable($data=array(), $header=array(), $options=array()){
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;
942 if($class) $class =
" class=\"$class\"";
943 if($style) $style =
" style=\"$style\"";
944 if($autosize) $autosize =
" autosize=\"$autosize\"";
945 if($border) $border =
" border=\"$border\"";
947 $buffer =
"<table".$autosize.$border.$style.$class.
">";
948 $buffer .=
"<thead>";
950 if(
sizeof($header) > 0)
952 foreach($header AS $value)
958 $buffer .=
"</thead>";
960 $buffer .=
"<tbody>";
961 if(
sizeof($data) > 0)
963 foreach($data AS $record)
966 if(
sizeof($record) > 0)
968 foreach($record AS $field)
970 $buffer .=
"<td valign=\"top\">".$field.
"</td>";
976 $buffer .=
"</tbody>";
977 $buffer .=
"</table>";
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);
1008 $table_class = $table_class ?
" class=\"$table_class\"" :
'';
1009 $td_class = $td_class ?
" class=\"$td_class\"" :
'';
1015 $items_for_col = ceil(count($items)/$cols);
1018 $col1 = $col2 = $col3 = array();
1019 foreach($items AS $item)
1023 if($i <= $items_for_col)
1025 elseif($i <= $items_for_col*2 && $i > $items_for_col)
1027 elseif($i > $items_for_col*2)
1032 if($i <= $items_for_col)
1034 elseif($i > $items_for_col)
1041 $buffer .=
"<table".$table_class.
">";
1043 for($i=0, $end=$items_for_col; $i<=$end; $i++)
1047 if(isset($col1[$i]))
1049 $i_col1 = $col1[$i];
1050 $checked = in_array($i_col1->id, $selected) ?
"checked=\"checked\"" :
'';
1053 $buffer .=
"<td".$td_class.
"><input type=\"checkbox\" $checked /> ".$this->
mText($text).
"</td>";
1057 $buffer .=
"<td".$td_class.
"></td>";
1060 if(isset($col2[$i]))
1062 $i_col2 = $col2[$i];
1063 $checked = in_array($i_col2->id, $selected) ?
"checked=\"checked\"" :
'';
1066 $buffer .=
"<td".$td_class.
"><input type=\"checkbox\" $checked /> ".$this->
mText($text).
"</td>";
1070 $buffer .=
"<td".$td_class.
"></td>";
1073 if(isset($col3[$i]))
1075 $i_col3 = $col3[$i];
1076 $checked = in_array($i_col3->id, $selected) ?
"checked=\"checked\"" :
'';
1079 $buffer .=
"<td".$td_class.
"><input type=\"checkbox\" $checked /> ".$this->
mText($text).
"</td>";
1083 $buffer .=
"<td".$td_class.
"></td>";
1087 if($add_rows) $buffer .= $add_rows;
1089 $buffer .=
"</table>";
1109 $separator = \Gino\gOpt(
'separator', $options,
' ');
1111 if(is_string($field))
1113 return $obj->$field;
1115 elseif(is_array($field))
1119 $end = count($field);
1121 foreach($field AS $f)
1125 if($i < $end) $text .= $separator;
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!"); }
1157 $this->progressBar = $mode;
1158 if ($this->progbar_altHTML) {
1159 echo $this->progbar_altHTML;
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" />
1171 <div class="heading">'.$this->progbar_heading.
'</div>
1172 <div class="demo">';
1174 if($this->progressBar==2)
1176 echo
'<table width="100%">';
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"> </div>
1183 <span class="code" id="box1"></span>
1185 echo
'<td style="width: 50%;">
1186 <span class="barheading">Autosizing elements</span> <br/>
1187 <div class="progressBar">
1188 <div id="element4" class="innerBar"> </div>
1190 <span class="code" id="box4"></span>
1192 <span class="barheading">Writing Tables</span> <br/>
1193 <div class="progressBar">
1194 <div id="element7" class="innerBar"> </div>
1196 <span class="code" id="box7"></span>
1199 echo
'<tr><td><br /><br /></td><td></td></tr>';
1200 echo
'<tr><td style="width: 50%;">';
1203 echo
'<span class="barheading">Writing PDF file</span> <br/>
1204 <div class="progressBar">
1205 <div id="element2" class="innerBar"> </div>
1207 <span class="code" id="box2"></span>';
1209 if($this->progressBar==2)
1212 echo
'<td style="width: 50%;">
1213 <span class="barheading">Memory usage</span> <br/>
1214 <div class="progressBar">
1215 <div id="element5" class="innerBar"> </div>
1217 <span id="box5">0</span> '.ini_get(
"memory_limit").
'<br />
1219 <span class="barheading">Memory usage (peak)</span> <br/>
1220 <div class="progressBar">
1221 <div id="element6" class="innerBar"> </div>
1223 <span id="box6">0</span> '.ini_get(
"memory_limit").
'<br />
1229 <span id="box3"></span>
1275 if(array_key_exists(
'output', $options) && $options[
'output'])
1277 $res = array_keys(self::outputs(), $options[
'output']);
1279 if($res && count($res))
1280 $this->_output = $res[0];
1282 $this->_output =
'I';
1284 else $this->_output =
'I';
1286 $this->_debug = array_key_exists(
'debug', $options) && $options[
'debug'] ? $options[
'debug'] :
false;
1296 return array(
'F'=>
'file',
'I'=>
'inline',
'D'=>
'download',
'S'=>
'string');
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);
1315 if(!is_null($memory_limit))
1317 ini_set(
'memory_limit', $max_execution_time);
1320 if(!is_null($max_execution_time))
1322 ini_set(
'max_execution_time', $max_execution_time);
1353 $memory_usage = \Gino\gOpt(
'memory_usage', $options,
false);
1354 $memory_peak_usage = \Gino\gOpt(
'memory_peak_usage', $options,
false);
1358 echo \Gino\convertSize(memory_get_usage(
true)).
"<br />";
1361 if($memory_peak_usage)
1363 echo \Gino\convertSize(memory_get_peak_usage(
true)).
"<br />";
1379 $dirname = dirname($filename);
1380 if(!is_dir($dirname))
1382 $filename = basename($filename);
1385 else $filename =
'';
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'] :
'';
1417 if(is_array($css_file) && count($css_file))
1419 foreach($css_file AS $item)
1421 $html .=
"<link href=\"$item\" type=\"text/css\" rel=\"stylesheet\" />";
1426 $html .=
"<link href=\"$css_file\" type=\"text/css\" rel=\"stylesheet\" />";
1430 $html .=
"<style>".$css_style.
"</style>";
1433 $html .=
"<body>\n";
1435 if(is_bool($footer) && $footer===
false)
1439 elseif(is_string($footer) && $footer)
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);
1452 <htmlpageheader name=\"myheader\">
1456 <htmlpagefooter name=\"myfooter\">
1460 <sethtmlpageheader name=\"myheader\" value=\"on\" show-this-page=\"1\" />
1461 <sethtmlpagefooter name=\"myfooter\" value=\"on\" />
1475 <div style=\"border-top: 1px solid #000000; font-size: 6pt; text-align: center; padding-top: 3mm; \">
1476 "._(
"Pagina").
" {PAGENO} "._(
"di").
" {nb}
1488 $html =
"</body>\n";
1489 $html .=
"</html>\n";
1503 $html = utf8_encode($html);
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);
1535 $buffer = $this->
htmlStart(array(
'header'=>$header,
'footer'=>$footer,
'css_file'=>$css_file));
1540 if($this->_debug && $debug_exit) exit();
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"));
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';
1641 $protection = \Gino\gOpt(
'protection', $options, null);
1642 $user_password = \Gino\gOpt(
'user_password', $options,
'');
1643 $owner_password = \Gino\gOpt(
'owner_password', $options,
'');
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);
1654 if($landscape) $format .=
'-L';
1656 if($format ==
'A4' || $format ==
'A3')
1661 $bottom_margin = 25;
1662 $header_margin = 10;
1663 $footer_margin = 10;
1670 $bottom_margin = 16;
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'];
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);
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');
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)
1723 $mpdf->StartProgressBarOutput($progress_bar);
1727 $content = \Gino\gOpt(
'content', $options, null);
1728 $object = \Gino\gOpt(
'object', $options, null);
1729 $img_dir = \Gino\gOpt(
'img_dir', $options, null);
1731 if(!$content && is_object($object)) $content = $object->content($options);
1733 if(is_array($content))
1739 if(is_object($object))
1741 $options[
'header'] = $object->header(array(
'img_dir'=>$img_dir));
1742 $options[
'footer'] = $object->footer(array(
'img_dir'=>$img_dir));
1745 $html = $this->
definePage($content, $options);
1749 if(is_string($html))
1751 $mpdf->WriteHTML($html);
1753 elseif(is_array($html) AND
sizeof($html) > 0)
1756 for($i=0, $end=
sizeof($pages); $i<$end; $i++)
1760 $mpdf->WriteHTML($pages[$i]);
1764 if(is_array($pages[$i]))
1766 $orientation_page = array_key_exists(
'orientation', $pages[$i]) ? $pages[$i][
'orientation'] :
'P';
1767 $html = array_key_exists(
'html', $pages[$i]) ? $pages[$i][
'html'] :
'';
1771 $orientation_page = $landscape ?
'L' :
'P';
1774 $mpdf->AddPageByArray(array(
'orientation'=>$orientation_page));
1775 $mpdf->WriteHTML($html);
1782 if($this->_output ==
'S')
1784 return $mpdf->Output($filename, $this->_output);
1786 elseif($this->_output ==
'I' || $this->_output ==
'D')
1788 $mpdf->Output($filename, $this->_output);
1793 $mpdf->Output($filename, $this->_output);
1815 public function sendToEmail($mpdf_output, $filename, $options=array()){
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'] :
'';
1824 $content = chunk_split(base64_encode($mpdf_output));
1828 $uid = md5(uniqid(time()));
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);
1858 $string = bin2hex($mpdf_output);
1859 $string =
"0x".$string;
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');
1890 return "<pagebreak />";
1902 $text =
"<div class=\"longtext\">$text</div>";
1922 public function text($text, $options=array()){
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');
1930 $class =
"class=\"$class\"";
1932 $style =
"style=\"$style\"";
1934 if($type ==
'textarea')
1935 $method =
'\Gino\pdfChars_Textarea';
1936 elseif($type ==
'editor')
1937 $method =
'\Gino\pdfTextChars';
1939 $method =
'\Gino\pdfChars';
1941 $text = $method($text);
1943 if($class OR $style OR $other)
1945 $text =
"<span $class$style$other>$text</span>";