2009-03-06 03:20:46 +00:00
|
|
|
<?php
|
|
|
|
//
|
2014-05-15 13:34:18 +00:00
|
|
|
// FPDI - Version 1.5.1
|
2009-03-06 03:20:46 +00:00
|
|
|
//
|
2014-05-15 13:34:18 +00:00
|
|
|
// Copyright 2004-2014 Setasign - Jan Slabon
|
2009-03-06 03:20:46 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
require_once('fpdf_tpl.php');
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
/**
|
|
|
|
* Class FPDI
|
|
|
|
*/
|
|
|
|
class FPDI extends FPDF_TPL
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* FPDI version
|
|
|
|
*
|
|
|
|
* @string
|
|
|
|
*/
|
|
|
|
const VERSION = '1.5.1';
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Actual filename
|
2014-05-15 13:34:18 +00:00
|
|
|
*
|
2009-03-06 03:20:46 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public $currentFilename;
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parser-Objects
|
2014-05-15 13:34:18 +00:00
|
|
|
*
|
|
|
|
* @var fpdi_pdf_parser[]
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public $parsers = array();
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current parser
|
2014-05-15 13:34:18 +00:00
|
|
|
*
|
|
|
|
* @var fpdi_pdf_parser
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public $currentParser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the last imported page box
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $lastUsedPageBox;
|
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Object stack
|
|
|
|
*
|
2009-03-06 03:20:46 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected $_objStack;
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Done object stack
|
|
|
|
*
|
2009-03-06 03:20:46 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected $_doneObjStack;
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current Object Id.
|
2014-05-15 13:34:18 +00:00
|
|
|
*
|
2009-03-06 03:20:46 +00:00
|
|
|
* @var integer
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected $_currentObjId;
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2011-11-03 02:33:37 +00:00
|
|
|
/**
|
|
|
|
* Cache for imported pages/template ids
|
2014-05-15 13:34:18 +00:00
|
|
|
*
|
2011-11-03 02:33:37 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected $_importedPages = array();
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Set a source-file.
|
2009-03-06 03:20:46 +00:00
|
|
|
*
|
2014-05-15 13:34:18 +00:00
|
|
|
* Depending on the PDF version of the used document the PDF version of the resulting document will
|
|
|
|
* be adjusted to the higher version.
|
|
|
|
*
|
|
|
|
* @param string $filename A valid path to the PDF document from which pages should be imported from
|
|
|
|
* @return int The number of pages in the document
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public function setSourceFile($filename)
|
|
|
|
{
|
|
|
|
$_filename = realpath($filename);
|
|
|
|
if (false !== $_filename)
|
|
|
|
$filename = $_filename;
|
|
|
|
|
|
|
|
$this->currentFilename = $filename;
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
if (!isset($this->parsers[$filename])) {
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->parsers[$filename] = $this->_getPdfParser($filename);
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->setPdfVersion(
|
|
|
|
max($this->getPdfVersion(), $this->parsers[$filename]->getPdfVersion())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->currentParser =& $this->parsers[$filename];
|
2011-11-03 02:33:37 +00:00
|
|
|
|
|
|
|
return $this->parsers[$filename]->getPageCount();
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 02:33:37 +00:00
|
|
|
/**
|
|
|
|
* Returns a PDF parser object
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return fpdi_pdf_parser
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected function _getPdfParser($filename)
|
|
|
|
{
|
|
|
|
require_once('fpdi_pdf_parser.php');
|
|
|
|
return new fpdi_pdf_parser($filename);
|
2011-11-03 02:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Get the current PDF version.
|
2011-11-03 02:33:37 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public function getPdfVersion()
|
|
|
|
{
|
2011-11-03 02:33:37 +00:00
|
|
|
return $this->PDFVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Set the PDF version.
|
2011-11-03 02:33:37 +00:00
|
|
|
*
|
2014-05-15 13:34:18 +00:00
|
|
|
* @param string $version
|
2011-11-03 02:33:37 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public function setPdfVersion($version = '1.3')
|
|
|
|
{
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->PDFVersion = $version;
|
|
|
|
}
|
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Import a page.
|
|
|
|
*
|
|
|
|
* The second parameter defines the bounding box that should be used to transform the page into a
|
|
|
|
* form XObject.
|
|
|
|
*
|
|
|
|
* Following values are available: MediaBox, CropBox, BleedBox, TrimBox, ArtBox.
|
|
|
|
* If a box is not especially defined its default box will be used:
|
|
|
|
*
|
|
|
|
* <ul>
|
|
|
|
* <li>CropBox: Default -> MediaBox</li>
|
|
|
|
* <li>BleedBox: Default -> CropBox</li>
|
|
|
|
* <li>TrimBox: Default -> CropBox</li>
|
|
|
|
* <li>ArtBox: Default -> CropBox</li>
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* It is possible to get the used page box by the {@link getLastUsedPageBox()} method.
|
2009-03-06 03:20:46 +00:00
|
|
|
*
|
2014-05-15 13:34:18 +00:00
|
|
|
* @param int $pageNo The page number
|
|
|
|
* @param string $boxName The boundary box to use when transforming the page into a form XObject
|
|
|
|
* @param boolean $groupXObject Define the form XObject as a group XObject to support transparency (if used)
|
|
|
|
* @return int An id of the imported page/template to use with e.g. fpdf_tpl::useTemplate()
|
|
|
|
* @throws LogicException|InvalidArgumentException
|
|
|
|
* @see getLastUsedPageBox()
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public function importPage($pageNo, $boxName = 'CropBox', $groupXObject = true)
|
|
|
|
{
|
|
|
|
if ($this->_inTpl) {
|
|
|
|
throw new LogicException('Please import the desired pages before creating a new template.');
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
$fn = $this->currentFilename;
|
|
|
|
$boxName = '/' . ltrim($boxName, '/');
|
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
// check if page already imported
|
2014-05-15 13:34:18 +00:00
|
|
|
$pageKey = $fn . '-' . ((int)$pageNo) . $boxName;
|
|
|
|
if (isset($this->_importedPages[$pageKey])) {
|
2009-03-06 03:20:46 +00:00
|
|
|
return $this->_importedPages[$pageKey];
|
2014-05-15 13:34:18 +00:00
|
|
|
}
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
$parser = $this->parsers[$fn];
|
|
|
|
$parser->setPageNo($pageNo);
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
if (!in_array($boxName, $parser->availableBoxes)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Unknown box: %s', $boxName));
|
|
|
|
}
|
2011-11-03 02:33:37 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
$pageBoxes = $parser->getPageBoxes($pageNo, $this->k);
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaBox
|
|
|
|
* CropBox: Default -> MediaBox
|
|
|
|
* BleedBox: Default -> CropBox
|
|
|
|
* TrimBox: Default -> CropBox
|
|
|
|
* ArtBox: Default -> CropBox
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
if (!isset($pageBoxes[$boxName]) && ($boxName == '/BleedBox' || $boxName == '/TrimBox' || $boxName == '/ArtBox'))
|
2009-07-17 08:38:31 +00:00
|
|
|
$boxName = '/CropBox';
|
2014-05-15 13:34:18 +00:00
|
|
|
if (!isset($pageBoxes[$boxName]) && $boxName == '/CropBox')
|
2009-07-17 08:38:31 +00:00
|
|
|
$boxName = '/MediaBox';
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
if (!isset($pageBoxes[$boxName]))
|
2009-03-06 03:20:46 +00:00
|
|
|
return false;
|
2011-11-03 02:33:37 +00:00
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
$this->lastUsedPageBox = $boxName;
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
$box = $pageBoxes[$boxName];
|
2011-11-03 02:33:37 +00:00
|
|
|
|
|
|
|
$this->tpl++;
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_tpls[$this->tpl] = array();
|
|
|
|
$tpl =& $this->_tpls[$this->tpl];
|
|
|
|
$tpl['parser'] = $parser;
|
2011-11-03 02:33:37 +00:00
|
|
|
$tpl['resources'] = $parser->getPageResources();
|
|
|
|
$tpl['buffer'] = $parser->getContent();
|
2009-03-06 03:20:46 +00:00
|
|
|
$tpl['box'] = $box;
|
2014-05-15 13:34:18 +00:00
|
|
|
$tpl['groupXObject'] = $groupXObject;
|
|
|
|
if ($groupXObject) {
|
|
|
|
$this->setPdfVersion(max($this->getPdfVersion(), 1.4));
|
|
|
|
}
|
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
// To build an array that can be used by PDF_TPL::useTemplate()
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_tpls[$this->tpl] = array_merge($this->_tpls[$this->tpl], $box);
|
2009-07-17 08:38:31 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
// An imported page will start at 0,0 all the time. Translation will be set in _putformxobjects()
|
2009-03-06 03:20:46 +00:00
|
|
|
$tpl['x'] = 0;
|
|
|
|
$tpl['y'] = 0;
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
// handle rotated pages
|
2014-05-15 13:34:18 +00:00
|
|
|
$rotation = $parser->getPageRotation($pageNo);
|
2009-07-17 08:38:31 +00:00
|
|
|
$tpl['_rotationAngle'] = 0;
|
2009-03-06 03:20:46 +00:00
|
|
|
if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0) {
|
2011-11-03 02:33:37 +00:00
|
|
|
$steps = $angle / 90;
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
$_w = $tpl['w'];
|
|
|
|
$_h = $tpl['h'];
|
|
|
|
$tpl['w'] = $steps % 2 == 0 ? $_w : $_h;
|
|
|
|
$tpl['h'] = $steps % 2 == 0 ? $_h : $_w;
|
|
|
|
|
2011-11-03 02:33:37 +00:00
|
|
|
if ($angle < 0)
|
|
|
|
$angle += 360;
|
|
|
|
|
|
|
|
$tpl['_rotationAngle'] = $angle * -1;
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_importedPages[$pageKey] = $this->tpl;
|
|
|
|
|
|
|
|
return $this->tpl;
|
|
|
|
}
|
|
|
|
|
2011-11-03 02:33:37 +00:00
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Returns the last used page boundary box.
|
2011-11-03 02:33:37 +00:00
|
|
|
*
|
2014-05-15 13:34:18 +00:00
|
|
|
* @return string The used boundary box: MediaBox, CropBox, BleedBox, TrimBox or ArtBox
|
2011-11-03 02:33:37 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public function getLastUsedPageBox()
|
|
|
|
{
|
2009-03-06 03:20:46 +00:00
|
|
|
return $this->lastUsedPageBox;
|
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use a template or imported page in current page or other template.
|
|
|
|
*
|
|
|
|
* You can use a template in a page or in another template.
|
|
|
|
* You can give the used template a new size. All parameters are optional.
|
|
|
|
* The width or height is calculated automatically if one is given. If no
|
|
|
|
* parameter is given the origin size as defined in beginTemplate() or of
|
|
|
|
* the imported page is used.
|
|
|
|
*
|
|
|
|
* The calculated or used width and height are returned as an array.
|
|
|
|
*
|
|
|
|
* @param int $tplIdx A valid template-id
|
|
|
|
* @param int $x The x-position
|
|
|
|
* @param int $y The y-position
|
|
|
|
* @param int $w The new width of the template
|
|
|
|
* @param int $h The new height of the template
|
|
|
|
* @param boolean $adjustPageSize If set to true the current page will be resized to fit the dimensions
|
|
|
|
* of the template
|
|
|
|
*
|
|
|
|
* @return array The height and width of the template (array('w' => ..., 'h' => ...))
|
|
|
|
* @throws LogicException|InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public 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);
|
2011-11-03 02:33:37 +00:00
|
|
|
$orientation = $size['w'] > $size['h'] ? 'L' : 'P';
|
|
|
|
$size = array($size['w'], $size['h']);
|
|
|
|
|
|
|
|
if (is_subclass_of($this, 'TCPDF')) {
|
|
|
|
$this->setPageFormat($size, $orientation);
|
|
|
|
} else {
|
|
|
|
$size = $this->_getpagesize($size);
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
if($orientation != $this->CurOrientation ||
|
|
|
|
$size[0] != $this->CurPageSize[0] ||
|
|
|
|
$size[1] != $this->CurPageSize[1]
|
|
|
|
) {
|
2011-11-03 02:33:37 +00:00
|
|
|
// New size or orientation
|
2014-05-15 13:34:18 +00:00
|
|
|
if ($orientation=='P') {
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->w = $size[0];
|
|
|
|
$this->h = $size[1];
|
2014-05-15 13:34:18 +00:00
|
|
|
} else {
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->w = $size[1];
|
|
|
|
$this->h = $size[0];
|
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->wPt = $this->w * $this->k;
|
|
|
|
$this->hPt = $this->h * $this->k;
|
|
|
|
$this->PageBreakTrigger = $this->h - $this->bMargin;
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->CurOrientation = $orientation;
|
|
|
|
$this->CurPageSize = $size;
|
|
|
|
$this->PageSizes[$this->page] = array($this->wPt, $this->hPt);
|
|
|
|
}
|
|
|
|
}
|
2009-07-17 08:38:31 +00:00
|
|
|
}
|
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
$this->_out('q 0 J 1 w 0 j 0 G 0 g'); // reset standard values
|
2014-05-15 13:34:18 +00:00
|
|
|
$size = parent::useTemplate($tplIdx, $x, $y, $w, $h);
|
2009-03-06 03:20:46 +00:00
|
|
|
$this->_out('Q');
|
2011-11-03 02:33:37 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
return $size;
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Copy all imported objects to the resulting document.
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected function _putimportedobjects()
|
|
|
|
{
|
|
|
|
foreach($this->parsers AS $filename => $p) {
|
|
|
|
$this->currentParser =& $p;
|
|
|
|
if (!isset($this->_objStack[$filename]) || !is_array($this->_objStack[$filename])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while(($n = key($this->_objStack[$filename])) !== null) {
|
|
|
|
$nObj = $this->currentParser->resolveObject($this->_objStack[$filename][$n][1]);
|
|
|
|
|
|
|
|
$this->_newobj($this->_objStack[$filename][$n][0]);
|
|
|
|
|
|
|
|
if ($nObj[0] == pdf_parser::TYPE_STREAM) {
|
|
|
|
$this->_writeValue($nObj);
|
|
|
|
} else {
|
|
|
|
$this->_writeValue($nObj[1]);
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
|
|
|
|
$this->_out("\nendobj");
|
|
|
|
$this->_objStack[$filename][$n] = null; // free memory
|
|
|
|
unset($this->_objStack[$filename][$n]);
|
|
|
|
reset($this->_objStack[$filename]);
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Writes the form XObjects to the PDF document.
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected function _putformxobjects()
|
|
|
|
{
|
|
|
|
$filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
|
|
|
|
reset($this->_tpls);
|
|
|
|
foreach($this->_tpls AS $tplIdx => $tpl) {
|
|
|
|
$this->_newobj();
|
|
|
|
$currentN = $this->n; // TCPDF/Protection: rem current "n"
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_tpls[$tplIdx]['n'] = $this->n;
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->_out('<<' . $filter . '/Type /XObject');
|
2009-03-06 03:20:46 +00:00
|
|
|
$this->_out('/Subtype /Form');
|
|
|
|
$this->_out('/FormType 1');
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
|
2011-11-03 02:33:37 +00:00
|
|
|
(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
|
2009-07-17 08:38:31 +00:00
|
|
|
));
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
$c = 1;
|
|
|
|
$s = 0;
|
|
|
|
$tx = 0;
|
|
|
|
$ty = 0;
|
|
|
|
|
|
|
|
if (isset($tpl['box'])) {
|
|
|
|
$tx = -$tpl['box']['llx'];
|
2011-11-03 02:33:37 +00:00
|
|
|
$ty = -$tpl['box']['lly'];
|
2009-07-17 08:38:31 +00:00
|
|
|
|
|
|
|
if ($tpl['_rotationAngle'] <> 0) {
|
|
|
|
$angle = $tpl['_rotationAngle'] * M_PI/180;
|
2014-05-15 13:34:18 +00:00
|
|
|
$c = cos($angle);
|
|
|
|
$s = sin($angle);
|
2009-07-17 08:38:31 +00:00
|
|
|
|
|
|
|
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:
|
2011-11-03 02:33:37 +00:00
|
|
|
$tx = $tpl['box']['ury'];
|
|
|
|
$ty = -$tpl['box']['llx'];
|
2009-07-17 08:38:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($tpl['x'] != 0 || $tpl['y'] != 0) {
|
2011-11-03 02:33:37 +00:00
|
|
|
$tx = -$tpl['x'] * 2;
|
|
|
|
$ty = $tpl['y'] * 2;
|
2009-07-17 08:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
));
|
|
|
|
}
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
$this->_out('/Resources ');
|
|
|
|
|
|
|
|
if (isset($tpl['resources'])) {
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->currentParser = $tpl['parser'];
|
|
|
|
$this->_writeValue($tpl['resources']); // "n" will be changed
|
2009-03-06 03:20:46 +00:00
|
|
|
} else {
|
2014-05-15 13:34:18 +00:00
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
$this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
|
2014-05-15 13:34:18 +00:00
|
|
|
if (isset($this->_res['tpl'][$tplIdx])) {
|
|
|
|
$res = $this->_res['tpl'][$tplIdx];
|
|
|
|
|
|
|
|
if (isset($res['fonts']) && count($res['fonts'])) {
|
|
|
|
$this->_out('/Font <<');
|
|
|
|
foreach ($res['fonts'] as $font)
|
|
|
|
$this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');
|
|
|
|
$this->_out('>>');
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
if (isset($res['images']) && count($res['images']) ||
|
|
|
|
isset($res['tpls']) && count($res['tpls']))
|
|
|
|
{
|
|
|
|
$this->_out('/XObject <<');
|
|
|
|
if (isset($res['images'])) {
|
|
|
|
foreach ($res['images'] as $image)
|
|
|
|
$this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R');
|
|
|
|
}
|
|
|
|
if (isset($res['tpls'])) {
|
|
|
|
foreach ($res['tpls'] as $i => $_tpl)
|
|
|
|
$this->_out($this->tplPrefix . $i . ' ' . $_tpl['n'] . ' 0 R');
|
|
|
|
}
|
|
|
|
$this->_out('>>');
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
$this->_out('>>');
|
2014-05-15 13:34:18 +00:00
|
|
|
}
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
if (isset($tpl['groupXObject']) && $tpl['groupXObject']) {
|
|
|
|
$this->_out('/Group <</Type/Group/S/Transparency>>');
|
|
|
|
}
|
|
|
|
|
|
|
|
$newN = $this->n; // TCPDF: rem new "n"
|
|
|
|
$this->n = $currentN; // TCPDF: reset to current "n"
|
|
|
|
|
|
|
|
$buffer = ($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];
|
|
|
|
|
2011-11-03 02:33:37 +00:00
|
|
|
if (is_subclass_of($this, 'TCPDF')) {
|
2014-05-15 13:34:18 +00:00
|
|
|
$buffer = $this->_getrawstream($buffer);
|
|
|
|
$this->_out('/Length ' . strlen($buffer) . ' >>');
|
|
|
|
$this->_out("stream\n" . $buffer . "\nendstream");
|
2011-11-03 02:33:37 +00:00
|
|
|
} else {
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_out('/Length ' . strlen($buffer) . ' >>');
|
|
|
|
$this->_putstream($buffer);
|
2011-11-03 02:33:37 +00:00
|
|
|
}
|
2009-03-06 03:20:46 +00:00
|
|
|
$this->_out('endobj');
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->n = $newN; // TCPDF: reset to new "n"
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
2009-07-17 08:38:31 +00:00
|
|
|
|
|
|
|
$this->_putimportedobjects();
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Creates and optionally write the object definition to the document.
|
|
|
|
*
|
2009-03-06 03:20:46 +00:00
|
|
|
* Rewritten to handle existing own defined objects
|
2014-05-15 13:34:18 +00:00
|
|
|
*
|
|
|
|
* @param bool $objId
|
|
|
|
* @param bool $onlyNewObj
|
|
|
|
* @return bool|int
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public function _newobj($objId = false, $onlyNewObj = false)
|
|
|
|
{
|
|
|
|
if (!$objId) {
|
|
|
|
$objId = ++$this->n;
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
//Begin a new object
|
2014-05-15 13:34:18 +00:00
|
|
|
if (!$onlyNewObj) {
|
|
|
|
$this->offsets[$objId] = is_subclass_of($this, 'TCPDF') ? $this->bufferlen : strlen($this->buffer);
|
|
|
|
$this->_out($objId . ' 0 obj');
|
|
|
|
$this->_currentObjId = $objId; // for later use with encryption
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
2011-11-03 02:33:37 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
return $objId;
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Writes a PDF value to the resulting document.
|
|
|
|
*
|
2009-03-06 03:20:46 +00:00
|
|
|
* Needed to rebuild the source document
|
|
|
|
*
|
|
|
|
* @param mixed $value A PDF-Value. Structure of values see cases in this method
|
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected function _writeValue(&$value)
|
2009-03-06 03:20:46 +00:00
|
|
|
{
|
|
|
|
if (is_subclass_of($this, 'TCPDF')) {
|
2014-05-15 13:34:18 +00:00
|
|
|
parent::_prepareValue($value);
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ($value[0]) {
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_TOKEN:
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_straightOut($value[1] . ' ');
|
|
|
|
break;
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_NUMERIC:
|
|
|
|
case pdf_parser::TYPE_REAL:
|
2009-07-17 08:38:31 +00:00
|
|
|
if (is_float($value[1]) && $value[1] != 0) {
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->_straightOut(rtrim(rtrim(sprintf('%F', $value[1]), '0'), '.') . ' ');
|
2009-07-17 08:38:31 +00:00
|
|
|
} else {
|
|
|
|
$this->_straightOut($value[1] . ' ');
|
|
|
|
}
|
2009-03-06 03:20:46 +00:00
|
|
|
break;
|
2009-07-17 08:38:31 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_ARRAY:
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
// An array. Output the proper
|
|
|
|
// structure and move on.
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_straightOut('[');
|
2009-03-06 03:20:46 +00:00
|
|
|
for ($i = 0; $i < count($value[1]); $i++) {
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_writeValue($value[1][$i]);
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_out(']');
|
2009-03-06 03:20:46 +00:00
|
|
|
break;
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_DICTIONARY:
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
// A dictionary.
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_straightOut('<<');
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
reset ($value[1]);
|
|
|
|
|
|
|
|
while (list($k, $v) = each($value[1])) {
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_straightOut($k . ' ');
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_writeValue($v);
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_straightOut('>>');
|
2009-03-06 03:20:46 +00:00
|
|
|
break;
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_OBJREF:
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
// An indirect object reference
|
|
|
|
// Fill the object stack if needed
|
2014-05-15 13:34:18 +00:00
|
|
|
$cpfn =& $this->currentParser->filename;
|
|
|
|
if (!isset($this->_doneObjStack[$cpfn][$value[1]])) {
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->_newobj(false, true);
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_objStack[$cpfn][$value[1]] = array($this->n, $value);
|
|
|
|
$this->_doneObjStack[$cpfn][$value[1]] = array($this->n, $value);
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
$objId = $this->_doneObjStack[$cpfn][$value[1]][0];
|
2009-03-06 03:20:46 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_out($objId . ' 0 R');
|
2009-03-06 03:20:46 +00:00
|
|
|
break;
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_STRING:
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
// A string.
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->_straightOut('(' . $value[1] . ')');
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_STREAM:
|
2009-03-06 03:20:46 +00:00
|
|
|
|
|
|
|
// A stream. First, output the
|
|
|
|
// stream dictionary, then the
|
|
|
|
// stream data itself.
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_writeValue($value[1]);
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_out('stream');
|
2009-03-06 03:20:46 +00:00
|
|
|
$this->_out($value[2][1]);
|
2014-05-15 13:34:18 +00:00
|
|
|
$this->_straightOut("endstream");
|
2009-03-06 03:20:46 +00:00
|
|
|
break;
|
2011-11-03 02:33:37 +00:00
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_HEX:
|
2011-11-03 02:33:37 +00:00
|
|
|
$this->_straightOut('<' . $value[1] . '>');
|
2009-03-06 03:20:46 +00:00
|
|
|
break;
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_BOOLEAN:
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_straightOut($value[1] ? 'true ' : 'false ');
|
2009-03-06 03:20:46 +00:00
|
|
|
break;
|
|
|
|
|
2014-05-15 13:34:18 +00:00
|
|
|
case pdf_parser::TYPE_NULL:
|
2009-03-06 03:20:46 +00:00
|
|
|
// The null object.
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->_straightOut('null ');
|
2009-03-06 03:20:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Modified _out() method so not each call will add a newline to the output.
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected function _straightOut($s)
|
|
|
|
{
|
2009-07-17 08:38:31 +00:00
|
|
|
if (!is_subclass_of($this, 'TCPDF')) {
|
2014-05-15 13:34:18 +00:00
|
|
|
if ($this->state == 2) {
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->pages[$this->page] .= $s;
|
2014-05-15 13:34:18 +00:00
|
|
|
} else {
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->buffer .= $s;
|
2014-05-15 13:34:18 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 08:38:31 +00:00
|
|
|
} else {
|
|
|
|
if ($this->state == 2) {
|
2014-05-15 13:34:18 +00:00
|
|
|
if ($this->inxobj) {
|
|
|
|
// we are inside an XObject template
|
|
|
|
$this->xobjects[$this->xobjid]['outdata'] .= $s;
|
|
|
|
} else if ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
|
2009-03-06 03:20:46 +00:00
|
|
|
// puts data before page footer
|
2014-05-15 13:34:18 +00:00
|
|
|
$pagebuff = $this->getPageBuffer($this->page);
|
|
|
|
$page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
|
|
|
|
$footer = substr($pagebuff, -$this->footerlen[$this->page]);
|
|
|
|
$this->setPageBuffer($this->page, $page . $s . $footer);
|
|
|
|
// update footer position
|
|
|
|
$this->footerpos[$this->page] += strlen($s);
|
2009-03-06 03:20:46 +00:00
|
|
|
} else {
|
2014-05-15 13:34:18 +00:00
|
|
|
// set page data
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->setPageBuffer($this->page, $s, true);
|
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
} else if ($this->state > 0) {
|
|
|
|
// set general data
|
2009-07-17 08:38:31 +00:00
|
|
|
$this->setBuffer($s);
|
|
|
|
}
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Ends the document
|
2009-03-06 03:20:46 +00:00
|
|
|
*
|
2014-05-15 13:34:18 +00:00
|
|
|
* Overwritten to close opened parsers
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
public function _enddoc()
|
|
|
|
{
|
2009-03-06 03:20:46 +00:00
|
|
|
parent::_enddoc();
|
|
|
|
$this->_closeParsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-15 13:34:18 +00:00
|
|
|
* Close all files opened by parsers.
|
|
|
|
*
|
|
|
|
* @return boolean
|
2009-03-06 03:20:46 +00:00
|
|
|
*/
|
2014-05-15 13:34:18 +00:00
|
|
|
protected function _closeParsers()
|
|
|
|
{
|
|
|
|
if ($this->state > 2) {
|
|
|
|
$this->cleanUp();
|
2009-03-06 03:20:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
|
2009-03-06 03:20:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-15 13:34:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes cycled references and closes the file handles of the parser objects.
|
|
|
|
*/
|
|
|
|
public function cleanUp()
|
|
|
|
{
|
|
|
|
while (($parser = array_pop($this->parsers)) !== null) {
|
|
|
|
/**
|
|
|
|
* @var fpdi_pdf_parser $parser
|
|
|
|
*/
|
|
|
|
$parser->closeFile();
|
|
|
|
}
|
|
|
|
}
|
2009-03-06 03:20:46 +00:00
|
|
|
}
|