1

I have a HTML page that contains a table with a list of a customers orders (pulled from a database via PHP).

I want to have a button on the page that will allow the users to download a PDF copy excluding the navigation bar etc.

The examples that I've seen and tested online initially load the page in PDF format but I don't want this. I want it to load as it currently is but also have a button that when pressed downloads a PDF copy (i.e. not display it in the browser).

user2656114
  • 949
  • 4
  • 18
  • 35
  • If you say " initially load the page in PDF format but I don't want this", do you mean you want to avoid another round-trip to the server? – Frank Rem May 06 '14 at 13:37
  • @Frank I mean that with the ones that I've tested the page displays in the browser as a PDF initially. I want it to continue like normal unless the user presses a button to download a PDF version. – user2656114 May 06 '14 at 13:40

4 Answers4

1

wkhtmltopdf will do the trick:

http://wkhtmltopdf.org/

It should allow you a lot of customization options.

morganjlopes
  • 925
  • 7
  • 10
0

But if you want to generate it dynamically then you have to use some library for pdf . There are many library like tcpdf, dompdf etc. Dom pdf example

truesource
  • 397
  • 3
  • 20
0

Since you ask for the easiest way.. just print the HTML client side. This button invokes printing the web page client side:

<input type="button" 
  onClick="window.print()" 
  value="Print This Page"/>

Then the user would have to select a PDF printer driver. Chrome has this built in.

Here you can read how to exclude parts of the HTML (such as the print button itself) from being printed: How do I hide an element when printing a web page?

Community
  • 1
  • 1
Frank Rem
  • 3,632
  • 2
  • 25
  • 37
  • Thanks for the answer! I did try this but it looks nothing like the website and also includes the navigation bar. I need to wrap the content that I want in the PDF but not have the PDF generated until a button is pressed, I think. – user2656114 May 07 '14 at 10:34
0

From experience I would recommend Phantom JS - see an example

I found Phantom JS offers better HTML/CSS support when compared to wkhtmltopdf.

Bear in mind, you will struggle to get support for things like CSS3 columns in Phantom and Wkhtmltopdf.

Workflow:

  1. Land on initial page (later to be converted to PDF).
  2. Display button - 'download as PDF'
  3. Listen for the click event on this button and call a custom callback function once the event is fired.
  4. The callback function executes an Ajax call sending the full page HTML as part of the server request.
  5. On server side, fire up Phantom JS or Wkhtmltopdf to generate PDF. Save PDF on server.
  6. Generate URL for saved PDF and display/load client side.

PS; I'm not sure if Phantom JS will generate a PDF from a string or whether it expects a valid url. I know Wkhtmltopdf can generate a PDF from a string.

Hope this helps

singh1469
  • 2,024
  • 22
  • 22