Skip to content
Snippets Groups Projects
mpdf.php 1.27 MiB
Newer Older
33001 33002 33003 33004 33005 33006 33007 33008 33009 33010 33011 33012 33013 33014 33015 33016 33017 33018 33019 33020 33021 33022 33023 33024 33025 33026 33027 33028 33029 33030 33031 33032 33033 33034 33035 33036 33037 33038 33039 33040 33041 33042 33043 33044 33045 33046 33047 33048 33049 33050 33051 33052 33053 33054 33055 33056 33057 33058 33059 33060 33061 33062 33063 33064 33065 33066 33067 33068 33069 33070 33071 33072 33073 33074 33075 33076 33077 33078 33079 33080 33081 33082 33083 33084 33085 33086 33087 33088 33089 33090 33091 33092 33093 33094 33095 33096 33097 33098 33099 33100 33101 33102 33103 33104 33105 33106 33107 33108 33109 33110 33111 33112 33113 33114 33115 33116 33117 33118 33119 33120 33121 33122 33123 33124 33125 33126 33127 33128 33129 33130 33131 33132 33133 33134 33135 33136 33137 33138 33139 33140 33141 33142 33143 33144 33145 33146 33147 33148 33149 33150 33151 33152 33153 33154 33155 33156 33157 33158 33159 33160 33161 33162 33163 33164 33165 33166 33167 33168 33169 33170 33171 33172 33173 33174 33175 33176 33177 33178 33179 33180 33181 33182 33183 33184 33185 33186 33187 33188 33189 33190 33191 33192 33193 33194 33195 33196 33197 33198 33199 33200 33201 33202 33203 33204 33205 33206 33207 33208 33209 33210 33211 33212 33213 33214 33215 33216 33217 33218 33219 33220 33221 33222 33223 33224 33225 33226 33227 33228 33229 33230 33231 33232 33233 33234 33235 33236 33237 33238 33239 33240 33241 33242 33243 33244 33245 33246 33247 33248 33249 33250 33251 33252 33253 33254
	}
	$newxref .= "trailer";
	$pdf = preg_replace("/xref\n0 \d+\n.*?\ntrailer/s",$newxref,$pdf);

	// Update startxref in PDF
	preg_match("/startxref\n(\d+)\n%%EOF/s", $pdf, $m);
	$startxref = $m[1];
	$startxref += array_sum($changes);
	$pdf = preg_replace("/startxref\n(\d+)\n%%EOF/s","startxref\n".$startxref."\n%%EOF",$pdf);

	// OUTPUT
	switch($dest) {
		case 'I':
			//Send to standard output
			if(isset($_SERVER['SERVER_NAME']))
			{
				//We send to a browser
				Header('Content-Type: application/pdf');
				Header('Content-Length: '.strlen($pdf));
				Header('Content-disposition: inline; filename='.$file_out);
			}
			echo $pdf;
			break;
		case 'F':
			//Save to local file
			if (!$file_out) { $file_out = 'mpdf.pdf'; }
			$f=fopen($file_out,'wb');
			if(!$f) die('Unable to create output file: '.$file_out);
			fwrite($f,$pdf,strlen($pdf));
			fclose($f);
			break;
		case 'S':
			//Return as a string
			return $pdf;
		case 'D':
		default:
			//Download file
			if(isset($_SERVER['HTTP_USER_AGENT']) and strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
				Header('Content-Type: application/force-download');
			else
				Header('Content-Type: application/octet-stream');
			Header('Content-Length: '.strlen($pdf));
			Header('Content-disposition: attachment; filename='.$file_out);
 			echo $pdf;
			break;
	}
}


function GetTemplateSize($tplidx, $_w=0, $_h=0) {
	if (!$this->tpls[$tplidx])
		return false;
	$w = $this->tpls[$tplidx]['box']['w'];
	$h = $this->tpls[$tplidx]['box']['h'];
	if ($_w == 0 and $_h == 0) {
		$_w = $w;
		$_h = $h;
	}
	if($_w==0)
		$_w=$_h*$w/$h;
	if($_h==0)
		$_h=$_w*$h/$w;
	return array("w" => $_w, "h" => $_h);
}

