Reputation: 83
I can't execute pdftk in php. As in documentation, it is just mentioned as follow to saveas.
use mikehaertl\pdftk\Pdf;
$pdf = new Pdf('/path/my.pdf');
$pdf->allow('AllFeatures') // Change permissions
->compress($value) // Compress/Uncompress
->saveAs('new.pdf');
But my code doesn't give any output. I try to debug like
// Check for errors
if (!$pdf->saveAs('new.pdf')) {
$error = $pdf->getError();
}
No message. Later I found, Pdftk hasn't been executed
public function saveAs($name)
{
if (!$this->getCommand()->getExecuted() && !$this->execute()) {
echo 'test'; // it is printing
return false;
}
}
What does it mean ? I have installed from composer. When reading github issue, developer's reply likely my problem. is You need to set the path to your pdftk command, see https://github.com/mikehaertl/php-pdftk#execution
I can't clear about how to set path, it is not highlighted in documentation. Only mentioned here
The class uses php-shellcommand to execute pdftk. You can pass $options for its Command class as second argument to the constructor: As example.
$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
]);
I am using xampp. Regarding pdftk, I have a directory within project which was created from composer. phpand what is this path ? this project can be moved in linux or window server ? Do I need any third party software installation for this ?
Upvotes: 0
Views: 5929
Reputation: 537
You need to install pdftk from here - pdftk official. After Installing if you are on windows make sure that your system environment variables are set to execute pdftk, you can test that by simply running in your command prompt
pdftk --version
If this works in your command prompt that means you have the path correct. Secondly if you are unable to set the environment variable in your windows, you can just directly pass the path of pdftk.exe
from your program files in your code like this:
$pdf = new Pdf('/path/my.pdf', [
'command' => 'C:\Program Files (x86)\PDFtk\bin\pdftk.exe',
// or on most Windows systems:
// 'command' => 'C:\Program Files (x86)\PDFtk\bin\pdftk.exe',
'useExec' => true, // May help on Windows systems if execution fails
]);
You also need to figure out any issues regarding permissions, make sure that php have permission to execute scripts in your windows and it can write to the file system.
Upvotes: 2