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

View File

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

View File

@ -1,8 +1,8 @@
<?php <?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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -17,9 +17,7 @@
// limitations under the License. // limitations under the License.
// //
//class FPDF_TPL extends FPDF { class FPDF_TPL extends FPDF {
// To support chinese unicode font
class FPDF_TPL extends PDF_Unicode {
/** /**
* Array of Tpl-Data * Array of Tpl-Data
* @var array * @var array
@ -50,6 +48,13 @@ class FPDF_TPL extends PDF_Unicode {
*/ */
var $_res = array(); var $_res = array();
/**
* Last used Template data
*
* @var array
*/
var $lastUsedTemplateData = array();
/** /**
* Start a Template * Start a Template
* *
@ -168,22 +173,38 @@ class FPDF_TPL extends PDF_Unicode {
} }
$tpl =& $this->tpls[$tplidx]; $tpl =& $this->tpls[$tplidx];
$x = $tpl['x'];
$y = $tpl['y'];
$w = $tpl['w']; $w = $tpl['w'];
$h = $tpl['h']; $h = $tpl['h'];
if ($_x == null) if ($_x == null)
$_x = $x; $_x = 0;
if ($_y == null) if ($_y == null)
$_y = $y; $_y = 0;
$_x += $tpl['x'];
$_y += $tpl['y'];
$wh = $this->getTemplateSize($tplidx, $_w, $_h); $wh = $this->getTemplateSize($tplidx, $_w, $_h);
$_w = $wh['w']; $_w = $wh['w'];
$_h = $wh['h']; $_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 $tData = array(
$this->_out($this->tplprefix.$tplidx." Do Q"); '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); 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 * force the resetting of font changes in a template
*/ */
if ($this->_intpl) if ($this->_intpl)
$this->FontFamily = ''; $this->FontFamily = '';
parent::SetFont($family, $style, $size); parent::SetFont($family, $style, $size, $fontfile);
$fontkey = $this->FontFamily.$this->FontStyle; $fontkey = $this->FontFamily.$this->FontStyle;
@ -242,12 +266,12 @@ class FPDF_TPL extends PDF_Unicode {
/** /**
* See FPDF/TCPDF-Documentation ;-) * 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) { 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.'); $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) { if ($this->_intpl) {
$this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file]; $this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file];
} else { } else {
@ -269,10 +293,14 @@ class FPDF_TPL extends PDF_Unicode {
/** /**
* Preserve adding Links in Templates ...won't work * 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) if ($this->_intpl)
$this->Error('Using links in templates aren\'t possible!'); $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() { function AddLink() {
@ -301,7 +329,23 @@ class FPDF_TPL extends PDF_Unicode {
$this->_out('<<'.$filter.'/Type /XObject'); $this->_out('<<'.$filter.'/Type /XObject');
$this->_out('/Subtype /Form'); $this->_out('/Subtype /Form');
$this->_out('/FormType 1'); $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('/Resources ');
$this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); $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() { function _putimages() {
if (!is_subclass_of($this, 'TCPDF')) { parent::_putimages();
$this->_putfonts(); $this->_putformxobjects();
$this->_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() { function _putxobjectdict() {
@ -381,7 +391,7 @@ class FPDF_TPL extends PDF_Unicode {
if (count($this->tpls)) { if (count($this->tpls)) {
foreach($this->tpls as $tplidx => $tpl) { 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 <?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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -17,15 +17,15 @@
// limitations under the License. // limitations under the License.
// //
define('FPDI_VERSION','1.2.1'); define('FPDI_VERSION','1.3');
// Check for TCPDF and remap TCPDF to FPDF // Check for TCPDF and remap TCPDF to FPDF
//if (class_exists('TCPDF')) { if (class_exists('TCPDF')) {
// require_once('fpdi2tcpdf_bridge.php'); require_once('fpdi2tcpdf_bridge.php');
//} }
require_once("fpdf_tpl.php"); require_once('fpdf_tpl.php');
require_once("fpdi_pdf_parser.php"); require_once('fpdi_pdf_parser.php');
class FPDI extends FPDF_TPL { class FPDI extends FPDF_TPL {
@ -85,7 +85,7 @@ class FPDI extends FPDF_TPL {
$fn =& $this->current_filename; $fn =& $this->current_filename;
if (!isset($this->parsers[$fn])) if (!isset($this->parsers[$fn]))
$this->parsers[$fn] =& new fpdi_pdf_parser($fn,$this); $this->parsers[$fn] =& new fpdi_pdf_parser($fn, $this);
$this->current_parser =& $this->parsers[$fn]; $this->current_parser =& $this->parsers[$fn];
return $this->parsers[$fn]->getPageCount(); return $this->parsers[$fn]->getPageCount();
@ -99,7 +99,7 @@ class FPDI extends FPDF_TPL {
*/ */
function importPage($pageno, $boxName='/CropBox') { function importPage($pageno, $boxName='/CropBox') {
if ($this->_intpl) { 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; $fn =& $this->current_filename;
@ -120,7 +120,7 @@ class FPDI extends FPDF_TPL {
$tpl['buffer'] = $parser->getContent(); $tpl['buffer'] = $parser->getContent();
if (!in_array($boxName, $parser->availableBoxes)) 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); $pageboxes = $parser->getPageBoxes($pageno);
/** /**
@ -130,10 +130,10 @@ class FPDI extends FPDF_TPL {
* TrimBox: Default -> CropBox * TrimBox: Default -> CropBox
* ArtBox: Default -> CropBox * ArtBox: Default -> CropBox
*/ */
if (!isset($pageboxes[$boxName]) && ($boxName == "/BleedBox" || $boxName == "/TrimBox" || $boxName == "/ArtBox")) if (!isset($pageboxes[$boxName]) && ($boxName == '/BleedBox' || $boxName == '/TrimBox' || $boxName == '/ArtBox'))
$boxName = "/CropBox"; $boxName = '/CropBox';
if (!isset($pageboxes[$boxName]) && $boxName == "/CropBox") if (!isset($pageboxes[$boxName]) && $boxName == '/CropBox')
$boxName = "/MediaBox"; $boxName = '/MediaBox';
if (!isset($pageboxes[$boxName])) if (!isset($pageboxes[$boxName]))
return false; return false;
@ -144,14 +144,16 @@ class FPDI extends FPDF_TPL {
// To build an array that can be used by PDF_TPL::useTemplate() // To build an array that can be used by PDF_TPL::useTemplate()
$this->tpls[$this->tpl] = array_merge($this->tpls[$this->tpl],$box); $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() // An imported page will start at 0,0 everytime. Translation will be set in _putformxobjects()
$tpl['x'] = 0; $tpl['x'] = 0;
$tpl['y'] = 0; $tpl['y'] = 0;
$page =& $parser->pages[$parser->pageno]; $page =& $parser->pages[$parser->pageno];
// fix for rotated pages // handle rotated pages
$rotation = $parser->getPageRotation($pageno); $rotation = $parser->getPageRotation($pageno);
$tpl['_rotationAngle'] = 0;
if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0) { if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0) {
$steps = $angle / 90; $steps = $angle / 90;
@ -160,23 +162,7 @@ class FPDI extends FPDF_TPL {
$tpl['w'] = $steps % 2 == 0 ? $_w : $_h; $tpl['w'] = $steps % 2 == 0 ? $_w : $_h;
$tpl['h'] = $steps % 2 == 0 ? $_h : $_w; $tpl['h'] = $steps % 2 == 0 ? $_h : $_w;
if ($steps % 2 != 0) { $tpl['_rotationAngle'] = $angle*-1;
$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']);
} }
$this->_importedPages[$pageKey] = $this->tpl; $this->_importedPages[$pageKey] = $this->tpl;
@ -188,7 +174,21 @@ class FPDI extends FPDF_TPL {
return $this->lastUsedPageBox; 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 $this->_out('q 0 J 1 w 0 j 0 G 0 g'); // reset standard values
$s = parent::useTemplate($tplidx, $_x, $_y, $_w, $_h); $s = parent::useTemplate($tplidx, $_x, $_y, $_w, $_h);
$this->_out('Q'); $this->_out('Q');
@ -203,7 +203,7 @@ class FPDI extends FPDF_TPL {
foreach($this->parsers AS $filename => $p) { foreach($this->parsers AS $filename => $p) {
$this->current_parser =& $this->parsers[$filename]; $this->current_parser =& $this->parsers[$filename];
if (isset($this->_obj_stack[$filename]) && is_array($this->_obj_stack[$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]); $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]); $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 * Private Method that writes the form xobjects
@ -286,15 +241,55 @@ class FPDI extends FPDF_TPL {
$this->_out('/Subtype /Form'); $this->_out('/Subtype /Form');
$this->_out('/FormType 1'); $this->_out('/FormType 1');
$this->_out(sprintf('/BBox [%.2f %.2f %.2f %.2f]', $this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
($tpl['x'] + (isset($tpl['box']['x'])?$tpl['box']['x']:0))*$this->k, (isset($tpl['box']['llx']) ? $tpl['box']['llx'] : $tpl['x'])*$this->k,
($tpl['h'] + (isset($tpl['box']['y'])?$tpl['box']['y']:0) - $tpl['y'])*$this->k, (isset($tpl['box']['lly']) ? $tpl['box']['lly'] : -$tpl['y'])*$this->k,
($tpl['w'] + (isset($tpl['box']['x'])?$tpl['box']['x']:0))*$this->k, (isset($tpl['box']['urx']) ? $tpl['box']['urx'] : $tpl['w'] + $tpl['x'])*$this->k,
($tpl['h'] + (isset($tpl['box']['y'])?$tpl['box']['y']:0) - $tpl['y']-$tpl['h'])*$this->k) (isset($tpl['box']['ury']) ? $tpl['box']['ury'] : $tpl['h']-$tpl['y'])*$this->k
); ));
if (isset($tpl['box'])) $c = 1;
$this->_out(sprintf('/Matrix [1 0 0 1 %.5f %.5f]',-$tpl['box']['x']*$this->k, -$tpl['box']['y']*$this->k)); $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 '); $this->_out('/Resources ');
@ -333,6 +328,8 @@ class FPDI extends FPDF_TPL {
$this->_out('endobj'); $this->_out('endobj');
$this->n = $nN; // TCPDF: reset to new "n" $this->n = $nN; // TCPDF: reset to new "n"
} }
$this->_putimportedobjects();
} }
/** /**
@ -343,9 +340,9 @@ class FPDI extends FPDF_TPL {
$obj_id = ++$this->n; $obj_id = ++$this->n;
} }
//Begin a new object //Begin a new object
if (!$onlynewobj) { 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->_out($obj_id.' 0 obj');
$this->_current_obj_id = $obj_id; // for later use with encryption $this->_current_obj_id = $obj_id; // for later use with encryption
} }
@ -365,40 +362,44 @@ class FPDI extends FPDF_TPL {
switch ($value[0]) { switch ($value[0]) {
case PDF_TYPE_NUMERIC :
case PDF_TYPE_TOKEN : case PDF_TYPE_TOKEN :
case PDF_TYPE_REAL : $this->_straightOut($value[1] . ' ');
// A numeric value or a token.
// Simply output them
$this->_out($value[1]." ", false);
break; break;
case PDF_TYPE_NUMERIC :
case PDF_TYPE_REAL :
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 : case PDF_TYPE_ARRAY :
// An array. Output the proper // An array. Output the proper
// structure and move on. // structure and move on.
$this->_out("[",false); $this->_straightOut('[');
for ($i = 0; $i < count($value[1]); $i++) { for ($i = 0; $i < count($value[1]); $i++) {
$this->pdf_write_value($value[1][$i]); $this->pdf_write_value($value[1][$i]);
} }
$this->_out("]"); $this->_out(']');
break; break;
case PDF_TYPE_DICTIONARY : case PDF_TYPE_DICTIONARY :
// A dictionary. // A dictionary.
$this->_out("<<",false); $this->_straightOut('<<');
reset ($value[1]); reset ($value[1]);
while (list($k, $v) = each($value[1])) { while (list($k, $v) = each($value[1])) {
$this->_out($k . " ",false); $this->_straightOut($k . ' ');
$this->pdf_write_value($v); $this->pdf_write_value($v);
} }
$this->_out(">>"); $this->_straightOut('>>');
break; break;
case PDF_TYPE_OBJREF : case PDF_TYPE_OBJREF :
@ -406,20 +407,21 @@ class FPDI extends FPDF_TPL {
// An indirect object reference // An indirect object reference
// Fill the object stack if needed // Fill the object stack if needed
$cpfn =& $this->current_parser->filename; $cpfn =& $this->current_parser->filename;
if (!isset($this->_don_obj_stack[$cpfn][$value[1]])) { if (!isset($this->_don_obj_stack[$cpfn][$value[1]])) {
$this->_newobj(false,true); $this->_newobj(false,true);
$this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value); $this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value);
$this->_don_obj_stack[$cpfn][$value[1]] = array($this->n, $value); // Value is maybee obsolete!!! $this->_don_obj_stack[$cpfn][$value[1]] = array($this->n, $value); // Value is maybee obsolete!!!
} }
$objid = $this->_don_obj_stack[$cpfn][$value[1]][0]; $objid = $this->_don_obj_stack[$cpfn][$value[1]][0];
$this->_out("{$objid} 0 R"); $this->_out($objid.' 0 R');
break; break;
case PDF_TYPE_STRING : case PDF_TYPE_STRING :
// A string. // A string.
$this->_out('('.$value[1].')'); $this->_straightOut('('.$value[1].')');
break; break;
@ -429,23 +431,22 @@ class FPDI extends FPDF_TPL {
// stream dictionary, then the // stream dictionary, then the
// stream data itself. // stream data itself.
$this->pdf_write_value($value[1]); $this->pdf_write_value($value[1]);
$this->_out("stream"); $this->_out('stream');
$this->_out($value[2][1]); $this->_out($value[2][1]);
$this->_out("endstream"); $this->_out('endstream');
break; break;
case PDF_TYPE_HEX : case PDF_TYPE_HEX :
$this->_straightOut('<'.$value[1].'>');
$this->_out("<".$value[1].">");
break; break;
case PDF_TYPE_BOOLEAN : case PDF_TYPE_BOOLEAN :
$this->_out($value[1] ? 'true ' : 'false ', false); $this->_straightOut($value[1] ? 'true ' : 'false ');
break; break;
case PDF_TYPE_NULL : case PDF_TYPE_NULL :
// The null object. // The null object.
$this->_out("null"); $this->_straightOut('null ');
break; break;
} }
} }
@ -454,22 +455,25 @@ class FPDI extends FPDF_TPL {
/** /**
* Modified so not each call will add a newline to the output. * Modified so not each call will add a newline to the output.
*/ */
function _out($s, $ln=true) { function _straightOut($s) {
//Add a line to the document if (!is_subclass_of($this, 'TCPDF')) {
if ($this->state==2) { if($this->state==2)
if (!$this->_intpl) { $this->pages[$this->page] .= $s;
if (is_subclass_of($this, 'TCPDF') && isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) { else
// puts data before page footer $this->buffer .= $s;
$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;
} else {
$this->pages[$this->page] .= $s.($ln == true ? "\n" : '');
}
} else
$this->tpls[$this->tpl]['buffer'] .= $s.($ln == true ? "\n" : '');
} else { } else {
$this->buffer.=$s.($ln == true ? "\n" : ''); if ($this->state == 2) {
if (isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
// puts data before page 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->setPageBuffer($this->page, $s, true);
}
} else {
$this->setBuffer($s);
}
} }
} }

View File

@ -1,8 +1,8 @@
<?php <?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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -28,21 +28,12 @@
*/ */
class FPDF extends TCPDF { class FPDF extends TCPDF {
/**
* Missing in TCPDF
*
* @var string
*/
var $padding;
function __get($name) { function __get($name) {
switch ($name) { switch ($name) {
case 'PDFVersion': case 'PDFVersion':
return $this->PDFVersion; return $this->PDFVersion;
case 'k': case 'k':
return $this->k; return $this->k;
case 'lastUsedPageBox':
return $this->lastUsedPageBox;
default: default:
// Error handling // Error handling
$this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name); $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 * @return string
*/ */
function _unescape($s) { function _unescape($s) {
return strtr($s, array( $out = '';
'\\\\' => "\\", for ($count = 0, $n = strlen($s); $count < $n; $count++) {
'\)' => ')', if ($s[$count] != '\\' || $count == $n-1) {
'\(' => '(', $out .= $s[$count];
'\\f' => chr(0x0C), } else {
'\\b' => chr(0x08), switch ($s[++$count]) {
'\\t' => chr(0x09), case ')':
'\\r' => chr(0x0D), case '(':
'\\n' => chr(0x0A), 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 * @return string
*/ */
function hex2str($hex) { 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 * @return string
*/ */
function str2hex($str) { function str2hex($str) {
return current(unpack("H*", $str)); return current(unpack('H*', $str));
} }
} }

View File

@ -1,8 +1,8 @@
<?php <?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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -17,7 +17,7 @@
// limitations under the License. // limitations under the License.
// //
require_once("pdf_parser.php"); require_once('pdf_parser.php');
class fpdi_pdf_parser extends pdf_parser { class fpdi_pdf_parser extends pdf_parser {
@ -58,7 +58,7 @@ class fpdi_pdf_parser extends pdf_parser {
* *
* @var array * @var array
*/ */
var $availableBoxes = array("/MediaBox","/CropBox","/BleedBox","/TrimBox","/ArtBox"); var $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');
/** /**
* Constructor * Constructor
@ -66,9 +66,8 @@ class fpdi_pdf_parser extends pdf_parser {
* @param string $filename Source-Filename * @param string $filename Source-Filename
* @param object $fpdi Object of type fpdi * @param object $fpdi Object of type fpdi
*/ */
function fpdi_pdf_parser($filename,&$fpdi) { function fpdi_pdf_parser($filename, &$fpdi) {
$this->fpdi =& $fpdi; $this->fpdi =& $fpdi;
$this->filename = $filename;
parent::pdf_parser($filename); parent::pdf_parser($filename);
@ -110,7 +109,7 @@ class fpdi_pdf_parser extends pdf_parser {
$pageno = ((int) $pageno) - 1; $pageno = ((int) $pageno) - 1;
if ($pageno < 0 || $pageno >= $this->getPageCount()) { if ($pageno < 0 || $pageno >= $this->getPageCount()) {
$this->fpdi->error("Pagenumber is wrong!"); $this->fpdi->error('Pagenumber is wrong!');
} }
$this->pageno = $pageno; $this->pageno = $pageno;
@ -163,7 +162,7 @@ class fpdi_pdf_parser extends pdf_parser {
* @return string * @return string
*/ */
function getContent() { function getContent() {
$buffer = ""; $buffer = '';
if (isset($this->pages[$this->pageno][1][1]['/Contents'])) { if (isset($this->pages[$this->pageno][1][1]['/Contents'])) {
$contents = $this->_getPageContent($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) { foreach ($filters AS $_filter) {
switch ($_filter[1]) { switch ($_filter[1]) {
case "/FlateDecode": case '/FlateDecode':
if (function_exists('gzuncompress')) { if (function_exists('gzuncompress')) {
$stream = (strlen($stream) > 0) ? @gzuncompress($stream) : ''; $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
} else { } 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) { if ($stream === false) {
$this->fpdi->error("Error while decompressing stream."); $this->fpdi->error('Error while decompressing stream.');
} }
break; break;
case null: case null:
$stream = $stream; $stream = $stream;
break; break;
default: 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); $filterName = substr($_filter[1],1);
if (class_exists($filterName)) { if (class_exists($filterName)) {
$decoder =& new $filterName($this->fpdi); $decoder =& new $filterName($this->fpdi);
$stream = $decoder->decode(trim($stream)); $stream = $decoder->decode($stream);
} else { } else {
$this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1])); $this->fpdi->error(sprintf('Unsupported Filter: %s',$_filter[1]));
} }
} else { } 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) { if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
$b =& $box[1]; $b =& $box[1];
return array("x" => $b[0][1]/$this->fpdi->k, return array('x' => $b[0][1]/$this->fpdi->k,
"y" => $b[1][1]/$this->fpdi->k, 'y' => $b[1][1]/$this->fpdi->k,
"w" => abs($b[0][1]-$b[2][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); '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'])) { } else if (!isset ($page[1][1]['/Parent'])) {
return false; return false;
} else { } else {
@ -352,7 +356,7 @@ class fpdi_pdf_parser extends pdf_parser {
$kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']); $kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
if (!is_array($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) { foreach ($kids[1] as $v) {
$pg = $this->pdf_resolve_object ($c, $v); $pg = $this->pdf_resolve_object ($c, $v);
if ($pg[1][1]['/Type'][1] === '/Pages') { if ($pg[1][1]['/Type'][1] === '/Pages') {

View File

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

View File

@ -1,8 +1,8 @@
<?php <?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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with 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')) if (!defined ('PDF_TYPE_REAL'))
define ('PDF_TYPE_REAL', 12); define ('PDF_TYPE_REAL', 12);
require_once("pdf_context.php"); require_once('pdf_context.php');
require_once("wrapper_functions.php");
class pdf_parser { class pdf_parser {
@ -93,17 +92,18 @@ class pdf_parser {
function pdf_parser($filename) { function pdf_parser($filename) {
$this->filename = $filename; $this->filename = $filename;
$this->f = @fopen($this->filename, "rb"); $this->f = @fopen($this->filename, 'rb');
if (!$this->f) if (!$this->f)
$this->error(sprintf("Cannot open %s !", $filename)); $this->error(sprintf('Cannot open %s !', $filename));
$this->getPDFVersion(); $this->getPDFVersion();
$this->c =& new pdf_context($this->f); $this->c =& new pdf_context($this->f);
// Read xref-Data // Read xref-Data
$this->pdf_read_xref($this->xref, $this->pdf_find_xref()); $this->pdf_read_xref($this->xref, $this->pdf_find_xref());
// Check for Encryption // Check for Encryption
$this->getEncryption(); $this->getEncryption();
@ -127,7 +127,7 @@ class pdf_parser {
* @param string $msg Error-Message * @param string $msg Error-Message
*/ */
function error($msg) { 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() { function getEncryption() {
if (isset($this->xref['trailer'][1]['/Encrypt'])) { 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() { function pdf_find_root() {
if ($this->xref['trailer'][1]['/Root'][0] != PDF_TYPE_OBJREF) { 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']; return $this->xref['trailer'][1]['/Root'];
} }
@ -166,7 +166,7 @@ class pdf_parser {
*/ */
function getPDFVersion() { function getPDFVersion() {
fseek($this->f, 0); 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])) if (isset($m[0]))
$this->pdfVersion = $m[0]; $this->pdfVersion = $m[0];
return $this->pdfVersion; return $this->pdfVersion;
@ -188,7 +188,7 @@ class pdf_parser {
$data = substr($data, $pos); $data = substr($data, $pos);
if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) { 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]; return (int) $matches[1];
@ -201,6 +201,7 @@ class pdf_parser {
* @param integer $offset of xref-table * @param integer $offset of xref-table
*/ */
function pdf_read_xref(&$result, $offset) { function pdf_read_xref(&$result, $offset) {
fseek($this->f, $o_pos = $offset-20); // set some bytes backwards to fetch errorious docs fseek($this->f, $o_pos = $offset-20); // set some bytes backwards to fetch errorious docs
$data = fread($this->f, 100); $data = fread($this->f, 100);
@ -252,7 +253,7 @@ class pdf_parser {
for ($i = 0; $i < $linesCount; $i++) { for ($i = 0; $i < $linesCount; $i++) {
$line = trim($lines[$i]); $line = trim($lines[$i]);
if ($line) { if ($line) {
$pieces = explode(" ", $line); $pieces = explode(' ', $line);
$c = count($pieces); $c = count($pieces);
switch($c) { switch($c) {
case 2: case 2:
@ -292,7 +293,7 @@ class pdf_parser {
} }
if (isset($trailer[1]['/Prev'])) { if (isset($trailer[1]['/Prev'])) {
$this->pdf_read_xref($result, $trailer[1]['/Prev'][1]); $this->pdf_read_xref($result, $trailer[1]['/Prev'][1]);
} }
$trailer = null; $trailer = null;
@ -317,7 +318,7 @@ class pdf_parser {
return false; return false;
} }
switch ($token) { switch ($token) {
case '<': case '<':
// This is a hex string. // This is a hex string.
// Read the value, then the terminator // Read the value, then the terminator
@ -357,11 +358,18 @@ class pdf_parser {
if ($key === false) { if ($key === false) {
return false; return false;
} }
if (($value = $this->pdf_read_value($c)) === false) { if (($value = $this->pdf_read_value($c)) === false) {
return false; return false;
} }
$result[$key] = $value;
// Catch missing value
if ($value[0] == PDF_TYPE_TOKEN && $value[1] == '>>') {
$result[$key] = array(PDF_TYPE_NULL);
break;
}
$result[$key] = $value;
} }
return array (PDF_TYPE_DICTIONARY, $result); return array (PDF_TYPE_DICTIONARY, $result);
@ -413,7 +421,7 @@ class pdf_parser {
return array (PDF_TYPE_STRING, $result); return array (PDF_TYPE_STRING, $result);
case "stream": case 'stream':
$o_pos = ftell($c->file)-strlen($c->buffer); $o_pos = ftell($c->file)-strlen($c->buffer);
$o_offset = $c->offset; $o_offset = $c->offset;
@ -443,31 +451,7 @@ class pdf_parser {
return array(PDF_TYPE_STREAM, $v); return array(PDF_TYPE_STREAM, $v);
case '%': default :
// 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)) { if (is_numeric ($token)) {
// A numeric token. Make sure that // A numeric token. Make sure that
// it is not part of something else. // it is not part of something else.
@ -502,6 +486,8 @@ class pdf_parser {
return array (PDF_TYPE_REAL, (float)$token); return array (PDF_TYPE_REAL, (float)$token);
} else if ($token == 'true' || $token == 'false') { } else if ($token == 'true' || $token == 'false') {
return array (PDF_TYPE_BOOLEAN, $token == 'true'); return array (PDF_TYPE_BOOLEAN, $token == 'true');
} else if ($token == 'null') {
return array (PDF_TYPE_NULL);
} else { } else {
// Just a token. Return it. // Just a token. Return it.
@ -542,7 +528,7 @@ class pdf_parser {
$c->reset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]]); $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]) { 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"); $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()) { if (!$c->ensure_content()) {
return false; 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); } while ($c->offset >= $c->length - 1);
// Get the first character in the stream // Get the first character in the stream
@ -628,7 +614,7 @@ class pdf_parser {
case ']' : case ']' :
case '(' : case '(' :
case ')' : case ')' :
// This is either an array or literal string // This is either an array or literal string
// delimiter, Return it // delimiter, Return it
@ -651,6 +637,26 @@ class pdf_parser {
return $char; 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 : default :
// This is "another" type of token (probably // This is "another" type of token (probably
@ -665,7 +671,7 @@ class pdf_parser {
// Determine the length of the token // 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) { if ($c->offset + $pos <= $c->length - 1) {
break; break;
} else { } else {
@ -685,6 +691,4 @@ class pdf_parser {
return $result; return $result;
} }
} }
} }