0

My goal is to open a PDF in a new browser tab and to fire JavaScript. I have two problems:

  • The PDF does not get displayed in Firefox. I see the PDF viewer elements, but no page is displayed and I get the message that the PDF cannot get rendered correctly.

  • In Chrome, the PDF gets displayed. However, the JavaScript does not fire.

Here comes my code:

 $file = file_get_contents('http://bililite.com/blog/blogfiles/pdf/preschool%20physical-fields.pdf');
 $js = <<<EOT
/Names << /JavaScript <</Names [ (EmbeddedJS) <<
    alert('test');
) >> ]  >> >>
EOT;

$file =  preg_replace(
            '#/Type\s*/Catalog#',
            '$0'.$js,
            $file
        );
header("Content-type: application/pdf");
echo $file;
Max
  • 832
  • 1
  • 14
  • 33
  • Possible duplicate of [Using Javascript inside a PDF](http://stackoverflow.com/questions/9219807/using-javascript-inside-a-pdf) – Terry Feb 24 '16 at 08:58
  • @Max Is it directly download on firefox ..? – Yash Feb 24 '16 at 09:01
  • No, there is no download, which is not my goal anyway. – Max Feb 24 '16 at 09:06
  • Your attempt to inject the Javascript into the PDF effectively damages it: The final cross reference section becomes incorrect. Furthermore the piece you do insert may cause trouble if there already is a **Names** entry in the catalog as their shall be only one. Last but not least the piece you inject is syntactically incorrect, in particular the `<< alert('test'); ) >>` part makes no sense. – mkl Feb 24 '16 at 09:24
  • @mkl Thank you. Do you see any way to get this working? – Max Feb 24 '16 at 09:35
  • My goal is that the pdf when it is opened in a new tab gets automatically printed and the browser print overlay shows up. – Max Feb 24 '16 at 09:40
  • *Do you see any way to get this working?* - Whenever you try to manipulate a PDF, do use a PDF library. I don't know which PDF libraries there are for PHP, but trying to patch something into PDFs without something like that is asking for trouble. – mkl Feb 24 '16 at 10:54

0 Answers0