PHP Класс 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').
Автор: Michael Härtl ([email protected])
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$ignoreWarnings whether to ignore any errors if some non-empty output file was still created. Default is false.

Защищенные свойства (Protected)

Свойство Тип Описание
$_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

Открытые методы

Метод Описание
__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.

Защищенные методы

Метод Описание
constrainSingleFile ( ) Make sure, that only one file is present
nextHandle ( ) : string

Описание методов

__construct() публичный метод

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 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
Результат Pdf the pdf instance for method chaining

allow() публичный метод

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.
Результат Pdf the pdf instance for method chaining

background() публичный метод

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.
Результат Pdf the pdf instance for method chaining

burst() публичный метод

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'
Результат boolean whether the burst command was successful

cat() публичный метод

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.
Результат Pdf the pdf instance for method chaining

compress() публичный метод

Restore/remove compression
public compress ( boolean $compress = true ) : Pdf
$compress boolean whether to restore (default) or remove the compression
Результат Pdf the pdf instance for method chaining

constrainSingleFile() защищенный метод

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

dropXfa() публичный метод

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
Результат Pdf the pdf instance for method chaining

dropXmp() публичный метод

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
Результат Pdf the pdf instance for method chaining

execute() публичный метод

You should probably never call this method unless you only need a temporary PDF file as result.
public execute ( ) : boolean
Результат boolean whether the command was executed successfully

fillForm() публичный метод

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)
Результат Pdf the pdf instance for method chaining

flatten() публичный метод

Flatten the PDF form fields values into a single PDF file.
public flatten ( ) : Pdf
Результат Pdf the pdf instance for method chaining

generateFdfFile() публичный метод

Generate the FDF file for a single PDF file.
public generateFdfFile ( string $name ) : Pdf
$name string name of the FDF file
Результат Pdf the pdf instance for method chaining

getCommand() публичный метод

public getCommand ( ) : Command
Результат Command the command instance that executes pdftk

getData() публичный метод

public getData ( boolean $utf8 = true ) : string | boolean
$utf8 boolean whether to dump the data UTF-8 encoded. Default is true.
Результат string | boolean meta data about the PDF or false on failure

getDataFields() публичный метод

public getDataFields ( boolean $utf8 = true ) : string | boolean
$utf8 boolean whether to dump the data UTF-8 encoded. Default is true.
Результат string | boolean data about the PDF form fields or false on failure

getError() публичный метод

public getError ( ) : string
Результат string the error message or an empty string if none

getTmpFile() публичный метод

public getTmpFile ( ) : mikehaertl\tmp\File
Результат mikehaertl\tmp\File the temporary output file instance

keepId() публичный метод

If not called, a new ID is created.
public keepId ( $id = 'first' ) : Pdf
Результат Pdf the pdf instance for method chaining

multiBackground() публичный метод

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.
Результат Pdf the pdf instance for method chaining

multiStamp() публичный метод

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
Результат Pdf the pdf instance for method chaining

needAppearances() публичный метод

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
Результат Pdf the pdf instance for method chaining

nextHandle() защищенный метод

protected nextHandle ( ) : string
Результат string the next handle in the series A, B, C, ... Z, AA, AB...

passwordEncryption() публичный метод

public passwordEncryption ( integer $strength = 128 ) : Pdf
$strength integer the password encryption strength. Default is 128
Результат Pdf the pdf instance for method chaining

saveAs() публичный метод

Execute the operation and save the output file
public saveAs ( string $name ) : boolean
$name string of output file
Результат boolean whether the PDF could be processed and saved

send() публичный метод

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.
Результат boolean whether PDF was created successfully

setPassword() публичный метод

public setPassword ( string $password ) : Pdf
$password string the owner password to set on the output PDF
Результат Pdf the pdf instance for method chaining

setUserPassword() публичный метод

public setUserPassword ( string $password ) : Pdf
$password string the user password to set on the output PDF
Результат Pdf the pdf instance for method chaining

shuffle() публичный метод

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.
Результат Pdf the pdf instance for method chaining

stamp() публичный метод

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.
Результат Pdf the pdf instance for method chaining

Описание свойств

$_command защищенное свойство

the command instance that executes pdftk
protected $_command

$_data защищенное свойство

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

$_dataFields защищенное свойство

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

$_dataFields_utf8 защищенное свойство

protected $_dataFields_utf8

$_data_utf8 защищенное свойство

protected $_data_utf8

$_error защищенное свойство

the error message
protected $_error

$_handle защищенное свойство

a counter for autogenerated handles
protected $_handle

$_output защищенное свойство

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 защищенное свойство

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 защищенное свойство

the temporary output file
protected $_tmpFile

$_tmpOutputContentType защищенное свойство

the content type of the tmp output
protected $_tmpOutputContentType

$ignoreWarnings публичное свойство

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