Download a div in a HTML page as pdf using javascript

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?

36 thoughts on “Download a div in a HTML page as pdf using javascript”

  1. 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",

    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 –

        $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;
  2. 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.

  3. Yes, it’s possible to To capture div as PDFs in JS. You can can check the solution provided by 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
    <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("",
    {"target": "#features", "format": "pdf"}).Create();

    You need to replace the 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

  4. You can do it using jsPDF


    <div id="content">
         <h3>Hello, this is a H3 tag</h3>
        <p>A paragraph</p>
    <div id="editor"></div>
    <button id="cmd">generate PDF</button>


    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
  5. 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=""></script>
    <script type="text/javascript" src=""></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);

    Ref: pdf genration from html canvas and jspdf.

    May be this will help someone.


Leave a Comment