// Thumbnails
function Thumbnail($file, $npr=3, $spacing=10) {	//$npr = number per row
	$w = (($this->pgwidth + $spacing)/$npr) - $spacing;
	$oldlinewidth = $this->LineWidth;
	$this->SetLineWidth(0.02);
	$this->SetDColor($this->ConvertColor(0));
	$h = 0;
	$maxh = 0;
	$x = $_x = $this->lMargin;
	$_y = $this->tMargin;
	if ($this->y==0) { $y = $_y; } else { $y = $this->y; }
	$pagecount = $this->SetSourceFile($file);
	for ($n = 1; $n <= $pagecount; $n++) {
		$tplidx = $this->ImportPage($n);
		$size = $this->useTemplate($tplidx, $x, $y, $w);
		$this->Rect($x, $y, $size['w'], $size['h']);
		$h = max($h, $size['h']);
		$maxh = max($h, $maxh);
		if ($n % $npr == 0) {
		   if (($y + $h + $spacing + $maxh)>$this->PageBreakTrigger && $n != $pagecount) {
			$this->AddPage();
			$x = $_x;
			$y = $_y;
		   }
		   else {
			$y += $h+$spacing ;
			$x = $_x;
			$h = 0;
		   }
		}
		else {
			$x += $w+$spacing ;
		}
	}
	$this->SetLineWidth($oldlinewidth);
}

