如何在PDF文档中安装jspdf autotable?

时间:2018-04-24 10:06:09

标签: jquery jspdf

我正在尝试使用jspdf autotable插件在pdf中放入一个表但由于某种原因,单元格中的文本正在切断。即使文本很长,我如何在单元格中显示整个文本,单元格应该包装文本并显示它。我怎样才能做到这一点?

function demoFromHTML(source) {
  var columns = ["PatientNumber", "PatientName", "Prescription", "MobileNumber", "Address", "DatePrescribed"]

  // Only pt supported (not mm or in)
  var doc = new jsPDF('p', 'pt');
  doc.autoTable(columns, source);
  doc.save('table.pdf');
}

2 个答案:

答案 0 :(得分:0)



sudo cat /var/lib/cloud/instances/i-05e8a76335472f27c/user-data.txt
files/user_data.tpl

document.getElementById('pdf').onclick = function() {
  var doc = new jsPDF('p', 'pt');
  var res = doc.autoTableHtmlToJson(document.getElementById('table'));
  var height = doc.internal.pageSize.height;
  doc.text("Generated PDF", 50, 50);
  doc.autoTable(res.columns, res.data, {
    startY: 200
  });
  doc.autoTable(res.columns, res.data, {
    startY: doc.autoTableEndPosY() + 50
  });
  doc.autoTable(res.columns, res.data, {
    startY: height,
    afterPageContent: function(data) {
      doc.setFontSize(20)
      doc.text("After page content", 50, height - data.settings.margin.bottom - 20);
    }
  });
  doc.save('Generated PDF.pdf');
};




答案 1 :(得分:0)

 var pdf = new jsPDF('p', 'pt', 'a4');

//Estadísticas
var tablaDatos = $('.tb-info');

var data = pdf.autoTableHtmlToJson(tablaDatos[0]);
var imgElements = tablaDatos[0].querySelectorAll('tbody img');
var images = [];

pdf.autoTable(data.columns, data.rows, {
  startY: 100,
  margin: {
    right: 20,
    left: 50
  },
  tableWidth: 500,
  styles: {
    overflow: 'linebreak',
    columnWidth: 'wrap',
    rowHeight:'wra',
    lineWidth: 1
  },
  columnStyles: {
    0: {
      columnWidth: 130
    },
    1: {
      columnWidth: 350
    },
    columnWidth: 'wrap'
  },
  rowStyles: {
    0: {rowHeight: 150}
  },

  createdCell: function(cell, opts) {
  cell.styles.cellPadding = {vertical: 50};
},
  rowHeight:'wra',
  drawCell: function(cell, opts) {
    if (opts.column.dataKey === 0 && opts.row.index < data.rows.length) {
      images.push({
        elem: imgElements[opts.row.index],
        x: cell.textPos.x,
        y: cell.textPos.y
      });
    }
  },
   addPageContent: function() {
    for (var i = 0; i < images.length; i++) {
      if (!images[i].isLoad) {
        pdf.addImage(images[i].elem, images[i].x + 20, images[i].y - 25);
        images[i].isLoad = true;
      }
    }
  }

});

使用这些脚本

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.1/jspdf.plugin.autotable.min.js"></script>


<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>