I have a content div with the id as "content". In the content div I have some graphs and some tables. I want to download that div as a pdf when user click on download button. Is there a way to do that using javascript or jQuery?
-
1http://code.google.com/p/jspdf/ is the best library i know of which can do this. – alvinarul Jun 25 '13 at 09:05
-
How can I use jsPDF for downloading a content in specific div? from what i see in the examples in their site it cant be done. – Midhun Mathew Jun 25 '13 at 09:12
5 Answers
You can do it using jsPDF
HTML:
<div id="content">
<h3>Hello, this is a H3 tag</h3>
<p>A paragraph</p>
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>
JavaScript:
var doc = new jsPDF();
var specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
$('#cmd').click(function () {
doc.fromHTML($('#content').html(), 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
doc.save('sample-file.pdf');
});

- 66,969
- 9
- 47
- 59

- 11,959
- 14
- 50
- 79
-
29I have a problem here. The css I have added in here is not getting applied in the pdf file. Also I have some graphs too..those r not coming in the pdf. What should I do to get those? – Midhun Mathew Jun 26 '13 at 06:00
-
18jsPDF doesn't format tables properly either, it just interprets td/th as display:block; – Dave Sep 12 '13 at 17:23
-
3Using the above code we can able to create a pdf only one time. But for the second time it is not working. To make it work, check this [answer](http://stackoverflow.com/a/19786792/3049065) by @kristof-feys, We need to declare the `doc` variable inside the click function. – Mahendran Sakkarai Dec 19 '14 at 08:14
-
Doesn't keep the same structure as `UL` to have an example... http://jsfiddle.net/5ud8jkvf/157/ – SearchForKnowledge Jan 08 '15 at 19:58
-
5
-
I like it. But I would suggest using a recursive solution to apply a css class to every element in the dom such as .printmode, and then you can do all your styling changes in the css (vs elementHandlers) – Jack Franzen Apr 27 '15 at 02:34
-
1
-
@skafandri I couldn't make pdf of graphs, here is my demo http://jsfiddle.net/tvshajeer/h5pcqz4u/ – tvshajeer Aug 17 '15 at 10:29
-
Is there a way to resolve the issue with the CSS not being rendered?? The same goes for JS code that is set to run in the page and affects the save content, for that matter... – TheCuBeMan Jan 20 '16 at 15:04
-
@skafandri This is working fine to convert only but if we talk about styling there is not a single property supported jsfiddle.net/5ud8jkvf/4607 – Gaurav Aggarwal May 06 '16 at 06:58
-
Can you name files required for this demo? I downloaded the plugin but it has lots of js files. – Half Blood Prince May 26 '16 at 06:29
-
Hi, While trying above codes i am getting an error "ERROR: jsPDF Warning: rendering issues? provide a callback to fromHTML!" Could anyone please tell me what the error is about? – Arj 1411 Aug 17 '16 at 12:55
-
1jsPdf is useless as it doesn't render css and you can't make it look like the page at all. I have just given up on it and I am looking for a different solution – Thomas Williams Nov 14 '16 at 19:28
-
-
-
this gives me a rendering erro in the console : `jspdf.debug.js:7599 jsPDF Warning: rendering issues? provide a callback to fromHTML!` – Beingnin Dec 22 '17 at 06:52
-
2I gave my days to work with jsPDF, and in the end what I found is, it is not rendering the CSS. My table looks too bad, not able to add any image properly. jsPDF is good for nothing. – Agent K Dec 17 '20 at 06:23
-
-
There is a big problem, the jquery code is visible(printed) to downloded pdf. – Muhammad Ali May 16 '21 at 11:56
-
I have this error TypeError: Cannot read property 'cells' of undefined at tableToJson – GSandro_Strongs Jul 27 '21 at 16:14
Content inside a <div class='html-content'>....</div>
can be downloaded as pdf with styles using jspdf & html2canvas.
You need to refer both js libraries,
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
Then call below function,
//Create PDf from HTML...
function CreatePDFfromHTML() {
var HTML_Width = $(".html-content").width();
var HTML_Height = $(".html-content").height();
var top_left_margin = 15;
var PDF_Width = HTML_Width + (top_left_margin * 2);
var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
var canvas_image_width = HTML_Width;
var canvas_image_height = HTML_Height;
var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;
html2canvas($(".html-content")[0]).then(function (canvas) {
var imgData = canvas.toDataURL("image/jpeg", 1.0);
var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
for (var i = 1; i <= totalPDFPages; i++) {
pdf.addPage(PDF_Width, PDF_Height);
pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4),canvas_image_width,canvas_image_height);
}
pdf.save("Your_PDF_Name.pdf");
$(".html-content").hide();
});
}
Ref: pdf genration from html canvas and jspdf.
May be this will help someone.