function SetSourceFile($filename) {
	$this->current_filename = $filename;
	$fn =& $this->current_filename;
	if (!isset($this->parsers[$fn]))
		// $this->parsers[$fn] =& new fpdi_pdf_parser($fn,$this);
		$this->parsers[$fn] = new fpdi_pdf_parser($fn,$this);
	if (!$this->parsers[$fn]->success) {
		$this->Error($this->parsers[$fn]->errormsg);	// Delete this line to return false on fail
		return false;
	}
	$this->current_parser =& $this->parsers[$fn];
	return $this->parsers[$fn]->getPageCount();
}
function ImportPage($pageno=1, $crop_x=null, $crop_y=null, $crop_w=0, $crop_h=0, $boxName='/CropBox') {
	$fn =& $this->current_filename;
	$parser =& $this->parsers[$fn];
	$parser->setPageno($pageno);

	$this->tpl++;
	$this->tpls[$this->tpl] = array();
	$tpl =& $this->tpls[$this->tpl];
	$tpl['parser'] =& $parser;
	$tpl['resources'] = $parser->getPageResources();
	$tpl['buffer'] = $parser->getContent();
	if (!in_array($boxName, $parser->availableBoxes))
		return $this->Error(sprintf("Unknown box: %s", $boxName));
	$pageboxes = $parser->getPageBoxes($pageno);
	/**
	 * MediaBox
	 * CropBox: Default -> MediaBox
	 * BleedBox: Default -> CropBox
	 * TrimBox: Default -> CropBox
	 * ArtBox: Default -> CropBox
	 */
	if (!isset($pageboxes[$boxName]) && ($boxName == "/BleedBox" || $boxName == "/TrimBox" || $boxName == "/ArtBox"))
		$boxName = "/CropBox";
	if (!isset($pageboxes[$boxName]) && $boxName == "/CropBox")
		$boxName = "/MediaBox";
	if (!isset($pageboxes[$boxName]))
		return false;
	$box = $pageboxes[$boxName];

	$tpl['box'] = $box;
	// To build an array that can be used by useTemplate()
	$this->tpls[$this->tpl] = array_merge($this->tpls[$this->tpl],$box);
	// An imported page will start at 0,0 everytime. Translation will be set in _putformxobjects()
	$tpl['x'] = 0;
	$tpl['y'] = 0;
	$tpl['w'] = $tpl['box']['w'] ;
	$tpl['h'] = $tpl['box']['h'] ;
	if ($crop_w) { $tpl['box']['w'] = $crop_w; }
	if ($crop_h) { $tpl['box']['h'] = $crop_h; }
	if (isset($crop_x)) { $tpl['box']['x'] = $crop_x; }
	if (isset($crop_y)) {$tpl['box']['y'] = $tpl['h'] - $crop_y  - $crop_h ; }

	$page =& $parser->pages[$parser->pageno];
	// fix for rotated pages
	$rotation = $parser->getPageRotation($pageno);

	if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0 && $tpl['box']['w'] == $tpl['w']) {
		$steps = $angle / 90;

		$_w = $tpl['w'];
		$_h = $tpl['h'];
		$tpl['w'] = $steps % 2 == 0 ? $_w : $_h;
		$tpl['h'] = $steps % 2 == 0 ? $_h : $_w;
		if ($steps % 2 != 0) {
			$x = $y = ($steps == 1 || $steps == -3) ? $tpl['h'] : $tpl['w'];
		} else {
			$x = $tpl['w'];
			$y = $tpl['h'];
		}
		$cx=($x/2+$tpl['box']['x'])*_MPDFK;
		$cy=($y/2+$tpl['box']['y'])*_MPDFK;
		$angle*=-1;
		$angle*=M_PI/180;
		$c=cos($angle);
		$s=sin($angle);
		$tpl['box']['w'] = $tpl['w'] ;
		$tpl['box']['h'] = $tpl['h'] ;
		$tpl['buffer'] = sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm %s Q',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy, $tpl['buffer']);
	}
	return $this->tpl;
}
function UseTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0) {
	if (!isset($this->tpls[$tplidx]))
		$this->Error("Template does not exist!");
	if($this->state==0) { $this->AddPage(); }
	$out = 'q 0 J 1 w 0 j 0 G'."\n"; // reset standard values
	$x = $this->tpls[$tplidx]['x'];
	$y = $this->tpls[$tplidx]['y'];
	$w = $this->tpls[$tplidx]['w'];
	$h = $this->tpls[$tplidx]['h'];
	if ($_x == null) { $_x = $x; }
	if ($_y == null) { $_y = $y; }
	if ($_x === -1) { $_x = $this->x; }
	if ($_y === -1) { $_y = $this->y; }

	$wh = $this->getTemplateSize($tplidx,$_w,$_h);
	$_w = $wh['w'];
	$_h = $wh['h'];
	$out .= sprintf("q %.4F 0 0 %.4F %.2F %.2F cm", ($_w/$this->tpls[$tplidx]['box']['w']), ($_h/$this->tpls[$tplidx]['box']['h']), $_x*_MPDFK, ($this->h-($_y+$_h))*_MPDFK)."\n";
	$out .= $this->tplprefix.$tplidx." Do Q\n";

	$s = array("w" => $_w, "h" => $_h);
	$out .= "Q\n";
	$this->pages[$this->page] = $out . $this->pages[$this->page];
	return $s;
}
function SetPageTemplate($tplidx='') {
	if (!isset($this->tpls[$tplidx])) {
		$this->pageTemplate = '';
		return false;
	}
	$this->pageTemplate = $tplidx;
}
function SetDocTemplate($file='', $continue=0) {
	$this->docTemplate = $file;
	$this->docTemplateContinue = $continue;
}
/*-- END IMPORTS --*/


/* ---------------------------------------------- */
/* ---------------------------------------------- */
/* ---------------------------------------------- */
/* ---------------------------------------------- */
/* ---------------------------------------------- */

// JAVASCRIPT
function _set_object_javascript ($string) {
	$this->_newobj();
	$this->_out('<<');
	$this->_out('/S /JavaScript ');
	$this->_out('/JS '.$this->_textstring($string));
	$this->_out('>>');
	$this->_out('endobj');
}

function SetJS($script) {
	$this->js = $script;
}




}//end of Class




?>