Create pdf using jsPDF with formatted table data

I can generate a PDF file from the html table using this below script: But I get all the column data row by row.

Please help me generate a PDF file in tabular format (with column borders, fields or additions, headers) in this script

I am using jsPDF lib script to create html table in 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 also tried this sample function below, but I only get an 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');
}
+4
source share
5 answers

You need to use something like: doc.setLineWidth (2);

. ,

pdf jsPDF

+2

, (https://github.com/simonbengtsson/jsPDF-AutoTable), , , rowspan, colspan, html, json, . : jsPDF-AutoTable

+6

"true" :

var doc = new jsPDF('p','pt', 'a4', true);
+1
$(".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;
});
0

html div, , , jspdf 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);
}
0

Source: https://habr.com/ru/post/1536145/


All Articles