Skip to content

Not working on a standalone windows box #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mutyii opened this issue Mar 16, 2015 · 12 comments
Closed

Not working on a standalone windows box #13

mutyii opened this issue Mar 16, 2015 · 12 comments

Comments

@mutyii
Copy link

mutyii commented Mar 16, 2015

I'm using yii2 and xampp on a windows 7 home edition box and trying
to fill pdf form using your php-pdftk extension which I've installed using composer.
I've downloaded pdftk and installed it and the binaries libiconf2.dll and pdftk.exe are in C:\Program Files (x86)\PDFtk\bin\

Here is my code in controller (same as the sample you provided with your extension)

use mikehaertl\pdftk\Pdf;

public function actionIndex()
{
// Fill form with data array
$pdf = new Pdf('form.pdf');
$pdf->fillForm(array('name'=>'John Doe'))
->needAppearances()
->saveAs('filled.pdf');
}

bu it does not work. Nothing happens, no errors, absolutely nothing!
form.pdf file is in the webroot directory C:\xampp\htdocs\MyWebsite\form.pdf

It seems, my php script cannot just find the pdftk binaries. I've tryed to put
them in the vendor\bin directory of my yii2 template without any success.
I don't know where to place the pdftk binaries for the extension to find then and/or whether there
are any other configurations steps ( in other words, basic things) I have to do.

You surely cannot have tested your extension in all possible environments but if you've
tested it in a windows environment, please help me.

Thanks in advance.

@mikehaertl
Copy link
Owner

Did you check for an error message? $pdf->getError()

You can set the command path like this:

$pdf = new Pdf('form.pdf', [
    'command' => '/path/to/pdftk',
]);

@mutyii
Copy link
Author

mutyii commented Mar 17, 2015

Thanks for feedback. Among others , I've tryed this

    
   use mikehaertl\pdftk\Pdf;
   public function actionIndex()
   {
      //Since path to pdftk.exe contains empty characters, escape it with quotes otherwise windows will  
      //try to execute C:\Program with arguments Files, (x86)\PDF,  Server\bin\pdftk.exe, …
      $pdf = new Pdf('form.pdf', ['command' => '"C:\\Program Files (x86)\\PDFtk Server\\bin\\pdftk.exe"' ] );
      $e = $pdf->getError();
      echo $e; 
      /* and alternatively this:
       $pdf = new Pdf('form.pdf', ['command' => '"C:\\Program Files (x86)\\PDFtk Server\\bin\\pdftk.exe"' ] );
       $pdf->fillForm(array('name'=>'John Doe'))
             ->needAppearances()
             ->saveAs('filled.pdf');    
       $e = $pdf->getError();
       echo $e; 
      */
    }

but still without success. Nothing happens and no message is shown.
This is weird because many devolopers still program their software on windows boxes though they do deploy them on unix systems.

@mikehaertl
Copy link
Owner

You could also try echo $pdf->getCommand() to get the actualy command line string. Then try if that executes. I recommend to also study my https://github.com/mikehaertl/php-shellcommand extension, which builds the basis for this command. That's what you get back from getCommand().

Personally I never use windows. If you have to develop there I'd recommend to have a look at Vagrant or maybe even docker. WAMP/XAMP/... is a dead end (to me ;) ).

@mikehaertl
Copy link
Owner

Probably related to this: mikehaertl/php-shellcommand#1

@developez
Copy link

I have got the same problem. This is my error log:

pdftk A="C:[mypath]\my-pdf-with-a-form.pdf" generate_fdf output "data.fdf"

The point is that I copy that line to execute it in the windows command line, it works and generates the "data.fdf".

Edit: There is not spaces in my path.
Edit 2: My code

$temp_file_uri = "C:\my-template.pdf"; // Or another path with no spaces
$pdf = new mikehaertl\pdftk\Pdf($temp_file_uri)
$pdf->generateFdfFile('data.fdf');

@mikehaertl
Copy link
Owner

@developez Can you maybe help to debug this issue? As said before: I don't do any PHP development on Windows, so I can't help you on this.

What I would try:

  1. Create a very simple PHP test script (without any of my libraries)
  2. There you build a simple pdftk command string manually
  3. Execute that command with the same core code of this library that you find here and here

So simplify as much as possible until you find the point where it works / does not work anymore. Then report back...

@developez
Copy link

Ok, but one question first.

I have to do this to use your code:

require_once 'php-tmpfile/File.php';
require_once 'php-shellcommand/Command.php';    
require_once 'php-pdftk/Command.php';    
require_once 'php-pdftk/Pdf.php';

Is that correct?

Edit:
Doing:
exec ("C:\Program Files (x86)\PDFtk Server\bin\pdftk ...") works!

@mikehaertl
Copy link
Owner

I have to do this to use your code:

No - please read about the composer autoloader: https://getcomposer.org/doc/01-basic-usage.md#autoloading

Doing "exec ("C:\Program Files (x86)\PDFtk Server\bin\pdftk ...") works!

What about the other variant that uses proc_open()? And if exec() works, have you tried to use the useExec option?

$pdf = new Pdf($file, ['useExec' => true]);

@developez
Copy link

Ok, with useExec it works.

$pdf = new mikehaertl\pdftk\Pdf($filepath1, ['command' => '"C:\\Program Files (x86)\\PDFtk    Server\\bin\\pdftk.exe"', 'useExec' => true ]);
$pdf->generateFdfFile($filepath2);

That generates the fdf file.

About the composer, I am learning the tool, the point is that I did "php composer.phar install" and as it downloaded my many files that I don't need, I took the php files in that folder distribution.

@mikehaertl
Copy link
Owner

You should not touch the files in /vendor. Just leave that folder as it is (even if not every file is used). And then include the autoloder from composer into your main script. That's really all there is to do to use composer packages.

So let's conclude for now: useExec fixes the problem?

@developez
Copy link

At this point the fdf is generated, so you library comunicates well with the pdftk.

off topic:
Is there a method to see the forms fields of a pdf with you library? I need to fill one pdf but I don't know which are the name of the fields.

@mikehaertl
Copy link
Owner

Try pdftk some.pdf dump_data_fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants