Create pdf using jsPDF with formatted Table data
Asked Answered
D

6

17

I am able to generated PDF file from html table using this below script: But I am getting all the columns data are line by line.

Please help me to generate PDF file as a tabular formatted way.(with column border, margin or padding, headers ) in this script

I am used jsPDF lib script to generate a html table to PDF .

 var pdf = new jsPDF('p', 'pt', 'letter')   
    , source = $('#TableId')[0] 
    , specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        '#bypassme': function(element, renderer){           
            return true
        }
    }

    , margins = {
             top: 20,
             bottom: 20,
             left: 30,
             width: 922
         };


    pdf.fromHTML(
        source // HTML string or DOM elem ref.
        , margins.left // x coord
        , margins.top // y coord
        , {
            'width': margins.width // max width of content on PDF
            , 'elementHandlers': specialElementHandlers
        },
        function (dispose) {          
          pdf.save('Test.pdf');
        },
        margins
    )

EDIT:

I have tried this sample below function too, but I am getting just empty pdf file.

function exportTabletoPdf()
{
    var doc = new jsPDF('p','pt', 'a4', true);
    var header = [1,2,3,4];
    doc.table(10, 10, $('#test').get(0), header, {
    left:10,
    top:10,
    bottom: 10,
    width: 170,
    autoSize:false,
    printHeaders: true
    });
    doc.save('sample-file.pdf');
}
Destinydestitute answered 11/4, 2014 at 16:42 Comment(1)
My other post might help you: enter link description hereMarasco
G
2

You have to use something like -- doc.setLineWidth(2);

for line border.. Please look the following for sample code

How to set column width for generating pdf using jsPDF

Giesecke answered 30/4, 2014 at 10:35 Comment(0)
E
61

I spent a lot of time looking for a good representation of my tables, then I found this plugin (https://github.com/simonbengtsson/jsPDF-AutoTable), It works great, includes themes, rowspan, colspan, extract data from html, works with json, you can also personalize your headers and make them horizontals. The image below is an example: jsPDF-AutoTable

Enclosure answered 26/10, 2015 at 18:27 Comment(3)
This is very nice! Thanks for your contribution!Grannias
Thank you very much! It's working with this plugin now!Liebman
Very sweet plugin. Thanks for sharing.Fillmore
G
2

You have to use something like -- doc.setLineWidth(2);

for line border.. Please look the following for sample code

How to set column width for generating pdf using jsPDF

Giesecke answered 30/4, 2014 at 10:35 Comment(0)
I
2

Maybe because your header is an array of numbers instead of an array of strings how it should be. Try this basic example which works fine:

var doc    = new jsPDF();
var header = ['1', '2', '3', '4'];
var data   = [{1: '1', 2: '2', 3: '3', 4: '4'}];
var config = {
    autoSize     : false,
    printHeaders : true
}

doc.table(10, 10, data, header, config);
doc.output('dataurlnewwindow');
Ideography answered 31/8, 2023 at 18:52 Comment(0)
I
1

Export html div content including both plain text and table data using jspdf include script https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js

function download_DIVPdf(divid) {
    var pdf = new jsPDF('p', 'pt', 'letter');
    var pdf_name = 'PostMode-'+om+'.pdf';
    // source can be HTML-formatted string, or a reference
    // to an actual DOM element from which the text will be scraped.
    htmlsource = $('#'+divid)[0];

    specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        '#bypassme': function (element, renderer) {
            // true = "handled elsewhere, bypass text extraction"
            return true
        }
    };
    margins = {
        top: 80,
        bottom: 60,
        left: 40,
        width: 522
    };

    pdf.fromHTML(
    htmlsource, // HTML string or DOM elem ref.
    margins.left, // x coord
    margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
    },

    function (dispose) {                

        pdf.save(pdf_name);
    }, margins);
}
Icebreaker answered 2/5, 2017 at 7:55 Comment(1)
Plz don't forget to include script cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"Icebreaker
V
0

Try removing last argument "true" from this method:

var doc = new jsPDF('p','pt', 'a4', true);
Venous answered 14/3, 2015 at 18:1 Comment(1)
the docs says that boolean is the putOnlyusedFonts option, that only includes fonts that were used in the PDF.Beefburger
E
0
$(".gridview td").each(function () {
      var value = $(this).html();
      doc.setFontSize(8);

      if (count == 1) {
          if (height > 278) {
              doc.rect(10, inc, 24, 8);
              doc.rect(34, inc, 111, 8);
              doc.rect(145, inc, 15, 8);
              doc.rect(160, inc, 20, 8);
              doc.rect(180, inc, 23, 8);

              doc.addPage(focus);
              doc.setLineWidth(0.5);
              inc = 15;
              height = 18;
          }


          doc.rect(10, inc, 24, 8);
          doc.text(value, 11, height);         
     }
     if (count == 2) {
          doc.rect(34, inc, 111, 8);
          var splitdesc = doc.splitTextToSize(value, 100);
          doc.text(splitdesc, 35, height);
     }
     if (count == 3) {
         doc.rect(145, inc, 15, 8);
         doc.text(value, 147, height);
         qty = value;
     }
     if (count == 4) {
        doc.rect(160, inc, 20, 8);
        doc.text(value, 163, height);

        amt = value;
     }
     if (count == 5) {
        doc.rect(180, inc, 23, 8);

        tot = parseInt(qty) * parseFloat(amt);
        doc.text("" + tot, 182, height);
        count = 0;

        height = height + 8;
        netamt = netamt + parseFloat(tot);

        inc = parseInt(inc) + 8;
        doc.rect(10, inc, 24, 8);
        doc.rect(34, inc, 111, 8);
        doc.rect(145, inc, 15, 8);
        doc.rect(160, inc, 20, 8);
        doc.rect(180, inc, 23, 8);
    }
    count = count + 1;
});
Elater answered 20/5, 2015 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.