PHP Class mikehaertl\pdftk\Pdf

This class is a wrapper around pdftk. The class was developed for pdftk 2.x but should also work with older versions, but you may have to use slightly different page rotation options (e.g 'E' instead 'east').
Author: Michael Härtl ([email protected])
Exibir arquivo Open project: mikehaertl/php-pdftk Class Usage Examples

Public Properties

Property Type Description
$ignoreWarnings whether to ignore any errors if some non-empty output file was still created. Default is false.

Protected Properties

Property Type Description
$_command the command instance that executes pdftk
$_data the PDF data as returned from getData()
$_dataFields the PDF form field data as returned from getDataFields()
$_dataFields_utf8
$_data_utf8
$_error the error message
$_handle a counter for autogenerated handles
$_output the output filename. If null (default) a tmp file is used as output. If false, no output option is added at all.
$_pdf if the input was an instance, we keep a reference here, so that it won't get unlinked before this object gets destroyed
$_tmpFile the temporary output file
$_tmpOutputContentType the content type of the tmp output

Public Methods

Method Description
__construct ( string | Pdf | array $pdf = null, array $options = [] )
addFile ( string | Pdf $name, string | null $handle = null, string | null $password = null ) : Pdf
allow ( string | null $permissions = null ) : Pdf Set PDF permissions
background ( string $file ) : Pdf Apply a PDF as watermark to the background of a single PDF file.
burst ( string | null $filepattern = null ) : boolean Split the PDF document into pages
cat ( integer | string | array $start, integer | string | null $end = null, string | null $handle = null, string | null $qualifier = null, string $rotation = null ) : Pdf Assemble (catenate) pages from the input files.
compress ( boolean $compress = true ) : Pdf Restore/remove compression
dropXfa ( ) : Pdf Drop XFA data from forms created with newer Acrobat.
dropXmp ( ) : Pdf Drop XMP meta data
execute ( ) : boolean Execute the pdftk command and store the output file to a temporary location or $this->_output if set.
fillForm ( string | array $data, $encoding = 'UTF-8', $dropXfa = true, $format = 'xfdf' ) : Pdf Fill a PDF form
flatten ( ) : Pdf Flatten the PDF form fields values into a single PDF file.
generateFdfFile ( string $name ) : Pdf Generate the FDF file for a single PDF file.
getCommand ( ) : Command
getData ( boolean $utf8 = true ) : string | boolean
getDataFields ( boolean $utf8 = true ) : string | boolean
getError ( ) : string
getTmpFile ( ) : mikehaertl\tmp\File
keepId ( $id = 'first' ) : Pdf When combining multiple PDFs, use either the first or last ID in the output.
multiBackground ( string $file ) : Pdf Apply multiple PDF pages as watermark to the corresponding pages of a single PDF file.
multiStamp ( string $file ) : Pdf Add multiple pages from $file as overlay to the corresponding pages of a single PDF file.
needAppearances ( ) : Pdf Set need_appearances flag in PDF
passwordEncryption ( integer $strength = 128 ) : Pdf
saveAs ( string $name ) : boolean Execute the operation and save the output file
send ( string | null $filename = null, boolean $inline = false ) : boolean Send PDF to client, either inline or as download (triggers PDF creation)
setPassword ( string $password ) : Pdf
setUserPassword ( string $password ) : Pdf
shuffle ( integer | array $start, integer | null $end = null, string $handle = null, string | null $qualifier = null, string $rotation = null ) : Pdf Shuffle pages from the input files.
stamp ( string $file ) : Pdf Add $file as overlay to a single PDF file.

Protected Methods

Method Description
constrainSingleFile ( ) Make sure, that only one file is present
nextHandle ( ) : string

Method Details

__construct() public method

public __construct ( string | Pdf | array $pdf = null, array $options = [] )
$pdf string | Pdf | array a pdf filename or Pdf instance or an array of filenames/instances indexed by a handle. The array values can also be arrays of the form array($filename, $password) if some files are password protected.
$options array Options to pass to set on the Command instance, e.g. the pdftk binary path

addFile() public method

public addFile ( string | Pdf $name, string | null $handle = null, string | null $password = null ) : Pdf
$name string | Pdf the PDF filename or Pdf instance to add for processing
$handle string | null one or more uppercase letters A..Z to reference this file later. If no handle is provided, an internal handle is autocreated, consuming the range Z..A
$password string | null the owner (or user) password if any
return Pdf the pdf instance for method chaining

