Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome - Save to PDF function use in a website

Chrome allows users to hit Ctrl + P and choose 'Save to PDF'.

Is it possible to have this function through an html button?

I want the user to just hit a button and go directly to the Save as PDF prompt ( where the file name is set by PHP and not automatically by Chrome ). So the user basically just hits the button and clicks on Save on the prompt window.

Is this possible? Can I skip the previous steps?

like image 899
hermann Avatar asked May 30 '12 12:05

hermann


2 Answers

Nope, it's not possible.

But, you can give the user the choice to download a HTML file output as a PDF file. There are a few libraries around, Prince is the best but expensive, so check out DOMPDF.

like image 109
alex Avatar answered Nov 19 '22 15:11

alex


By the time your user can hit ctrl+P in the browse, the PHP process that generated the page (on server) is already closed.

Besides the fact that PHP exerts no direct control over browsers. It just sends them information in the form of HTML/CSS and important in this case JavaScript.

With JavaScript you could trigger the normal print behaviour of the browser but you would have no control over it.

Another approach could be to generate the PDF on server and send it as a file to the browser. In which case the browser will either ask the user if he wants to open it or it will ask the user if he wants to download it.

I've personally used fPDF to customise the PDF invoices of various open source e-commerce software. Like for example PrestaShop.

All you really need to do is download the library from (fPDF](http://www.fpdf.org/), but seeing it in action and being able to change/adapt a working version might help you so you could look for the PDF invoice in a fresh the PrestaShop install inside the folder classes the file name is PDF.php .

like image 2
Mihai Stancu Avatar answered Nov 19 '22 16:11

Mihai Stancu