JSPDF : Genearte pdf from object of array and display like Lables

混江龙づ霸主 提交于 2019-12-12 04:47:15

问题


I want to generate PDF from object of array and display like this which I have attached in image. .how I can achieve this while my output is in row wise.

  htmlStr += '<table id="customers" class="table-wide">';
htmlStr += '<thead ></thead>';
htmlStr += '<tbody ></tbodyid>';
htmlStr += '</table>';
this.html = $(htmlStr);

回答1:


I think you have to do it on your own. jsPDF-Autotable is not good option for this scenario. Following is something like a scratch it's not a perfect solution. Please work on it further.

Actually we are going to create grid cards until the page width and height.

If height reaches, add new page doc.addPage().

If width reaches, add new row.

    var data = [{
      "Name": "Ronan",
      "Email": "sodales.elit@eratSed.co.uk",
      "Company": "Malesuada Malesuada Ltd"
    }, {
      "Name": "Calvin",
      "Email": "amet.nulla@Vestibulumante.ca",
      "Company": "Donec Egestas Foundation"
    }, {
      "Name": "Kane",
      "Email": "Duis.mi@consectetueradipiscingelit.net",
      "Company": "Arcu Institute"
    }, {
      "Name": "Kasper",
      "Email": "magna.Phasellus.dolor@velconvallisin.co.uk",
      "Company": "Tempor LLP"
    }];
    
    
  

     var doc = new jsPDF('p', 'pt', 'a4');
//Dimension of A4 in pts: 595 × 842

var pageWidth = 595;
var pageHeight = 842;

var pageMargin = 20;

pageWidth -= pageMargin * 2;
pageHeight -= pageMargin * 2;

var cellPadding = 10;
var cellWidth = 180;
var cellHeight = 70;
var lineHeight = 20;

var startX = pageMargin;
var startY = pageMargin;


doc.setFontSize(12);

var page = 1;

function createCard(item) {

  //cell projection
  var requiredWidth = startX + cellWidth + (cellPadding * 2);

  var requiredHeight = startY + cellHeight + (cellPadding * 2);



  if (requiredWidth <= pageWidth) {

    textWriter(item, startX + cellPadding, startY + cellPadding);

    startX = requiredWidth;
    //  startY += cellHeight + cellPadding;

  } else {


    if (requiredHeight > pageHeight) {
      doc.addPage();
      page++;
      doc.setPage(page);

      startY = pageMargin;
    } else {
      startY = requiredHeight;
    }

    startX = pageMargin;


    textWriter(item, startX + cellPadding, startY + cellPadding);

    startX = startX + cellWidth + (cellPadding * 2);
  }

}

function textWriter(item, xAxis, yAxis) {
  doc.text(item.Name, xAxis, yAxis);
  doc.text(item.Email, xAxis, yAxis + lineHeight);
  doc.text(item.Company, xAxis, yAxis + (lineHeight * 2));
}


for (var i = 0; i < data.length; i++) {
  createCard(data[i]);
}

doc.save('grid.pdf');

For reference https://jsfiddle.net/Purushoth/jodfkz59/



来源:https://stackoverflow.com/questions/42900822/jspdf-genearte-pdf-from-object-of-array-and-display-like-lables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!