Question

Is there an easy way to get HTML2PDF to automatically open the print dialog window when outputting a pdf to the browser?

I tried setting javascript before output - Header conflict I tried after output of the PDF - No influence I tried within the PDF like TCPDF suggests (I thought HTML2PDF was built on this library) but it would not allow that due to no JS in PDF support.

Is there any other ways people know of or am I going to have to hack something with iframes or windows and declare print via that method?

Any help appreciated.

Was it helpful?

Solution

The printing depends on how you are letting the browser display the PDF. If you are letting the browser display them "normally" either a plugin (Adobe, FoxIT) or its native display of PDFs, then there is not much you can do, since you can't use JavaScript to access those.

You could use pdf.js which renders the PDF using JavaScript on a canvas which you can print, see for example Printing PDF using pdf.js

OTHER TIPS

Well, there should be a way and although I have not explored it more in depth, I hope this inspires someone else (I will have my go at it if I find some time).

As mentioned on this page http://www.fpdf.org/en/script/script36.php, it's possible to inject some javascript (to open a print dialog). Now this extension is for FPDF, and not for the HTML2PDF library.

Maybe there is, or will be an HTML2PDF extension written, but my hunch is, that it will be easier to just do it in plain PHP after document creation.

As described here, it should be straight forward:

take an existing PDF, open it in a text editor and find /Catalog and insert the boilerplate after the /Pages reference, and put in your code

(src: http://bililite.com/blog/2012/06/06/adding-javascript-to-pdf-files/)

I will update if I have more information or a proof of concept (using HTML2PDF).

Edit

I just tested the concept, and it works just fine. Like in the examples, insert this script right after /Catalog and a new line.

/Names << % the Javascript entry
  /JavaScript <<
    /Names [
      (EmbeddedJS)
      <<
        /S /JavaScript
        /JS (
          print(true);
        )
      >>
    ]
  >>
>> % end of the javascript entry

Beware though, this will only work in Adobe Reader or Acrobat Pro and may not in other PDF readers (like the preview app in OSX did not work, but the build in reader in Chrome worked fine)

EDIT 2 - Proof of concept using the HTML2PDF library

$printCommand = <<<EOF

/Type /Catalog
 /Names <<
    /JavaScript <<
      /Names [
        (EmbeddedJS)
        <<
          /S /JavaScript
          /JS (
            print(true);
          )
        >>
      ]
    >>
  >>

EOF;

// Using the output method like this, you will get
// the raw ouput back to manipulate
$bin = $html2pdf->Output('', true);

// When the /Names block shows up somewhere later in
// in PDF code, it will override your script and will do nothing.
// This is just for proof of concept, you want to use regex here
if (strpos($bin, '/Names << >>') === false) {
    $bin = str_replace('/Type /Catalog', $printCommand, $bin);
} else {
    $printCommand = str_replace('/Type /Catalog', '', $printCommand);
    $bin = str_replace('/Names << >>', $printCommand, $bin);
}

// Since we don't use the output function from HTML2PDF,
// you have to set the headers manually
header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); 
header('Pragma: public');
header('Expires: Sat, 29 Jun 1985 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Disposition: inline; filename="your-pdf-title";');

echo $bin;
exit;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top