allow() public method

The available permissions are Printing, DegradedPrinting, ModifyContents, Assembly, CopyContents, ScreenReaders, ModifyAnnotations, FillIn, AllFeatures.
public allow ( string | null $permissions = null ) : Pdf
$permissions string | null list of space separated permissions or null for none.
return Pdf the pdf instance for method chaining

background() public method

The PDF file must have a transparent background for the watermark to be visible.
public background ( string $file ) : Pdf
$file string name of the background PDF file. Only the first page is used.
return Pdf the pdf instance for method chaining

burst() public method

Split the PDF document into pages
public burst ( string | null $filepattern = null ) : boolean
$filepattern string | null the output name in sprintf format or null for default 'pg_%04d.pdf'
return boolean whether the burst command was successful

cat() public method

Values for rotation are (in degrees): north: 0, east: 90, south: 180, west: 270, left: -90, right: +90, down: +180. left, right and down make relative adjustments to a page's rotation. Note: Older pdftk versions use N, E, S, W, L, R, and D instead. Example: $pdf = new Pdf; $pdf->addFile('file1.pdf', 'A') ->addFile('file2.pdf', 'B') ->cat(array(1,3),'B')) // pages 1 and 3 of file B ->cat(1, 5, 'A', 'odd') // pages 1, 3, 5 of file A ->cat('end', 5, 'B') // pages 5 to end of file B in reverse order ->cat(null, null, 'B', 'east') // All pages from file B rotated by 90 degree ->saveAs('out.pdf');
public cat ( integer | string | array $start, integer | string | null $end = null, string | null $handle = null, string | null $qualifier = null, string $rotation = null ) : Pdf
$start integer | string | array the start page number or an array of page numbers. If an array, the other arguments will be ignored. $start can also be bigger than $end for pages in reverse order.
$end integer | string | null the end page number or null for single page (or list if $start is an array)
$handle string | null the handle of the file to use. Can be null if only a single file was added.
$qualifier string | null the page number qualifier, either 'even' or 'odd' or null for none
$rotation string the rotation to apply to the pages.
return Pdf the pdf instance for method chaining

compress() public method

Restore/remove compression
public compress ( boolean $compress = true ) : Pdf
$compress boolean whether to restore (default) or remove the compression
return Pdf the pdf instance for method chaining

constrainSingleFile() protected method

Make sure, that only one file is present
protected constrainSingleFile ( )

dropXfa() public method

Newer PDF forms contain both, the newer XFA and the older AcroForm form fields. PDF readers can use both, but will prefer XFA if present. Since pdftk can only fill in AcroForm data you should always add this option when filling in forms with pdftk.
public dropXfa ( ) : Pdf
return Pdf the pdf instance for method chaining

dropXmp() public method

Newer PDFs can contain both, new style XMP data and old style info directory. PDF readers can use both, but will prefer XMP if present. Since pdftk can only update the info directory you should always add this option when updating PDF info.
public dropXmp ( ) : Pdf
return Pdf the pdf instance for method chaining

execute() public method

You should probably never call this method unless you only need a temporary PDF file as result.
public execute ( ) : boolean
return boolean whether the command was executed successfully

fillForm() public method

Fill a PDF form
public fillForm ( string | array $data, $encoding = 'UTF-8', $dropXfa = true, $format = 'xfdf' ) : Pdf
$data string | array either a XFDF/FDF filename or an array with form field data (name => value)
return Pdf the pdf instance for method chaining

flatten() public method

Flatten the PDF form fields values into a single PDF file.
public flatten ( ) : Pdf
return Pdf the pdf instance for method chaining

generateFdfFile() public method

Generate the FDF file for a single PDF file.
public generateFdfFile ( string $name ) : Pdf
$name string name of the FDF file
return Pdf the pdf instance for method chaining

getCommand() public method

public getCommand ( ) : Command
return Command the command instance that executes pdftk

getData() public method

public getData ( boolean $utf8 = true ) : string | boolean
$utf8 boolean whether to dump the data UTF-8 encoded. Default is true.
return string | boolean meta data about the PDF or false on failure

getDataFields() public method

public getDataFields ( boolean $utf8 = true ) : string | boolean
$utf8 boolean whether to dump the data UTF-8 encoded. Default is true.
return string | boolean data about the PDF form fields or false on failure

getError() public method