- 817
- 14
- 20
-
9This worked great for me, thanks. It preserves all the styling but it does have some limitations (creates the PDF as an image so it's not searchable; seems to also be slightly blurry). – jramm Sep 30 '19 at 18:32
-
how to set page break if half of content fails to fit on one page? – SHEKHAR SHETE Mar 05 '20 at 09:06
-
1Worked very fine to me. The only problem was that some of my SVG icons were not rendered! – Felipe N Moura Mar 28 '20 at 03:25
-
1Amazing! This worked beautifully for me. In my case, I just needed to export a div that contained a few tables and graphs. Thanks so much! – Andrew Mar 03 '22 at 05:09
Your solution requires some ajax method to pass the html to a back-end server that has a html to pdf facility and then returning the pdf output generated back to the browser.
First setting up the client side code, we will setup the jquery code as
var options = {
"url": "/pdf/generate/convert_to_pdf.php",
"data": "data=" + $("#content").html(),
"type": "post",
}
$.ajax(options)
Then intercept the data from the html2pdf generation script (somewhere from the internet).
convert_to_pdf.php
(given as url in JQUERY code) looks like this -
<?php
$html = $_POST['data'];
$pdf = html2pdf($html);
header("Content-Type: application/pdf"); //check this is the proper header for pdf
header("Content-Disposition: attachment; filename='some.pdf';");
echo $pdf;
?>

- 670
- 6
- 18

- 13,433
- 7
- 27
- 51
-
-
-
You would need a slightly different url so you can differentiate between the two content – DevZer0 Jun 26 '13 at 05:56
-
-
2You'll be screwed if the CSS is linked though because your back-end program won't know where to find the styles unless it can grab the CSS file and apply the styles from within the file. – TheRealChx101 Apr 14 '16 at 03:31
-
This solution doesn't take care of any style rules applied to the html. – Dario Corbelli Oct 21 '21 at 09:05
AFAIK there is no native jquery function that does this. Best option would be to process the conversion on the server. How you do this depends on what language you are using (.net, php etc.). You can pass the content of the div to the function that handles the conversion, which would return a pdf to the user.

- 6,272
- 6
- 34
- 59
-
hi dear friend, pay attention to question... Download a div in a HTML page as pdf using javascript – R.Akhlaghi Nov 16 '22 at 05:39
Yes, it's possible to To capture div as PDFs in JS. You can can check the solution provided by https://grabz.it. They have nice and clean JavaScript API which will allow you to capture the content of a single HTML element such as a div or a span.
So, yo use it you will need and app+key and the free SDK. The usage of it is as following:
Let's say you have a HTML:
<div id="features">
<h4>Acme Camera</h4>
<label>Price</label>$399<br />
<label>Rating</label>4.5 out of 5
</div>
<p>Cras ut velit sed purus porttitor aliquam. Nulla tristique magna ac libero tempor, ac vestibulum felisvulput ate. Nam ut velit eget
risus porttitor tristique at ac diam. Sed nisi risus, rutrum a metus suscipit, euismod tristique nulla. Etiam venenatis rutrum risus at
blandit. In hac habitasse platea dictumst. Suspendisse potenti. Phasellus eget vehicula felis.</p>
To capture what is under the features id you will need to:
//add the sdk
<script type="text/javascript" src="grabzit.min.js"></script>
<script type="text/javascript">
//login with your key and secret.
GrabzIt("KEY", "SECRET").ConvertURL("http://www.example.com/my-page.html",
{"target": "#features", "format": "pdf"}).Create();
</script>
You need to replace the http://www.example.com/my-page.html
with your target url and #feature
per your CSS selector.
That's all. Now, when the page is loaded an image screenshot will now be created in the same location as the script tag, which will contain all of the contents of the features div and nothing else.
The are other configuration and customization you can do to the div-screenshot mechanism, please check them out here

- 14,397
- 15
- 77
- 118
-
4I think you only get 1,000 free captures and 100 free "scrapes" with grabz.it. So not a permanent solution unless you're willing to pay for it. – BrettC Mar 01 '18 at 16:18