javascript - How to set column width for generating pdf using jsPDF -
i got struck last 2 days, set column width generating pdf using jspdf.
i able generate pdf using jspdf lib html table, getting problem of overlapping columns. because want display 20 columns in sheet. have changed options portrait landscape , set width 5000 in exporting script options html table width.
please me, set pdf column width jspdf.
i had same problem 4 days , able fix it.. have provided sample code below understand..
i assuming requirement export html table (mentioned below table1, has 3 or more rows) pdf format. setting cell/column width , additionally font , font types 3+ rows..
first row header - bold applied.. have applied different styles 2nd , 3rd rows understanding styles available.. if want apply different column width, each of columns - can well..
hope following code readable , self explanatory. let me know if have questions
$(document).on("click", "#btnexporttopdf", function () { var table1 = tabletojson($('#table1').get(0)), cellwidth = 35, rowcount = 0, cellcontents, leftmargin = 2, topmargin = 12, topmargintable = 55, headerrowheight = 13, rowheight = 9, l = { orientation: 'l', unit: 'mm', format: 'a3', compress: true, fontsize: 8, lineheight: 1, autosize: false, printheaders: true }; var doc = new jspdf(l, '', '', ''); doc.setproperties({ title: 'test pdf document', subject: 'this subject', author: 'author', keywords: 'generated, javascript, web 2.0, ajax', creator: 'author' }); doc.cellinitialize(); $.each(table1, function (i, row) { rowcount++; $.each(row, function (j, cellcontent) { if (rowcount == 1) { doc.margins = 1; doc.setfont("helvetica"); doc.setfonttype("bold"); doc.setfontsize(9); doc.cell(leftmargin, topmargin, cellwidth, headerrowheight, cellcontent, i) } else if (rowcount == 2) { doc.margins = 1; doc.setfont("times "); doc.setfonttype("italic"); // or normal font type use ------ doc.setfonttype("normal"); doc.setfontsize(8); doc.cell(leftmargin, topmargin, cellwidth, rowheight, cellcontent, i); } else { doc.margins = 1; doc.setfont("courier "); doc.setfonttype("bolditalic "); doc.setfontsize(6.5); doc.cell(leftmargin, topmargin, cellwidth, rowheight, cellcontent, i); // 1st=left margin 2nd parameter=top margin, 3rd=row cell width 4th=row height } }) }) doc.save('sample report.pdf'); }) function tabletojson(table) { var data = []; // first row needs headers var headers = []; (var i=0; i<table.rows[0].cells.length; i++) { headers[i] = table.rows[0].cells[i].innerhtml.tolowercase().replace(/ /gi,''); } // go through cells (var i=1; i<table.rows.length; i++) { var tablerow = table.rows[i]; var rowdata = {}; (var j=0; j<tablerow.cells.length; j++) { rowdata[ headers[j] ] = tablerow.cells[j].innerhtml; } data.push(rowdata); } return data; }
Comments
Post a Comment