public getError ( ) : string
return string the error message or an empty string if none

getTmpFile() public method

public getTmpFile ( ) : mikehaertl\tmp\File
return mikehaertl\tmp\File the temporary output file instance

keepId() public method

If not called, a new ID is created.
public keepId ( $id = 'first' ) : Pdf
return Pdf the pdf instance for method chaining

multiBackground() public method

If $file has fewer pages than the PDF file then the last page is repeated as background.
public multiBackground ( string $file ) : Pdf
$file string name of the background PDF file.
return Pdf the pdf instance for method chaining

multiStamp() public method

If $file has fewer pages than the PDF file then the last page is repeated as overlay.
public multiStamp ( string $file ) : Pdf
$file string name of the PDF file to add as overlay
return Pdf the pdf instance for method chaining

needAppearances() public method

This flag makes sure, that a PDF reader takes care of rendering form field content, even if it contains non ASCII characters. You should always use this option if you fill in forms e.g. with Unicode characters. You can't combine this option with flatten() though!
public needAppearances ( ) : Pdf
return Pdf the pdf instance for method chaining

nextHandle() protected method

protected nextHandle ( ) : string
return string the next handle in the series A, B, C, ... Z, AA, AB...

passwordEncryption() public method

public passwordEncryption ( integer $strength = 128 ) : Pdf
$strength integer the password encryption strength. Default is 128
return Pdf the pdf instance for method chaining

saveAs() public method

Execute the operation and save the output file
public saveAs ( string $name ) : boolean
$name string of output file
return boolean whether the PDF could be processed and saved

send() public method

Send PDF to client, either inline or as download (triggers PDF creation)
public send ( string | null $filename = null, boolean $inline = false ) : boolean
$filename string | null the filename to send. If empty, the PDF is streamed inline.
$inline boolean whether to force inline display of the PDF, even if filename is present.
return boolean whether PDF was created successfully

setPassword() public method

public setPassword ( string $password ) : Pdf
$password string the owner password to set on the output PDF
return Pdf the pdf instance for method chaining

setUserPassword() public method

public setUserPassword ( string $password ) : Pdf
$password string the user password to set on the output PDF
return Pdf the pdf instance for method chaining

shuffle() public method

This works the same as cat(), but each call to this method creates a "stream" of pages. The outfile will be assembled by adding one page from each stream at a time. Example: $pdf = new Pdf; $pdf1 = $pdf->addFile('file1.pdf'); $pdf->cat($pdf1, array(1,3,2)) ->cat($pdf1, array(4,5,9) ->saveAs('out.pdf'); This will give the page order 1, 4, 3, 5, 2, 9 in the out.pdf
public shuffle ( integer | array $start, integer | null $end = null, string $handle = null, string | null $qualifier = null, string $rotation = null ) : Pdf
$start integer | array the start page number or an array of page numbers.
$end integer | null the end page number or null for single page (or list if $start is an array)
$handle string the handle of the input file to use
$qualifier string | null the page number qualifier, either 'even' or 'odd' or null for none
$rotation string the rotation to apply to the pages. See cat() for more details.
return Pdf the pdf instance for method chaining

stamp() public method

The $file should have a transparent background.
public stamp ( string $file ) : Pdf
$file string name of the PDF file to add as overlay. Only the first page is used.
return Pdf the pdf instance for method chaining

Property Details

$_command protected_oe property

the command instance that executes pdftk
protected $_command

$_data protected_oe property

the PDF data as returned from getData()
protected $_data

$_dataFields protected_oe property

the PDF form field data as returned from getDataFields()
protected $_dataFields

$_dataFields_utf8 protected_oe property

protected $_dataFields_utf8

$_data_utf8 protected_oe property

protected $_data_utf8

$_error protected_oe property

the error message
protected $_error

$_handle protected_oe property

a counter for autogenerated handles
protected $_handle

$_output protected_oe property

the output filename. If null (default) a tmp file is used as output. If false, no output option is added at all.
protected $_output

$_pdf protected_oe property

if the input was an instance, we keep a reference here, so that it won't get unlinked before this object gets destroyed
protected $_pdf

$_tmpFile protected_oe property

the temporary output file
protected $_tmpFile

$_tmpOutputContentType protected_oe property

the content type of the tmp output
protected $_tmpOutputContentType

$ignoreWarnings public_oe property

whether to ignore any errors if some non-empty output file was still created. Default is false.
public $ignoreWarnings