mikehaertl / php-pdftk

A PDF conversion and form utility based on pdftk
MIT License
952 stars 128 forks source link

Argument with % is escaped #298

Open giaza opened 2 years ago

giaza commented 2 years ago

Hi, thank you for your amazing work. I used your library to split a large pdf into single pages. The method burst did not worked right away because the % character in the pattern was escaped and substituted with a white space in the processOptions function at line 213 of file command.php. So the output consisted of the last page only, as each page was overwriting the older. Changing the statement from $this->addArg('output', $filename, true); to $this->addArg('output', $filename, false); solved the problem. But probably It can cause other issues?

mikehaertl commented 2 years ago

I will have a close look later. But could you give some more details? E.g. how did you call the method, what was the expected command and what did you get instead?

giaza commented 2 years ago

I have found that this problem has been mentioned before in the issues. It happens in Windows OS because the PHP function escapeshellarg escapes the % character in Windows. There are workarounds. I used this function coding a drupal module like this:

     $pdf = new Pdf( $dir . '/test.pdf');
     $result = $pdf->burst($dir . "/output/pg_%04d.pdf");  
     if ($result === false) 
     {
        $error = $pdf->getError();
        return $error;
     }
     else
     {
        $output = "Operation completed";  
     }

The % character in the pattern is escaped and turned into a space.

mikehaertl commented 2 years ago

Hmm, this is really tricky. escapeshellarg() is neccessary to not get problems with special characters. And the reason why it's replacing % with spaces on Windows is to prevent expansion of env vars (which could be a security concern).

We could add a second argument to burst() to prevent escaping if required. I somehow don't like that idea but I see no better way (except forbidding people to use PHP under Windows as it's causing so much pain in this world 😛 ).

mikehaertl commented 2 years ago

Probably related issue: #41