update fpdi and fpdf_tpl to version 1.3(released at 2009-03-06).`

This commit is contained in:
wlx 2009-07-17 08:38:31 +00:00
parent 4c11cb835f
commit 251a5e965a
8 changed files with 391 additions and 314 deletions

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2009 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,14 +17,14 @@
// limitations under the License.
//
if (!defined("ORD_z"))
define("ORD_z",ord('z'));
if (!defined("ORD_exclmark"))
define("ORD_exclmark", ord('!'));
if (!defined("ORD_u"))
define("ORD_u", ord("u"));
if (!defined("ORD_tilde"))
define("ORD_tilde", ord('~'));
if (!defined('ORD_z'))
define('ORD_z',ord('z'));
if (!defined('ORD_exclmark'))
define('ORD_exclmark', ord('!'));
if (!defined('ORD_u'))
define('ORD_u', ord('u'));
if (!defined('ORD_tilde'))
define('ORD_tilde', ord('~'));
class ASCII85Decode {
@ -34,7 +34,7 @@ class ASCII85Decode {
function decode($in) {
$out = "";
$out = '';
$state = 0;
$chn = null;
@ -46,7 +46,7 @@ class ASCII85Decode {
if ($ch == ORD_tilde) {
break;
}
if (preg_match("/^\s$/",chr($ch))) {
if (preg_match('/^\s$/',chr($ch))) {
continue;
}
if ($ch == ORD_z && $state == 0) {
@ -54,7 +54,7 @@ class ASCII85Decode {
continue;
}
if ($ch < ORD_exclmark || $ch > ORD_u) {
$this->fpdi->error("Illegal character in ASCII85Decode.");
$this->fpdi->error('Illegal character in ASCII85Decode.');
}
$chn[$state++] = $ch - ORD_exclmark;
@ -73,7 +73,7 @@ class ASCII85Decode {
$r = 0;
if ($state == 1)
$this->fpdi->error("Illegal length in ASCII85Decode.");
$this->fpdi->error('Illegal length in ASCII85Decode.');
if ($state == 2) {
$r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1]+1) * 85 * 85 * 85;
$out .= chr($r >> 24);

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2009 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -21,6 +21,7 @@ class LZWDecode {
var $sTable = array();
var $data = null;
var $dataLength = 0;
var $tIdx;
var $bitsToGet = 9;
var $bytePointer;
@ -41,12 +42,13 @@ class LZWDecode {
function decode($data) {
if($data[0] == 0x00 && $data[1] == 0x01) {
$this->fpdi->error("LZW flavour not supported.");
$this->fpdi->error('LZW flavour not supported.');
}
$this->initsTable();
$this->data = $data;
$this->dataLength = strlen($data);
// Initialize pointers
$this->bytePointer = 0;
@ -57,8 +59,8 @@ class LZWDecode {
$oldCode = 0;
$string = "";
$uncompData = "";
$string = '';
$uncompData = '';
while (($code = $this->getNextCode()) != 257) {
if ($code == 256) {
@ -111,7 +113,7 @@ class LZWDecode {
/**
* Add a new string to the string table.
*/
function addStringToTable ($oldString, $newString="") {
function addStringToTable ($oldString, $newString='') {
$string = $oldString.$newString;
// Add this new String to the table
@ -128,8 +130,9 @@ class LZWDecode {
// Returns the next 9, 10, 11 or 12 bits
function getNextCode() {
if ($this->bytePointer == strlen($this->data))
if ($this->bytePointer == $this->dataLength) {
return 257;
}
$this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
$this->nextBits += 8;

View File

@ -1,8 +1,8 @@
<?php
//
// FPDF_TPL - Version 1.1.2
// FPDF_TPL - Version 1.1.3
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2009 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,9 +17,7 @@
// limitations under the License.
//
//class FPDF_TPL extends FPDF {
// To support chinese unicode font
class FPDF_TPL extends PDF_Unicode {
class FPDF_TPL extends FPDF {
/**
* Array of Tpl-Data
* @var array
@ -50,6 +48,13 @@ class FPDF_TPL extends PDF_Unicode {
*/
var $_res = array();
/**
* Last used Template data
*
* @var array
*/
var $lastUsedTemplateData = array();
/**
* Start a Template
*
@ -168,21 +173,37 @@ class FPDF_TPL extends PDF_Unicode {
}
$tpl =& $this->tpls[$tplidx];
$x = $tpl['x'];
$y = $tpl['y'];
$w = $tpl['w'];
$h = $tpl['h'];
if ($_x == null)
$_x = $x;
$_x = 0;
if ($_y == null)
$_y = $y;
$_y = 0;
$_x += $tpl['x'];
$_y += $tpl['y'];
$wh = $this->getTemplateSize($tplidx, $_w, $_h);
$_w = $wh['w'];
$_h = $wh['h'];
$this->_out(sprintf("q %.4f 0 0 %.4f %.2f %.2f cm", ($_w/$w), ($_h/$h), $_x*$this->k, ($this->h-($_y+$_h))*$this->k)); // Translate
$this->_out($this->tplprefix.$tplidx." Do Q");
$tData = array(
'x' => $this->x,
'y' => $this->y,
'w' => $_w,
'h' => $_h,
'scaleX' => ($_w/$w),
'scaleY' => ($_h/$h),
'tx' => $_x,
'ty' => ($this->h-$_y-$_h),
'lty' => ($this->h-$_y-$_h) - ($this->h-$h) * ($_h/$h)
);
$this->_out(sprintf("q %.4F 0 0 %.4F %.4F %.4F cm", $tData['scaleX'], $tData['scaleY'], $tData['tx']*$this->k, $tData['ty']*$this->k)); // Translate
$this->_out(sprintf('%s%d Do Q', $this->tplprefix, $tplidx));
$this->lastUsedTemplateData = $tData;
return array("w" => $_w, "h" => $_h);
}
@ -219,16 +240,19 @@ class FPDF_TPL extends PDF_Unicode {
}
/**
* See FPDF-Documentation ;-)
* See FPDF/TCPDF-Documentation ;-)
*/
function SetFont($family, $style='', $size=0) {
function SetFont($family, $style='', $size=0, $fontfile='') {
if (!is_subclass_of($this, 'TCPDF') && func_num_args() > 3) {
$this->Error('More than 3 arguments for the SetFont method are only available in TCPDF.');
}
/**
* force the resetting of font changes in a template
*/
if ($this->_intpl)
$this->FontFamily = '';
parent::SetFont($family, $style, $size);
parent::SetFont($family, $style, $size, $fontfile);
$fontkey = $this->FontFamily.$this->FontStyle;
@ -242,12 +266,12 @@ class FPDF_TPL extends PDF_Unicode {
/**
* See FPDF/TCPDF-Documentation ;-)
*/
function Image($file, $x, $y, $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300) {
function Image($file, $x, $y, $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0) {
if (!is_subclass_of($this, 'TCPDF') && func_num_args() > 7) {
$this->Error('More than 7 arguments for the Image method are only available in TCPDF.');
}
parent::Image($file, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi);
parent::Image($file, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border);
if ($this->_intpl) {
$this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file];
} else {
@ -269,10 +293,14 @@ class FPDF_TPL extends PDF_Unicode {
/**
* Preserve adding Links in Templates ...won't work
*/
function Link($x, $y, $w, $h, $link) {
function Link($x, $y, $w, $h, $link, $spaces=0) {
if (!is_subclass_of($this, 'TCPDF') && func_num_args() > 5) {
$this->Error('More than 7 arguments for the Image method are only available in TCPDF.');
}
if ($this->_intpl)
$this->Error('Using links in templates aren\'t possible!');
parent::Link($x, $y, $w, $h, $link);
parent::Link($x, $y, $w, $h, $link, $spaces);
}
function AddLink() {
@ -301,7 +329,23 @@ class FPDF_TPL extends PDF_Unicode {
$this->_out('<<'.$filter.'/Type /XObject');
$this->_out('/Subtype /Form');
$this->_out('/FormType 1');
$this->_out(sprintf('/BBox [%.2f %.2f %.2f %.2f]',$tpl['x']*$this->k, ($tpl['h']-$tpl['y'])*$this->k, $tpl['w']*$this->k, ($tpl['h']-$tpl['y']-$tpl['h'])*$this->k));
$this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
// llx
$tpl['x'],
// lly
-$tpl['y'],
// urx
($tpl['w']+$tpl['x'])*$this->k,
// ury
($tpl['h']-$tpl['y'])*$this->k
));
if ($tpl['x'] != 0 || $tpl['y'] != 0) {
$this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]',
-$tpl['x']*$this->k*2, $tpl['y']*$this->k*2
));
}
$this->_out('/Resources ');
$this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
@ -334,46 +378,12 @@ class FPDF_TPL extends PDF_Unicode {
}
/**
* Private Method
* Overwritten to add _putformxobjects() after _putimages()
*
*/
function _putresources() {
if (!is_subclass_of($this, 'TCPDF')) {
$this->_putfonts();
$this->_putimages();
function _putimages() {
parent::_putimages();
$this->_putformxobjects();
//Resource dictionary
$this->offsets[2]=strlen($this->buffer);
$this->_out('2 0 obj');
$this->_out('<<');
$this->_putresourcedict();
$this->_out('>>');
$this->_out('endobj');
} else {
$this->_putextgstates();
$this->_putocg();
$this->_putfonts();
$this->_putimages();
$this->_putshaders();
$this->_putformxobjects();
//Resource dictionary
$this->offsets[2]=strlen($this->buffer);
$this->_out('2 0 obj');
$this->_out('<<');
$this->_putresourcedict();
$this->_out('>>');
$this->_out('endobj');
$this->_putjavascript();
$this->_putbookmarks();
// encryption
if ($this->encrypted) {
$this->_newobj();
$this->enc_obj_id = $this->n;
$this->_out('<<');
$this->_putencryption();
$this->_out('>>');
$this->_out('endobj');
}
}
}
function _putxobjectdict() {
@ -381,7 +391,7 @@ class FPDF_TPL extends PDF_Unicode {
if (count($this->tpls)) {
foreach($this->tpls as $tplidx => $tpl) {
$this->_out($this->tplprefix.$tplidx.' '.$tpl['n'].' 0 R');
$this->_out(sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n']));
}
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2009 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,15 +17,15 @@
// limitations under the License.
//
define('FPDI_VERSION','1.2.1');
define('FPDI_VERSION','1.3');
// Check for TCPDF and remap TCPDF to FPDF
//if (class_exists('TCPDF')) {
// require_once('fpdi2tcpdf_bridge.php');
//}
if (class_exists('TCPDF')) {
require_once('fpdi2tcpdf_bridge.php');
}
require_once("fpdf_tpl.php");
require_once("fpdi_pdf_parser.php");
require_once('fpdf_tpl.php');
require_once('fpdi_pdf_parser.php');
class FPDI extends FPDF_TPL {
@ -99,7 +99,7 @@ class FPDI extends FPDF_TPL {
*/
function importPage($pageno, $boxName='/CropBox') {
if ($this->_intpl) {
return $this->error("Please import the desired pages before creating a new template.");
return $this->error('Please import the desired pages before creating a new template.');
}
$fn =& $this->current_filename;
@ -120,7 +120,7 @@ class FPDI extends FPDF_TPL {
$tpl['buffer'] = $parser->getContent();
if (!in_array($boxName, $parser->availableBoxes))
return $this->Error(sprintf("Unknown box: %s", $boxName));
return $this->Error(sprintf('Unknown box: %s', $boxName));
$pageboxes = $parser->getPageBoxes($pageno);
/**
@ -130,10 +130,10 @@ class FPDI extends FPDF_TPL {
* 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]) && ($boxName == '/BleedBox' || $boxName == '/TrimBox' || $boxName == '/ArtBox'))
$boxName = '/CropBox';
if (!isset($pageboxes[$boxName]) && $boxName == '/CropBox')
$boxName = '/MediaBox';
if (!isset($pageboxes[$boxName]))
return false;
@ -144,14 +144,16 @@ class FPDI extends FPDF_TPL {
// To build an array that can be used by PDF_TPL::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;
$page =& $parser->pages[$parser->pageno];
// fix for rotated pages
// handle rotated pages
$rotation = $parser->getPageRotation($pageno);
$tpl['_rotationAngle'] = 0;
if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0) {
$steps = $angle / 90;
@ -160,23 +162,7 @@ class FPDI extends FPDF_TPL {
$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'])*$this->k;
$cy=($y/2+$tpl['box']['y'])*$this->k;
$angle*=-1;
$angle*=M_PI/180;
$c=cos($angle);
$s=sin($angle);
$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']);
$tpl['_rotationAngle'] = $angle*-1;
}
$this->_importedPages[$pageKey] = $this->tpl;
@ -188,7 +174,21 @@ class FPDI extends FPDF_TPL {
return $this->lastUsedPageBox;
}
function useTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0) {
function useTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0, $adjustPageSize=false) {
if ($adjustPageSize == true && is_null($_x) && is_null($_y)) {
$size = $this->getTemplateSize($tplidx, $_w, $_h);
$format = array($size['w'], $size['h']);
if ($format[0]!=$this->CurPageFormat[0] || $format[1]!=$this->CurPageFormat[1]) {
$this->w=$format[0];
$this->h=$format[1];
$this->wPt=$this->w*$this->k;
$this->hPt=$this->h*$this->k;
$this->PageBreakTrigger=$this->h-$this->bMargin;
$this->CurPageFormat=$format;
$this->PageSizes[$this->page]=array($this->wPt, $this->hPt);
}
}
$this->_out('q 0 J 1 w 0 j 0 G 0 g'); // reset standard values
$s = parent::useTemplate($tplidx, $_x, $_y, $_w, $_h);
$this->_out('Q');
@ -203,7 +203,7 @@ class FPDI extends FPDF_TPL {
foreach($this->parsers AS $filename => $p) {
$this->current_parser =& $this->parsers[$filename];
if (isset($this->_obj_stack[$filename]) && is_array($this->_obj_stack[$filename])) {
while($n = key($this->_obj_stack[$filename])) {
while(($n = key($this->_obj_stack[$filename])) !== null) {
$nObj = $this->current_parser->pdf_resolve_object($this->current_parser->c,$this->_obj_stack[$filename][$n][1]);
$this->_newobj($this->_obj_stack[$filename][$n][0]);
@ -224,51 +224,6 @@ class FPDI extends FPDF_TPL {
}
}
/**
* Put resources
*/
function _putresources() {
if (!is_subclass_of($this, 'TCPDF')) {
$this->_putfonts();
$this->_putimages();
$this->_putformxobjects();
$this->_putimportedobjects();
//Resource dictionary
$this->offsets[2]=strlen($this->buffer);
$this->_out('2 0 obj');
$this->_out('<<');
$this->_putresourcedict();
$this->_out('>>');
$this->_out('endobj');
} else { // TCPDF - Part
$this->_putextgstates();
$this->_putocg();
$this->_putfonts();
$this->_putimages();
$this->_putshaders();
$this->_putformxobjects();
$this->_putimportedobjects();
//Resource dictionary
$this->offsets[2]=strlen($this->buffer);
$this->_out('2 0 obj');
$this->_out('<<');
$this->_putresourcedict();
$this->_out('>>');
$this->_out('endobj');
$this->_putjavascript();
$this->_putbookmarks();
// encryption
if ($this->encrypted) {
$this->_newobj();
$this->enc_obj_id = $this->n;
$this->_out('<<');
$this->_putencryption();
$this->_out('>>');
$this->_out('endobj');
}
}
}
/**
* Private Method that writes the form xobjects
@ -286,15 +241,55 @@ class FPDI extends FPDF_TPL {
$this->_out('/Subtype /Form');
$this->_out('/FormType 1');
$this->_out(sprintf('/BBox [%.2f %.2f %.2f %.2f]',
($tpl['x'] + (isset($tpl['box']['x'])?$tpl['box']['x']:0))*$this->k,
($tpl['h'] + (isset($tpl['box']['y'])?$tpl['box']['y']:0) - $tpl['y'])*$this->k,
($tpl['w'] + (isset($tpl['box']['x'])?$tpl['box']['x']:0))*$this->k,
($tpl['h'] + (isset($tpl['box']['y'])?$tpl['box']['y']:0) - $tpl['y']-$tpl['h'])*$this->k)
);
$this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
(isset($tpl['box']['llx']) ? $tpl['box']['llx'] : $tpl['x'])*$this->k,
(isset($tpl['box']['lly']) ? $tpl['box']['lly'] : -$tpl['y'])*$this->k,
(isset($tpl['box']['urx']) ? $tpl['box']['urx'] : $tpl['w'] + $tpl['x'])*$this->k,
(isset($tpl['box']['ury']) ? $tpl['box']['ury'] : $tpl['h']-$tpl['y'])*$this->k
));
if (isset($tpl['box']))
$this->_out(sprintf('/Matrix [1 0 0 1 %.5f %.5f]',-$tpl['box']['x']*$this->k, -$tpl['box']['y']*$this->k));
$c = 1;
$s = 0;
$tx = 0;
$ty = 0;
if (isset($tpl['box'])) {
$tx = -$tpl['box']['llx'];
$ty = -$tpl['box']['lly'];
if ($tpl['_rotationAngle'] <> 0) {
$angle = $tpl['_rotationAngle'] * M_PI/180;
$c=cos($angle);
$s=sin($angle);
switch($tpl['_rotationAngle']) {
case -90:
$tx = -$tpl['box']['lly'];
$ty = $tpl['box']['urx'];
break;
case -180:
$tx = $tpl['box']['urx'];
$ty = $tpl['box']['ury'];
break;
case -270:
$tx = $tpl['box']['ury'];
$ty = 0;
break;
}
}
} else if ($tpl['x'] != 0 || $tpl['y'] != 0) {
$tx = -$tpl['x']*2;
$ty = $tpl['y']*2;
}
$tx *= $this->k;
$ty *= $this->k;
if ($c != 1 || $s != 0 || $tx != 0 || $ty != 0) {
$this->_out(sprintf('/Matrix [%.5F %.5F %.5F %.5F %.5F %.5F]',
$c, $s, -$s, $c, $tx, $ty
));
}
$this->_out('/Resources ');
@ -333,6 +328,8 @@ class FPDI extends FPDF_TPL {
$this->_out('endobj');
$this->n = $nN; // TCPDF: reset to new "n"
}
$this->_putimportedobjects();
}
/**
@ -345,7 +342,7 @@ class FPDI extends FPDF_TPL {
//Begin a new object
if (!$onlynewobj) {
$this->offsets[$obj_id] = strlen($this->buffer);
$this->offsets[$obj_id] = is_subclass_of($this, 'TCPDF') ? $this->bufferlen : strlen($this->buffer);
$this->_out($obj_id.' 0 obj');
$this->_current_obj_id = $obj_id; // for later use with encryption
}
@ -365,12 +362,16 @@ class FPDI extends FPDF_TPL {
switch ($value[0]) {
case PDF_TYPE_NUMERIC :
case PDF_TYPE_TOKEN :
$this->_straightOut($value[1] . ' ');
break;
case PDF_TYPE_NUMERIC :
case PDF_TYPE_REAL :
// A numeric value or a token.
// Simply output them
$this->_out($value[1]." ", false);
if (is_float($value[1]) && $value[1] != 0) {
$this->_straightOut(rtrim(rtrim(sprintf('%F', $value[1]), '0'), '.') .' ');
} else {
$this->_straightOut($value[1] . ' ');
}
break;
case PDF_TYPE_ARRAY :
@ -378,27 +379,27 @@ class FPDI extends FPDF_TPL {
// An array. Output the proper
// structure and move on.
$this->_out("[",false);
$this->_straightOut('[');
for ($i = 0; $i < count($value[1]); $i++) {
$this->pdf_write_value($value[1][$i]);
}
$this->_out("]");
$this->_out(']');
break;
case PDF_TYPE_DICTIONARY :
// A dictionary.
$this->_out("<<",false);
$this->_straightOut('<<');
reset ($value[1]);
while (list($k, $v) = each($value[1])) {
$this->_out($k . " ",false);
$this->_straightOut($k . ' ');
$this->pdf_write_value($v);
}
$this->_out(">>");
$this->_straightOut('>>');
break;
case PDF_TYPE_OBJREF :
@ -406,6 +407,7 @@ class FPDI extends FPDF_TPL {
// An indirect object reference
// Fill the object stack if needed
$cpfn =& $this->current_parser->filename;
if (!isset($this->_don_obj_stack[$cpfn][$value[1]])) {
$this->_newobj(false,true);
$this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value);
@ -413,13 +415,13 @@ class FPDI extends FPDF_TPL {
}
$objid = $this->_don_obj_stack[$cpfn][$value[1]][0];
$this->_out("{$objid} 0 R");
$this->_out($objid.' 0 R');
break;
case PDF_TYPE_STRING :
// A string.
$this->_out('('.$value[1].')');
$this->_straightOut('('.$value[1].')');
break;
@ -429,23 +431,22 @@ class FPDI extends FPDF_TPL {
// stream dictionary, then the
// stream data itself.
$this->pdf_write_value($value[1]);
$this->_out("stream");
$this->_out('stream');
$this->_out($value[2][1]);
$this->_out("endstream");
$this->_out('endstream');
break;
case PDF_TYPE_HEX :
$this->_out("<".$value[1].">");
$this->_straightOut('<'.$value[1].'>');
break;
case PDF_TYPE_BOOLEAN :
$this->_out($value[1] ? 'true ' : 'false ', false);
$this->_straightOut($value[1] ? 'true ' : 'false ');
break;
case PDF_TYPE_NULL :
// The null object.
$this->_out("null");
$this->_straightOut('null ');
break;
}
}
@ -454,22 +455,25 @@ class FPDI extends FPDF_TPL {
/**
* Modified so not each call will add a newline to the output.
*/
function _out($s, $ln=true) {
//Add a line to the document
function _straightOut($s) {
if (!is_subclass_of($this, 'TCPDF')) {
if($this->state==2)
$this->pages[$this->page] .= $s;
else
$this->buffer .= $s;
} else {
if ($this->state == 2) {
if (!$this->_intpl) {
if (is_subclass_of($this, 'TCPDF') && isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
if (isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
// puts data before page footer
$page = substr($this->pages[$this->page], 0, -$this->footerlen[$this->page]);
$footer = substr($this->pages[$this->page], -$this->footerlen[$this->page]);
$this->pages[$this->page] = $page." ".$s."\n".$footer;
$page = substr($this->getPageBuffer($this->page), 0, -$this->footerlen[$this->page]);
$footer = substr($this->getPageBuffer($this->page), -$this->footerlen[$this->page]);
$this->setPageBuffer($this->page, $page.' '.$s."\n".$footer);
} else {
$this->pages[$this->page] .= $s.($ln == true ? "\n" : '');
$this->setPageBuffer($this->page, $s, true);
}
} else
$this->tpls[$this->tpl]['buffer'] .= $s.($ln == true ? "\n" : '');
} else {
$this->buffer.=$s.($ln == true ? "\n" : '');
$this->setBuffer($s);
}
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2009 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -28,21 +28,12 @@
*/
class FPDF extends TCPDF {
/**
* Missing in TCPDF
*
* @var string
*/
var $padding;
function __get($name) {
switch ($name) {
case 'PDFVersion':
return $this->PDFVersion;
case 'k':
return $this->k;
case 'lastUsedPageBox':
return $this->lastUsedPageBox;
default:
// Error handling
$this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
@ -100,16 +91,62 @@ class FPDF extends TCPDF {
* @return string
*/
function _unescape($s) {
return strtr($s, array(
'\\\\' => "\\",
'\)' => ')',
'\(' => '(',
'\\f' => chr(0x0C),
'\\b' => chr(0x08),
'\\t' => chr(0x09),
'\\r' => chr(0x0D),
'\\n' => chr(0x0A),
));
$out = '';
for ($count = 0, $n = strlen($s); $count < $n; $count++) {
if ($s[$count] != '\\' || $count == $n-1) {
$out .= $s[$count];
} else {
switch ($s[++$count]) {
case ')':
case '(':
case '\\':
$out .= $s[$count];
break;
case 'f':
$out .= chr(0x0C);
break;
case 'b':
$out .= chr(0x08);
break;
case 't':
$out .= chr(0x09);
break;
case 'r':
$out .= chr(0x0D);
break;
case 'n':
$out .= chr(0x0A);
break;
case "\r":
if ($count != $n-1 && $s[$count+1] == "\n")
$count++;
break;
case "\n":
break;
default:
// Octal-Values
if (ord($s[$count]) >= ord('0') &&
ord($s[$count]) <= ord('9')) {
$oct = ''. $s[$count];
if (ord($s[$count+1]) >= ord('0') &&
ord($s[$count+1]) <= ord('9')) {
$oct .= $s[++$count];
if (ord($s[$count+1]) >= ord('0') &&
ord($s[$count+1]) <= ord('9')) {
$oct .= $s[++$count];
}
}
$out .= chr(octdec($oct));
} else {
$out .= $s[$count];
}
}
}
}
return $out;
}
/**
@ -119,7 +156,7 @@ class FPDF extends TCPDF {
* @return string
*/
function hex2str($hex) {
return pack("H*", str_replace(array("\r", "\n", " "), "", $hex));
return pack('H*', str_replace(array("\r", "\n", ' '), '', $hex));
}
/**
@ -129,6 +166,6 @@ class FPDF extends TCPDF {
* @return string
*/
function str2hex($str) {
return current(unpack("H*", $str));
return current(unpack('H*', $str));
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2009 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,7 +17,7 @@
// limitations under the License.
//
require_once("pdf_parser.php");
require_once('pdf_parser.php');
class fpdi_pdf_parser extends pdf_parser {
@ -58,7 +58,7 @@ class fpdi_pdf_parser extends pdf_parser {
*
* @var array
*/
var $availableBoxes = array("/MediaBox","/CropBox","/BleedBox","/TrimBox","/ArtBox");
var $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');
/**
* Constructor
@ -68,7 +68,6 @@ class fpdi_pdf_parser extends pdf_parser {
*/
function fpdi_pdf_parser($filename, &$fpdi) {
$this->fpdi =& $fpdi;
$this->filename = $filename;
parent::pdf_parser($filename);
@ -110,7 +109,7 @@ class fpdi_pdf_parser extends pdf_parser {
$pageno = ((int) $pageno) - 1;
if ($pageno < 0 || $pageno >= $this->getPageCount()) {
$this->fpdi->error("Pagenumber is wrong!");
$this->fpdi->error('Pagenumber is wrong!');
}
$this->pageno = $pageno;
@ -163,7 +162,7 @@ class fpdi_pdf_parser extends pdf_parser {
* @return string
*/
function getContent() {
$buffer = "";
$buffer = '';
if (isset($this->pages[$this->pageno][1][1]['/Contents'])) {
$contents = $this->_getPageContent($this->pages[$this->pageno][1][1]['/Contents']);
@ -225,30 +224,30 @@ class fpdi_pdf_parser extends pdf_parser {
foreach ($filters AS $_filter) {
switch ($_filter[1]) {
case "/FlateDecode":
case '/FlateDecode':
if (function_exists('gzuncompress')) {
$stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
} else {
$this->fpdi->error(sprintf("To handle %s filter, please compile php with zlib support.",$_filter[1]));
$this->fpdi->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1]));
}
if ($stream === false) {
$this->fpdi->error("Error while decompressing stream.");
$this->fpdi->error('Error while decompressing stream.');
}
break;
case null:
$stream = $stream;
break;
default:
if (preg_match("/^\/[a-z85]*$/i", $_filter[1], $filterName) && @include_once('decoders'.$_filter[1].'.php')) {
if (preg_match('/^\/[a-z85]*$/i', $_filter[1], $filterName) && @include_once('decoders'.$_filter[1].'.php')) {
$filterName = substr($_filter[1],1);
if (class_exists($filterName)) {
$decoder =& new $filterName($this->fpdi);
$stream = $decoder->decode(trim($stream));
$stream = $decoder->decode($stream);
} else {
$this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1]));
$this->fpdi->error(sprintf('Unsupported Filter: %s',$_filter[1]));
}
} else {
$this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1]));
$this->fpdi->error(sprintf('Unsupported Filter: %s',$_filter[1]));
}
}
}
@ -278,10 +277,15 @@ class fpdi_pdf_parser extends pdf_parser {
if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
$b =& $box[1];
return array("x" => $b[0][1]/$this->fpdi->k,
"y" => $b[1][1]/$this->fpdi->k,
"w" => abs($b[0][1]-$b[2][1])/$this->fpdi->k,
"h" => abs($b[1][1]-$b[3][1])/$this->fpdi->k);
return array('x' => $b[0][1]/$this->fpdi->k,
'y' => $b[1][1]/$this->fpdi->k,
'w' => abs($b[0][1]-$b[2][1])/$this->fpdi->k,
'h' => abs($b[1][1]-$b[3][1])/$this->fpdi->k,
'llx' => $b[0][1]/$this->fpdi->k,
'lly' => $b[1][1]/$this->fpdi->k,
'urx' => $b[2][1]/$this->fpdi->k,
'ury' => $b[3][1]/$this->fpdi->k,
);
} else if (!isset ($page[1][1]['/Parent'])) {
return false;
} else {
@ -352,7 +356,7 @@ class fpdi_pdf_parser extends pdf_parser {
$kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
if (!is_array($kids))
$this->fpdi->Error("Cannot find /Kids in current /Page-Dictionary");
$this->fpdi->Error('Cannot find /Kids in current /Page-Dictionary');
foreach ($kids[1] as $v) {
$pg = $this->pdf_resolve_object ($c, $v);
if ($pg[1][1]['/Type'][1] === '/Pages') {

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2009 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -19,6 +19,13 @@
class pdf_context {
/**
* Modi
*
* @var integer 0 = file | 1 = string
*/
var $_mode = 0;
var $file;
var $buffer;
var $offset;
@ -28,8 +35,10 @@ class pdf_context {
// Constructor
function pdf_context($f) {
$this->file = $f;
function pdf_context(&$f) {
$this->file =& $f;
if (is_string($this->file))
$this->_mode = 1;
$this->reset();
}
@ -38,6 +47,7 @@ class pdf_context {
// and reset the buffered data
function reset($pos = null, $l = 100) {
if ($this->_mode == 0) {
if (!is_null ($pos)) {
fseek ($this->file, $pos);
}
@ -46,6 +56,10 @@ class pdf_context {
$this->length = strlen($this->buffer);
if ($this->length < $l)
$this->increase_length($l - $this->length);
} else {
$this->buffer = $this->file;
$this->length = strlen($this->buffer);
}
$this->offset = 0;
$this->stack = array();
}
@ -67,16 +81,17 @@ class pdf_context {
// Forcefully read more data into the buffer
function increase_length($l=100) {
if (feof($this->file)) {
if ($this->_mode == 0 && feof($this->file)) {
return false;
} else {
} else if ($this->_mode == 0) {
$totalLength = $this->length + $l;
do {
$this->buffer .= fread($this->file, $totalLength-$this->length);
} while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));
return true;
} else {
return false;
}
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2009 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -44,8 +44,7 @@ if (!defined ('PDF_TYPE_BOOLEAN'))
if (!defined ('PDF_TYPE_REAL'))
define ('PDF_TYPE_REAL', 12);
require_once("pdf_context.php");
require_once("wrapper_functions.php");
require_once('pdf_context.php');
class pdf_parser {
@ -93,14 +92,15 @@ class pdf_parser {
function pdf_parser($filename) {
$this->filename = $filename;
$this->f = @fopen($this->filename, "rb");
$this->f = @fopen($this->filename, 'rb');
if (!$this->f)
$this->error(sprintf("Cannot open %s !", $filename));
$this->error(sprintf('Cannot open %s !', $filename));
$this->getPDFVersion();
$this->c =& new pdf_context($this->f);
// Read xref-Data
$this->pdf_read_xref($this->xref, $this->pdf_find_xref());
@ -127,7 +127,7 @@ class pdf_parser {
* @param string $msg Error-Message
*/
function error($msg) {
die("<b>PDF-Parser Error:</b> ".$msg);
die('<b>PDF-Parser Error:</b> '.$msg);
}
/**
@ -135,7 +135,7 @@ class pdf_parser {
*/
function getEncryption() {
if (isset($this->xref['trailer'][1]['/Encrypt'])) {
$this->error("File is encrypted!");
$this->error('File is encrypted!');
}
}
@ -146,7 +146,7 @@ class pdf_parser {
*/
function pdf_find_root() {
if ($this->xref['trailer'][1]['/Root'][0] != PDF_TYPE_OBJREF) {
$this->error("Wrong Type of Root-Element! Must be an indirect reference");
$this->error('Wrong Type of Root-Element! Must be an indirect reference');
}
return $this->xref['trailer'][1]['/Root'];
}
@ -166,7 +166,7 @@ class pdf_parser {
*/
function getPDFVersion() {
fseek($this->f, 0);
preg_match("/\d\.\d/",fread($this->f,16),$m);
preg_match('/\d\.\d/',fread($this->f,16),$m);
if (isset($m[0]))
$this->pdfVersion = $m[0];
return $this->pdfVersion;
@ -188,7 +188,7 @@ class pdf_parser {
$data = substr($data, $pos);
if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) {
$this->error("Unable to find pointer to xref table");
$this->error('Unable to find pointer to xref table');
}
return (int) $matches[1];
@ -201,6 +201,7 @@ class pdf_parser {
* @param integer $offset of xref-table
*/
function pdf_read_xref(&$result, $offset) {
fseek($this->f, $o_pos = $offset-20); // set some bytes backwards to fetch errorious docs
$data = fread($this->f, 100);
@ -252,7 +253,7 @@ class pdf_parser {
for ($i = 0; $i < $linesCount; $i++) {
$line = trim($lines[$i]);
if ($line) {
$pieces = explode(" ", $line);
$pieces = explode(' ', $line);
$c = count($pieces);
switch($c) {
case 2:
@ -361,6 +362,13 @@ class pdf_parser {
if (($value = $this->pdf_read_value($c)) === false) {
return false;
}
// Catch missing value
if ($value[0] == PDF_TYPE_TOKEN && $value[1] == '>>') {
$result[$key] = array(PDF_TYPE_NULL);
break;
}
$result[$key] = $value;
}
@ -413,7 +421,7 @@ class pdf_parser {
return array (PDF_TYPE_STRING, $result);
case "stream":
case 'stream':
$o_pos = ftell($c->file)-strlen($c->buffer);
$o_offset = $c->offset;
@ -443,30 +451,6 @@ class pdf_parser {
return array(PDF_TYPE_STREAM, $v);
case '%':
// this is a comment - just jump over it
$pos = $c->offset;
while(1) {
// PHP 4.3.3 required
#$match = preg_match("/(\r\n|\r|\n)/", $c->buffer, $m, PREG_OFFSET_CAPTURE, $pos);
// alternative
$match = preg_match("/(\r\n|\r|\n)/", substr($c->buffer, $pos), $m);
if ($match === false) {
if (!$c->increase_length()) {
return false;
} else {
continue;
}
}
// PHP 4.3.3 required
#$c->offset = $m[0][1]+strlen($m[0][0]);
// alternative
$c->offset = strpos($c->buffer, $m[0], $pos)+strlen($m[0]);
return $this->pdf_read_value($c);
}
default :
if (is_numeric ($token)) {
// A numeric token. Make sure that
@ -502,6 +486,8 @@ class pdf_parser {
return array (PDF_TYPE_REAL, (float)$token);
} else if ($token == 'true' || $token == 'false') {
return array (PDF_TYPE_BOOLEAN, $token == 'true');
} else if ($token == 'null') {
return array (PDF_TYPE_NULL);
} else {
// Just a token. Return it.
@ -542,7 +528,7 @@ class pdf_parser {
$c->reset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]]);
$header = $this->pdf_read_value($c,null,true);
$header = $this->pdf_read_value($c);
if ($header[0] != PDF_TYPE_OBJDEC || $header[1] != $obj_spec[1] || $header[2] != $obj_spec[2]) {
$this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");
@ -615,7 +601,7 @@ class pdf_parser {
if (!$c->ensure_content()) {
return false;
}
$c->offset += _strspn($c->buffer, " \n\r\t", $c->offset);
$c->offset += strspn($c->buffer, " \n\r\t", $c->offset);
} while ($c->offset >= $c->length - 1);
// Get the first character in the stream
@ -651,6 +637,26 @@ class pdf_parser {
return $char;
}
case '%' :
// This is a comment - jump over it!
$pos = $c->offset;
while(1) {
$match = preg_match("/(\r\n|\r|\n)/", $c->buffer, $m, PREG_OFFSET_CAPTURE, $pos);
if ($match === 0) {
if (!$c->increase_length()) {
return false;
} else {
continue;
}
}
$c->offset = $m[0][1]+strlen($m[0][0]);
return $this->pdf_read_token($c);
}
default :
// This is "another" type of token (probably
@ -665,7 +671,7 @@ class pdf_parser {
// Determine the length of the token
$pos = _strcspn($c->buffer, " []<>()\r\n\t/", $c->offset);
$pos = strcspn($c->buffer, " %[]<>()\r\n\t/", $c->offset);
if ($c->offset + $pos <= $c->length - 1) {
break;
} else {
@ -685,6 +691,4 @@ class pdf_parser {
return $result;
}
}
}