Export a div element as a PDF in clientside

Hi guys,

When you are developing web applications sometimes you may have to export some of your DIV elements as pdfs or print them. I faced it. Search a lot in the net but did not find any good solution ubt found many options like calling to server to execute some scripts and do the thing. But that was not my case. I wanted to do it in the client side.

I found a solution. Actually this is not THE solution as this method will not directly do the thing. But using this method you will be able to get a PDF output of your html div element.

This is a simple jQuery plugin called printThis. This plugin will try to print the given div element. When we try to print, we can get a PDF output through it. Now that's what we want right?

First of all install jQuery to your page. And download the above mentioned plugin and install it as well.

Then use the following code to print the div.

 $("#mySelector").printThis({
       debug: false,                                       // show the iframe for debugging
       importCSS: true,                                // import page CSS
       printContainer: true,                          // grab outer container as well as the contents of the selector
       loadCSS: "path/to/my.css",               // path to additional css file
       pageTitle: "",                                     // add title to print page
       removeInline: false                           // remove all inline styles from print elements
 });

0 comments:

Post a Comment