0

I am using an application on my windows server called PDFtK which generates pdfs and you can merge data into them etc.

I am trying to get passthru to work so I can just output the pdf to the browser without saving anything to the folder.

This works perfectly fine for creating the PDF in the folder. However, I dont want to create the PDF, I need to output it to the browser.

$WshShell = new COM("WScript.Shell");
$WshShell->exec('"pdftk" C:\\inetpub\\wwwroot\\xx\\demos\\pdf\\uploads\\xxx.pdf fill_form C:\\inetpub\\wwwroot\\xx\\demos\\pdf\\uploads\\'.$fdf_file.' output C:\\inetpub\\wwwroot\\xx\\demos\\pdf\\uploads\\'.$newPDF.' flatten'); 

Here is what I then tried:

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="download.pdf"');
$WshShell = new COM("WScript.Shell");
$WshShell->passthru('"pdftk" C:\\inetpub\\wwwroot\\xx\\demos\\pdf\\uploads\\xxx.pdf fill_form C:\\inetpub\\wwwroot\\xx\\demos\\pdf\\uploads\\'.$fdf_file.' output -'); 

I then get this error when running it:

Call to undefined method com::passthru()

Is there a way to test is passthru is working correctly?

SBB
  • 8,560
  • 30
  • 108
  • 223

1 Answers1

1

The function passthru() doesn't exist in the class COM. A possible solution would be, to write the pdf file (using COM::exec()), read it with file_get_contents('filename.pdf'), output the content and then delete it with php @unlink('filename.pdf').

Charlotte Dunois
  • 4,638
  • 2
  • 20
  • 39
  • How can I do this while forcing the browser to output the pdf as well? – SBB Jul 31 '14 at 18:59
  • `$pdf = file_get_contents('filename.pdf'); @unlink('filename.pdf'); header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="filename.pdf"'); echo $pdf;` It would even work if you would do unlink() at the end. Since PHP caches the output until the parsing is done or the buffer is full (if the value in the php.ini is not set to Off (default is 4096)). – Charlotte Dunois Jul 31 '14 at 19:01
  • I guess I am confused on the order of it though - Does that go before or after the exec? – SBB Jul 31 '14 at 19:04
  • After the exec(). Before wouldn't make any sense. PHP wouldn't find the file to read, it doesn't even exist. – Charlotte Dunois Jul 31 '14 at 19:05
  • It seems like its trying to run file_get_contents before it has finished writing the file – SBB Jul 31 '14 at 19:07
  • Well, in my case I'm using shell_exec() and PHP waits til it gets the output of shell_exec(). You maybe want to use sleep() in your case to tell the php script to wait some seconds. http://ch1.php.net/manual/en/function.sleep.php – Charlotte Dunois Jul 31 '14 at 19:09
  • Would it make sense to just keep checking if the file exists and when true then it gets the contents etc? – SBB Jul 31 '14 at 19:13
  • Not really, since PHP would only run the script once and doesn't care if the file exists after PHP finished parsing. Either you make two scripts. In the first one you're generating the pdf and in the second one you're checking if the file exists. You have to load the second script dynamically with Javascript (AJAX) and redirect to the script if the file exists. You may want to add a GET parameter to tell the script he has now to output the pdf if the file exists. – Charlotte Dunois Jul 31 '14 at 19:16