当页面中数据已满时,JSPDF 转移到新页面

JSPDF shift to new page when Data is full in page

我是 JSPDF 的新手,正在创建这样的 PDF 页面 Now Problem is that when data is full on this page it didn't produce new page. I have that github 以及堆栈溢出的其他解决方案。但没有得到解决方案。 [JSPDF 演示 link 在这里][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]);
    }

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

看到这个 fiddle : 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]);
    }

使用“startY”加1000,会自动推送到新页面

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