页面

时间:2017-03-21 18:02:37

标签: javascript jspdf jspdf-autotable

我是JSPDF的新手,并创建了一个像这样的Lable view of data PDF页面现在问题是,当这个页面上的数据已满时,它不会产生新的页面。我有github和堆栈溢出的其他解决方案。但没有得到解决方案。 [JSPDF演示链接在这里] [3]

[3]:> https://jsfiddle.net/jodfkz59/7/

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 y=500;
    var pageMargin = 10;
    
    pageWidth -= pageMargin * 2;
    pageHeight -= pageMargin * 2;
    
    var cellMargin = 5;
    var cellWidth = 250;
    var cellHeight = 60;
    
    var startX = pageMargin;
    var startY = pageMargin;
    
    function createCard(item) {
    
      // doc.getTextDimensions(item.Name); turncate or split string if you needed
      
    if (y >= pageHeight)
{
  doc.addPage();
  y = 0 // Restart height position
}
    
      doc.text(item.Name, startX, startY);
      doc.text(item.Email, startX, startY + 20);
      doc.text(item.Company, startX, startY + 40);
    
      var nextPosX = startX + cellWidth + cellMargin;
    
      if (nextPosX > pageWidth) {
        startX = pageMargin;
        startY += cellHeight;
      } else {
        startX = nextPosX;
      }
    
    }
    
    
    for (var i = 0; i < data.length; i++) {
      createCard(data[i]);
    }

2 个答案:

答案 0 :(得分:3)

问题在于函数createCard中的条件。您必须更改y偏移的实际当前值的测试。

看到这个小提琴:https://jsfiddle.net/jodfkz59/8/

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 y=500;
    var pageMargin = 10;

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

    var cellMargin = 5;
    var cellWidth = 250;
    var cellHeight = 60;

    var startX = pageMargin;
    var startY = pageMargin;

    function createCard(item) {

      // doc.getTextDimensions(item.Name); turncate or split string if you needed

    if (startY >= pageHeight)
{
  doc.addPage();
  startY = pageMargin  // Restart height position
}

      doc.text(item.Name, startX, startY);
      doc.text(item.Email, startX, startY + 20);
      doc.text(item.Company, startX, startY + 40);

      var nextPosX = startX + cellWidth + cellMargin;

      if (nextPosX > pageWidth) {
        startX = pageMargin;
        startY += cellHeight;
      } else {
        startX = nextPosX;
      }

    }


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

答案 1 :(得分:0)

使用“ startY”并添加1000,它将自动推送到新页面

doc.autoTable({html:"#exportfrontpageTable",startY:doc.autoTable.previous.finalY + 1115});