{"id":13579297,"url":"https://github.com/mikehaertl/php-pdftk","last_synced_at":"2025-05-14T14:09:26.698Z","repository":{"id":18456168,"uuid":"21650297","full_name":"mikehaertl/php-pdftk","owner":"mikehaertl","description":"A PDF conversion and form utility based on pdftk","archived":false,"fork":false,"pushed_at":"2023-11-03T16:09:17.000Z","size":294,"stargazers_count":972,"open_issues_count":6,"forks_count":132,"subscribers_count":45,"default_branch":"master","last_synced_at":"2025-04-11T06:14:57.955Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikehaertl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-07-09T11:43:59.000Z","updated_at":"2025-04-09T16:07:33.000Z","dependencies_parsed_at":"2024-01-12T10:26:02.651Z","dependency_job_id":"cafd3f7e-e3f9-4687-9acc-11a372aebcd1","html_url":"https://github.com/mikehaertl/php-pdftk","commit_stats":{"total_commits":122,"total_committers":20,"mean_commits":6.1,"dds":0.2622950819672131,"last_synced_commit":"bfe3d0552ca77c1a0d2e9af980a1954259a1a41e"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikehaertl%2Fphp-pdftk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikehaertl%2Fphp-pdftk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikehaertl%2Fphp-pdftk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikehaertl%2Fphp-pdftk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikehaertl","download_url":"https://codeload.github.com/mikehaertl/php-pdftk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254160229,"owners_count":22024567,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T15:01:38.152Z","updated_at":"2025-05-14T14:09:21.689Z","avatar_url":"https://github.com/mikehaertl.png","language":"PHP","funding_links":[],"categories":["PHP","目录","PDF"],"sub_categories":["PDF"],"readme":"php-pdftk\n=========\n\n[![GitHub Tests](https://github.com/mikehaertl/php-pdftk/workflows/Tests/badge.svg)](https://github.com/mikehaertl/php-pdftk/actions)\n[![Packagist Version](https://img.shields.io/packagist/v/mikehaertl/php-pdftk?label=version)](https://packagist.org/packages/mikehaertl/php-pdftk)\n[![Packagist Downloads](https://img.shields.io/packagist/dt/mikehaertl/php-pdftk)](https://packagist.org/packages/mikehaertl/php-pdftk)\n[![GitHub license](https://img.shields.io/github/license/mikehaertl/php-pdftk)](https://github.com/mikehaertl/php-pdftk/blob/master/LICENSE)\n[![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/mikehaertl/php-pdftk)](https://packagist.org/packages/mikehaertl/php-pdftk)\n\nA PDF conversion and form utility based on pdftk.\n\n## Features\n\n*php-pdftk* brings the full power of `pdftk` to PHP - and more.\n\n * Fill forms, either from a XFDF/FDF file or from a data array (UTF-8 safe for unflattened forms, requires pdftk 2.x !)\n * Create XFDF or FDF files from PHP arrays (UTF-8 safe!)\n * Create FDF files from filled PDF forms\n * Combine pages from several PDF files into a new PDF file\n * Split a PDF into one file per page\n * Add background or overlay PDFs\n * Read out meta data about PDF and form fields\n * Set passwords and permissions\n * Remove passwords\n\n## Requirements\n\n * The `pdftk` command must be installed and working on your system\n * This library is written for pdftk 2.x versions. You should be able to use it with pdftk 1.x but not all methods will work there.\n   For details consult the man page of pdftk on your system.\n * There is a [known issue](https://github.com/mikehaertl/php-pdftk/issues/150)\n   on Ubuntu if you installed the `pdftk` package from snap. This version has\n   no permission to write to the `/tmp` directory. You can either set another\n   temporay directory as described below or use another package.  For Ubuntu\n   18.10 there's also a `pdftk-java` package available via apt which should work\n   fine. You can also install this package on Ubuntu 18.04 if you download it\n   manually. Also check [this answer](https://askubuntu.com/a/1028983/175814)\n   on askubuntu.\n\n\u003e **Note:** The pdftk version from the alternative PPA `ppa:malteworld/ppa` is\n\u003e no longer available. The author instead now points to his answer on askubuntu\n\u003e linked above.\n\n## Installation\n\nYou should use [composer](https://getcomposer.org/) to install this library.\n\n```\ncomposer require mikehaertl/php-pdftk\n```\n\n## Examples\n\n### Create instance for PDF files\n\nThere are several ways to tell the `Pdf` instance which file(s) it should use.\nSome files may also require a password or need an alias to be used as a handle\nin some operations (e.g. cat or shuffle).\n\n\u003e **Note:** In version 2.x of pdftk a handle can be one or more upper case letters.\n\n```php\n// Create an instance for a single file\n$pdf = new Pdf('/path/to/form.pdf');\n\n// Alternatively add files later. Handles are autogenerated in this case.\n$pdf = new Pdf();\n$pdf-\u003eaddFile('/path/to/file1.pdf');\n$pdf-\u003eaddFile('/path/to/file2.pdf');\n\n// Add files with own handle\n$pdf = new Pdf();\n$pdf-\u003eaddFile('/path/to/file1.pdf', 'A');\n$pdf-\u003eaddFile('/path/to/file2.pdf', 'B');\n// Add file with handle and password\n$pdf-\u003eaddFile('/path/to/file3.pdf', 'C', 'secret*password');\n\n// Shortcut to pass all files to the constructor\n$pdf = new Pdf([\n  'A' =\u003e ['/path/to/file1.pdf', 'secret*password1'],\n  'B' =\u003e ['/path/to/file2.pdf', 'secret*password2'],\n]);\n```\n\n### Operations\n\nPlease consult the `pdftk` man page for each operation to find out how each operation works\nin detail and which options are available.\n\nFor all operations you can either save the PDF locally through `saveAs($name)` or send it to the\nbrowser with `send()`. If you pass a filename to `send($name)` the client browser will open a download\ndialogue whereas without a filename it will usually display the PDF inline.\n\n**IMPORTANT: You can always only perform *one* of the following operations on a single PDF instance.\nBelow you can find a workaround if you need multiple operations.**\n\n#### Fill Form\n\nFill a PDF form with data from a PHP array or an XFDF/FDF file.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n// Fill form with data array\n$pdf = new Pdf('/full/path/to/form.pdf');\n$result = $pdf-\u003efillForm([\n        'name'=\u003e'ÄÜÖ äüö мирано čárka',\n        'nested.name' =\u003e 'valX',\n    ])\n    -\u003eneedAppearances()\n    -\u003esaveAs('filled.pdf');\n\n// Always check for errors\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Fill form from FDF\n$pdf = new Pdf('form.pdf');\n$result = $pdf-\u003efillForm('data.xfdf')\n    -\u003esaveAs('filled.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n**Note:** When filling in UTF-8 data, you should always add the `needAppearances()` option.\nThis will make sure, that the PDF reader takes care of using the right fonts for rendering,\nsomething that pdftk can't do for you. Also note that `flatten()` doesn't really work well\nif you have special characters in your data.\n\nIf you use `pdftk-java` \u003e= 3.3.0 and the embedded font does not support UTF-8\ncharacters you can also replace it with a local font:\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n// Fill form with data array\n$pdf = new Pdf('/full/path/to/form.pdf');\n$result = $pdf-\u003efillForm($data)\n    -\u003ereplacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf')\n    -\u003esaveAs('filled.pdf');\n```\n\n#### Create a XFDF/FDF file from a PHP array\n\nThis is a bonus feature that is not available from `pdftk`.\n\n```php\nuse mikehaertl\\pdftk\\XfdfFile;\nuse mikehaertl\\pdftk\\FdfFile;\n\n$xfdf = new XfdfFile(['name' =\u003e 'Jürgen мирано']);\n$xfdf-\u003esaveAs('/path/to/data.xfdf');\n\n$fdf = new FdfFile(['name' =\u003e 'Jürgen мирано']);\n$fdf-\u003esaveAs('/path/to/data.fdf');\n```\n\n#### Cat\n\nAssemble a PDF from pages from one or more PDF files.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n// Extract pages 1-5 and 7,4,9 into a new file\n$pdf = new Pdf('/path/to/my.pdf');\n$result = $pdf-\u003ecat(1, 5)\n    -\u003ecat([7, 4, 9])\n    -\u003esaveAs('/path/to/new.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Combine pages from several files\n$pdf = new Pdf([\n    'A' =\u003e '/path/file1.pdf',                 // A is alias for file1.pdf\n    'B' =\u003e ['/path/file2.pdf','pass**word'],  // B is alias for file2.pdf\n    'C' =\u003e ['/path/file3.pdf','secret**pw'],  // C is alias for file3.pdf\n]);\n$result = $pdf-\u003ecat(1, 5, 'A')                // pages 1-5 from A\n    -\u003ecat(3, null, 'B')             // page 3 from B\n    -\u003ecat(7, 'end', 'B', null, 'east') // pages 7-end from B, rotated East\n    -\u003ecat('end',3,'A','even')       // even pages 3-end in reverse order from A\n    -\u003ecat([2,3,7], 'C')             // pages 2,3 and 7 from C\n    -\u003esaveAs('/path/new.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n#### Shuffle\n\nLike `cat()` but create \"*streams*\" and fill the new PDF with one page from each\nstream at a time.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n$pdf = new Pdf([\n    'A' =\u003e '/path/file1.pdf',     // A is alias for file1.pdf\n    'B' =\u003e '/path/file2.pdf',     // B is alias for file2.pdf\n]);\n\n// new.pdf will have pages A1, B3, A2, B4, A3, B5, ...\n$result = $pdf-\u003eshuffle(1, 5, 'A')    // pages 1-5 from A\n    -\u003eshuffle(3, 8, 'B')    // pages 3-8 from B\n    -\u003esaveAs('/path/new.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n#### Burst\n\nSplit a PDF file into one file per page.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n$pdf = new Pdf('/path/my.pdf');\n$result = $pdf-\u003eburst('/path/page_%d.pdf');     // Supply a printf() pattern\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n#### Add background PDF\n\nAdd another PDF file as background.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n// Set background from another PDF (first page repeated)\n$pdf = new Pdf('/path/my.pdf');\n$result = $pdf-\u003ebackground('/path/back.pdf')\n    -\u003esaveAs('/path/watermarked.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Set background from another PDF (one page each)\n$pdf = new Pdf('/path/my.pdf');\n$result = $pdf-\u003emultiBackground('/path/back_pages.pdf')\n    -\u003esaveAs('/path/watermarked.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n#### Add overlay PDF\n\nAdd another PDF file as overlay.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n// Stamp with another PDF (first page repeated)\n$pdf = new Pdf('/path/my.pdf');\n$result = $pdf-\u003estamp('/path/overlay.pdf')\n    -\u003esaveAs('/path/stamped.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Stamp with another PDF (one page each)\n$pdf = new Pdf('/path/my.pdf');\n$result = $pdf-\u003emultiStamp('/path/overlay_pages.pdf')\n    -\u003esaveAs('/path/stamped.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n#### Attach Files\n\nAdd file attachments to the document or to a specific page.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n$files = [\n    '/path/to/file1',\n    '/path/to/file2',\n]\n\n// Add files at the document level\n$pdf = new Pdf('/path/my.pdf');\n$result = $pdf-\u003eattachFiles($files)\n    -\u003esaveAs('/path/withfiles.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Add files to a specific page\n$pdf = new Pdf('/path/my.pdf');\n$page = 7;\n$result = $pdf-\u003eattachFiles($files, $page)\n    -\u003esaveAs('/path/withfiles.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n#### Unpack Files\n\nCopy file attachments from a PDF to the given directory.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n$pdf = new Pdf('/path/my.pdf');\n$result = $pdf-\u003eunpackFiles('/path/to/dir');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n#### Generate FDF\n\nCreate a FDF file from a given filled PDF form.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n// Create FDF from PDF\n$pdf = new Pdf('/path/form.pdf');\n$result = $pdf-\u003egenerateFdfFile('/path/data.fdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n#### Get PDF data\n\nRead out metadata or form field information from a PDF file.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n// Get data\n$pdf = new Pdf('/path/my.pdf');\n$data = $pdf-\u003egetData();\nif ($data === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Get form data fields\n$pdf = new Pdf('/path/my.pdf');\n$data = $pdf-\u003egetDataFields();\nif ($data === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Get data as string\necho $data;\n$txt = (string) $data;\n$txt = $data-\u003e__toString();\n\n// Get data as array\n$arr = (array) $data;\n$arr = $data-\u003e__toArray();\n$field1 = $data[0]['Field1'];\n```\n\n#### How to perform more than one operation on a PDF\n\nAs stated above, you can only perform one of the preceeding operations on a single PDF instance.\nIf you need more than one operation you can feed one `Pdf` instance into another:\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n// Extract pages 1-5 and 7,4,9 into a new file\n$pdf = new Pdf('/path/my.pdf');\n$pdf-\u003ecat(1, 5)\n    -\u003ecat([7, 4, 9]);\n\n// We now use the above PDF as source file for a new PDF\n$pdf2 = new Pdf($pdf);\n$result = $pdf2-\u003efillForm(['name' =\u003e 'ÄÜÖ äüö мирано čárka'])\n    -\u003eneedAppearances()\n    -\u003esaveAs('/path/filled.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n### Options\n\nYou can combine the above operations with one or more of the following options.\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n$pdf = new Pdf('/path/my.pdf');\n\n$result = $pdf-\u003eallow('AllFeatures')      // Change permissions\n    -\u003eflatten()                 // Merge form data into document (doesn't work well with UTF-8!)\n    -\u003ecompress($value)          // Compress/Uncompress\n    -\u003ekeepId('first')           // Keep first/last Id of combined files\n    -\u003edropXfa()                 // Drop newer XFA form from PDF\n    -\u003edropXmp()                 // Drop newer XMP data from PDF\n    -\u003eneedAppearances()         // Make clients create appearance for form fields\n    -\u003esetPassword($pw)          // Set owner password\n    -\u003esetUserPassword($pw)      // Set user password\n    -\u003epasswordEncryption(128)   // Set password encryption strength\n    -\u003esaveAs('new.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Example: Fill PDF form and merge form data into PDF\n// Fill form with data array\n$result = $pdf = new Pdf('/path/form.pdf');\n$pdf-\u003efillForm(['name' =\u003e 'My Name'])\n    -\u003eflatten()\n    -\u003esaveAs('/path/filled.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n\n// Example: Remove password from a PDF\n$pdf = new Pdf;\n$result = $pdf-\u003eaddFile('/path/my.pdf', null, 'some**password')\n    -\u003esaveAs('/path/new.pdf');\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n```\n\n### Shell Command\n\nThe class uses [php-shellcommand](https://github.com/mikehaertl/php-shellcommand) to execute\n`pdftk`. You can pass `$options` for its `Command` class as second argument to the constructor:\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n$pdf = new Pdf('/path/my.pdf', [\n    'command' =\u003e '/some/other/path/to/pdftk',\n    // or on most Windows systems:\n    // 'command' =\u003e 'C:\\Program Files (x86)\\PDFtk\\bin\\pdftk.exe',\n    'useExec' =\u003e true,  // May help on Windows systems if execution fails\n]);\n```\n\n#### Solve issues with UTF-8 characters in filenames or infofile content\n\nIf you have files with UTF-8 encoded characters in their filename or if you\npass an infofile with such characters to `updateInfo()` you should supply the\ncorrect locale when excuting `pdftk`. You can therefore add these options:\n\n```php\n$pdf = new Pdf($file, [\n    'locale' =\u003e 'en_US.utf8',\n    'procEnv' =\u003e [\n        'LANG' =\u003e 'en_US.utf-8',\n    ],\n]);\n```\n\n\u003e **Note:** You need to ensure that the locale you set here is available on\n\u003e your system. On Linux you can check with `locale -a` which locales are\n\u003e installed. [This article](https://wiki.archlinux.org/title/locale) explains\n\u003e the concept in more detail.\n\n\n\n### Temporary File\n\nInternally a temporary file is created via [php-tmpfile](https://github.com/mikehaertl/php-tmpfile).\nYou can also access that file directly, e.g. if you neither want to send or save the\nfile but only need the binary PDF content:\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n$pdf = new Pdf('/path/my.pdf');\n$result = $pdf-\u003efillForm(['name' =\u003e 'My Name'])\n    -\u003eexecute();\nif ($result === false) {\n    $error = $pdf-\u003egetError();\n}\n$content = file_get_contents( (string) $pdf-\u003egetTmpFile() );\n```\n\nIf you have permission issues you may have to set a directory where your\n`pdftk` command can write to:\n\n```php\nuse mikehaertl\\pdftk\\Pdf;\n\n$pdf = new Pdf('/path/my.pdf');\n$pdf-\u003etempDir = '/home/john/temp';\n```\n\n## API\n\nPlease consult the source files for a full documentation of each method.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikehaertl%2Fphp-pdftk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikehaertl%2Fphp-pdftk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikehaertl%2Fphp-pdftk/lists"}