1. Go to this page and download the library: Download mikehaertl/php-pdftk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mikehaertl / php-pdftk example snippets
// Create an instance for a single file
$pdf = new Pdf('/path/to/form.pdf');
// Alternatively add files later. Handles are autogenerated in this case.
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf');
$pdf->addFile('/path/to/file2.pdf');
// Add files with own handle
$pdf = new Pdf();
$pdf->addFile('/path/to/file1.pdf', 'A');
$pdf->addFile('/path/to/file2.pdf', 'B');
// Add file with handle and password
$pdf->addFile('/path/to/file3.pdf', 'C', 'secret*password');
// Shortcut to pass all files to the constructor
$pdf = new Pdf([
'A' => ['/path/to/file1.pdf', 'secret*password1'],
'B' => ['/path/to/file2.pdf', 'secret*password2'],
]);
use mikehaertl\pdftk\Pdf;
// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
'name'=>'ÄÜÖ äüö мирано čárka',
'nested.name' => 'valX',
])
->needAppearances()
->saveAs('filled.pdf');
// Always check for errors
if ($result === false) {
$error = $pdf->getError();
}
// Fill form from FDF
$pdf = new Pdf('form.pdf');
$result = $pdf->fillForm('data.xfdf')
->saveAs('filled.pdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
// Fill form with data array
$pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm($data)
->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')
->saveAs('filled.pdf');
use mikehaertl\pdftk\XfdfFile;
use mikehaertl\pdftk\FdfFile;
$xfdf = new XfdfFile(['name' => 'Jürgen мирано']);
$xfdf->saveAs('/path/to/data.xfdf');
$fdf = new FdfFile(['name' => 'Jürgen мирано']);
$fdf->saveAs('/path/to/data.fdf');
use mikehaertl\pdftk\Pdf;
// Extract pages 1-5 and 7,4,9 into a new file
$pdf = new Pdf('/path/to/my.pdf');
$result = $pdf->cat(1, 5)
->cat([7, 4, 9])
->saveAs('/path/to/new.pdf');
if ($result === false) {
$error = $pdf->getError();
}
// Combine pages from several files
$pdf = new Pdf([
'A' => '/path/file1.pdf', // A is alias for file1.pdf
'B' => ['/path/file2.pdf','pass**word'], // B is alias for file2.pdf
'C' => ['/path/file3.pdf','secret**pw'], // C is alias for file3.pdf
]);
$result = $pdf->cat(1, 5, 'A') // pages 1-5 from A
->cat(3, null, 'B') // page 3 from B
->cat(7, 'end', 'B', null, 'east') // pages 7-end from B, rotated East
->cat('end',3,'A','even') // even pages 3-end in reverse order from A
->cat([2,3,7], 'C') // pages 2,3 and 7 from C
->saveAs('/path/new.pdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
$pdf = new Pdf([
'A' => '/path/file1.pdf', // A is alias for file1.pdf
'B' => '/path/file2.pdf', // B is alias for file2.pdf
]);
// new.pdf will have pages A1, B3, A2, B4, A3, B5, ...
$result = $pdf->shuffle(1, 5, 'A') // pages 1-5 from A
->shuffle(3, 8, 'B') // pages 3-8 from B
->saveAs('/path/new.pdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->burst('/path/page_%d.pdf'); // Supply a printf() pattern
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
// Set background from another PDF (first page repeated)
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->background('/path/back.pdf')
->saveAs('/path/watermarked.pdf');
if ($result === false) {
$error = $pdf->getError();
}
// Set background from another PDF (one page each)
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->multiBackground('/path/back_pages.pdf')
->saveAs('/path/watermarked.pdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
// Stamp with another PDF (first page repeated)
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->stamp('/path/overlay.pdf')
->saveAs('/path/stamped.pdf');
if ($result === false) {
$error = $pdf->getError();
}
// Stamp with another PDF (one page each)
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->multiStamp('/path/overlay_pages.pdf')
->saveAs('/path/stamped.pdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
$files = [
'/path/to/file1',
'/path/to/file2',
]
// Add files at the document level
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->attachFiles($files)
->saveAs('/path/withfiles.pdf');
if ($result === false) {
$error = $pdf->getError();
}
// Add files to a specific page
$pdf = new Pdf('/path/my.pdf');
$page = 7;
$result = $pdf->attachFiles($files, $page)
->saveAs('/path/withfiles.pdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->unpackFiles('/path/to/dir');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
// Create FDF from PDF
$pdf = new Pdf('/path/form.pdf');
$result = $pdf->generateFdfFile('/path/data.fdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
// Get data
$pdf = new Pdf('/path/my.pdf');
$data = $pdf->getData();
if ($data === false) {
$error = $pdf->getError();
}
// Get form data fields
$pdf = new Pdf('/path/my.pdf');
$data = $pdf->getDataFields();
if ($data === false) {
$error = $pdf->getError();
}
// Get data as string
echo $data;
$txt = (string) $data;
$txt = $data->__toString();
// Get data as array
$arr = (array) $data;
$arr = $data->__toArray();
$field1 = $data[0]['Field1'];
use mikehaertl\pdftk\Pdf;
// Extract pages 1-5 and 7,4,9 into a new file
$pdf = new Pdf('/path/my.pdf');
$pdf->cat(1, 5)
->cat([7, 4, 9]);
// We now use the above PDF as source file for a new PDF
$pdf2 = new Pdf($pdf);
$result = $pdf2->fillForm(['name' => 'ÄÜÖ äüö мирано čárka'])
->needAppearances()
->saveAs('/path/filled.pdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
$pdf = new Pdf('/path/my.pdf');
$result = $pdf->allow('AllFeatures') // Change permissions
->flatten() // Merge form data into document (doesn't work well with UTF-8!)
->compress($value) // Compress/Uncompress
->keepId('first') // Keep first/last Id of combined files
->dropXfa() // Drop newer XFA form from PDF
->dropXmp() // Drop newer XMP data from PDF
->needAppearances() // Make clients create appearance for form fields
->setPassword($pw) // Set owner password
->setUserPassword($pw) // Set user password
->passwordEncryption(128) // Set password encryption strength
->saveAs('new.pdf');
if ($result === false) {
$error = $pdf->getError();
}
// Example: Fill PDF form and merge form data into PDF
// Fill form with data array
$result = $pdf = new Pdf('/path/form.pdf');
$pdf->fillForm(['name' => 'My Name'])
->flatten()
->saveAs('/path/filled.pdf');
if ($result === false) {
$error = $pdf->getError();
}
// Example: Remove password from a PDF
$pdf = new Pdf;
$result = $pdf->addFile('/path/my.pdf', null, 'some**password')
->saveAs('/path/new.pdf');
if ($result === false) {
$error = $pdf->getError();
}
use mikehaertl\pdftk\Pdf;
$pdf = new Pdf('/path/my.pdf', [
'command' => '/some/other/path/to/pdftk',
// or on most Windows systems:
// 'command' => 'C:\Program Files (x86)\PDFtk\bin\pdftk.exe',
'useExec' => true, // May help on Windows systems if execution fails
]);