0

I have an order form where users place orders and after placing their orders, their is an invoice that is generated with jquery which they are asked to print. I want to be able to take only that div that contains the invoice on the page submit it to an engine or something that will now convert it to pdf, its a table structured order print out. And also how do i make only that portion available when printing so that the printout contains only the order invoice and hides all other potions of the page. Pseudocode:

    Function make_pdf($html){
    // run thought the submitred html
    // create pdf version
    // make available for download.
    }
Udo
  • 1,129
  • 3
  • 15
  • 30
  • http://code.google.com/p/wkhtmltopdf/ – Explosion Pills Feb 15 '13 at 05:36
  • 1
    i'm assuming you want to create a pdf as in output a PDF file in new tab, not just use the print function and then saving it as a file. i would suggest [FPDF](http://www.fpdf.org). this library is well tested and creates seamless pdf's. – coder101 Feb 15 '13 at 05:46

3 Answers3

0

You can use jQuery Print Element from here. When printing a div, you just have to call the plugin's function:

$('#invoiceContainer').printElement();

You can pass the div id that containts the invoice, then you can save it as pdf.

whastupduck
  • 1,156
  • 11
  • 25
  • You cannot rely on client side scripts to generate PDF. Browser may/may not contain Plugins. – vedarthk Feb 15 '13 at 05:44
  • It's a jQuery plugin not the kind of plugin that gets installed on the client's browser. It have to be incorporated into the page. – whastupduck Feb 15 '13 at 05:49
  • The plugin cannot generate the PDF file, it will just print the html element then can be saved as pdf. – whastupduck Feb 15 '13 at 05:59
0

Use the print button to call a function.

  1. Add a javascript function at the onclick event of the print button. Get the content of the receipt div ( using jquery: $('#receipt').html() or javascript document.getEmementById('invoice').innerHTML').
  2. Add this content to a hidden variable in a form. Submit the form to a dedicated php file. (<form target='_blank') You got the the post data in the dedicated php file.

  3. $html = $_POST['hiddenHtml'];

  4. $pdf->writeHTML($html, true, false, true, false, '');

Anew
  • 5,175
  • 1
  • 24
  • 36
Ajo Koshy
  • 1,205
  • 2
  • 21
  • 33
0

You will need some sort of PDF generator to run server side that you post the necessary information to. There are a few common PDF generator libraries out there, however I am not sure of their capabilities. Check out this post:

PHP PDF Generator Advice

The easiest thing to do would be to find one that supports HTML to PDF conversion and just post the html markup directly to the server via ajax etc. Otherwise without the conversion, you will have to pass the raw data from the form and template the PDF server side, while inserting the data in the relative placeholders. Then just output the PDF back to the client with the correct headers allowing the user to download the file.

eg: header('Content-type: application/pdf');

Community
  • 1
  • 1