00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009 $thirdparty_dir = dirname(__FILE__) . '/../../3rdparty/fpdf/';
00010 if (!defined('FPDF_FONTPATH')) {
00011 define('FPDF_FONTPATH', $thirdparty_dir . 'font/');
00012 }
00013 require_once $thirdparty_dir . 'fpdf.php';
00014 require_once $thirdparty_dir . 'fpdi.php';
00015
00016 class PDFMaker extends FPDI
00017 {
00018 private $text;
00019 private $template;
00020 private $template_pagecount = 0;
00021 private $filename;
00022
00023 private $style = array();
00024 private $cell = false;
00025 private $skipFirstLF = false;
00026
00027 public function __construct($text, $filename, $template = "") {
00028 $this->text = $text;
00029 $this->filename = $filename;
00030 $this->template = $template;
00031 if (!empty($this->template)) {
00032 $this->template_pagecount = $this->setSourceFile($this->template);
00033 }
00034
00035 FPDI::FPDI("P", "mm", "A4");
00036 }
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 function create($top = 10, $left = 10, $bottom = 20) {
00048 $this->AddPage();
00049 $this->SetMargins($left, $top, $left);
00050 $this->SetAutoPageBreak(true, $bottom);
00051 $this->SetFont("helvetica", "", 9);
00052 $this->SetXY($left, $top);
00053 $this->writeFormatted(5, $this->text);
00054 $this->Output($this->filename, "F");
00055 }
00056
00057 function Footer() {
00058 $page_no = $this->PageNo();
00059 $tpl_page = 0;
00060 if ($page_no <= $this->template_pagecount) {
00061 $tpl_page = $page_no;
00062 }
00063 else if ($this->template_pagecount > 0) {
00064 $tpl_page = $this->template_pagecount;
00065 }
00066 if ($tpl_page) {
00067 $tplidx = $this->ImportPage($tpl_page);
00068 $this->useTemplate($tplidx);
00069 }
00070 }
00071
00072 function writeFormatted($lineHeight, $text) {
00073 $arrText = preg_split('/<(.*)>/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
00074 foreach($arrText as $index => $content) {
00075 if($index % 2 == 0) {
00076 if ($this->cell !== false) {
00077 $arrCell = array(
00078 "w" => 0,
00079 "h" => 5,
00080 "border" => 0,
00081 "align" => "L",
00082 "fill" => 0
00083 );
00084
00085 Arr::clean($arrCell, $this->cell);
00086
00087
00088 $this->Cell(
00089 $arrCell["w"],
00090 $arrCell["h"],
00091 $content,
00092 $arrCell["border"],
00093 0,
00094 $arrCell["align"],
00095 $arrCell["fill"]
00096 );
00097 }
00098 else {
00099 if (false && $this->skipFirstLF === true && strlen($content) > 0) {
00100 if (strpos($content, "\r\n") === 0) {
00101 $content = substr($content, 2);
00102 }
00103 else if(strpos($content, "\n") === 0) {
00104 $content = substr($content, 1);
00105 }
00106 $this->skipFirstLF = false;
00107 }
00108
00109 $this->Write(5, $content);
00110 }
00111 }
00112 else {
00113
00114 if($content{0} == '/') {
00115 $this->closeTag(strtoupper(substr($content,1)));
00116 }
00117 else {
00118
00119 $arrAttrs = explode(' ', $content);
00120 $tag = strtoupper(array_shift($arrAttrs));
00121 $arrAttrValues = array();
00122 foreach($arrAttrs as $attrExpression) {
00123 if ( ereg('^([^=]*)=["\']?([^"\']*)["\']?$', $attrExpression, $temp)) {
00124 $arrAttrValues[strtoupper($temp[1])] = $temp[2];
00125 }
00126 }
00127 $this->openTag($tag, $arrAttrValues);
00128 }
00129 }
00130 }
00131 }
00132
00133 function openTag($tag, &$arrAttributes) {
00134 switch ($tag) {
00135 case "B":
00136 case "I":
00137 case "U":
00138 $this->setStyle($tag, 1);
00139 break;
00140 case "CELL":
00141 $this->cell = array();
00142 foreach($arrAttributes as $name => $value) {
00143 $this->cell[strtolower($name)] = strtoupper($value);
00144 }
00145 break;
00146 }
00147 }
00148
00149 function closeTag($tag) {
00150 switch ($tag) {
00151 case "B":
00152 case "I":
00153 case "U":
00154 $this->setStyle($tag, 0);
00155 break;
00156 case "CELL":
00157 $this->cell = false;
00158 $this->skipFirstLF = true;
00159 break;
00160 }
00161 }
00162
00163 function setStyle($tag, $enable) {
00164
00165 if (array_key_exists($tag, $this->style) == false) {
00166 $this->style[$tag] = 0;
00167 }
00168
00169 $this->style[$tag] += ($enable ? 1 : -1);
00170 $style='';
00171 foreach($this->style as $s => $count) {
00172 if($count > 0) {
00173 $style.=$s;
00174 }
00175 }
00176 $this->SetFont('',$style);
00177 }
00178 }