使用jspdf导出HTML表格到pdf

前端之家收集整理的这篇文章主要介绍了使用jspdf导出HTML表格到pdf前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用jspdf导出HTML表格到pdf文件。我尝试了以下代码,但它显示在pdf文件中的空白/空输出。任何建议或示例代码将是有帮助的。
`
  1. <script type="text/javascript">
  2. function demo1() {
  3. $(function () {
  4. var specialElementHandlers = {
  5. '#editor': function (element,renderer) {
  6. return true;
  7. }
  8. };
  9. $('#cmd').click(function () {
  10. var doc = new jsPDF();
  11. doc.fromHTML($('#htmlTableId').html(),15,{
  12. 'width': 170,'elementHandlers': specialElementHandlers
  13. });
  14. doc.save('sample-file.pdf');
  15. });
  16. });
  17. }
  18. </script>
  19. `

解决方法

这里是工作示例:

在头上

  1. <script type="text/javascript" src="jspdf.debug.js"></script>

脚本:

  1. <script type="text/javascript">
  2. function demoFromHTML() {
  3. var pdf = new jsPDF('p','pt','letter');
  4. // source can be HTML-formatted string,or a reference
  5. // to an actual DOM element from which the text will be scraped.
  6. source = $('#customers')[0];
  7.  
  8. // we support special element handlers. Register them with jQuery-style
  9. // ID selector for either ID or node name. ("#iAmID","div","span" etc.)
  10. // There is no support for any other type of selectors
  11. // (class,of compound) at this time.
  12. specialElementHandlers = {
  13. // element with id of "bypass" - jQuery style selector
  14. '#bypassme': function(element,renderer) {
  15. // true = "handled elsewhere,bypass text extraction"
  16. return true
  17. }
  18. };
  19. margins = {
  20. top: 80,bottom: 60,left: 40,width: 522
  21. };
  22. // all coords and widths are in jsPDF instance's declared units
  23. // 'inches' in this case
  24. pdf.fromHTML(
  25. source,// HTML string or DOM elem ref.
  26. margins.left,// x coord
  27. margins.top,{// y coord
  28. 'width': margins.width,// max width of content on PDF
  29. 'elementHandlers': specialElementHandlers
  30. },function(dispose) {
  31. // dispose: object with X,Y of the last line add to the PDF
  32. // this allow the insertion of new lines after html
  33. pdf.save('Test.pdf');
  34. },margins);
  35. }
  36. </script>

和表:

  1. <div id="customers">
  2. <table id="tab_customers" class="table table-striped" >
  3. <colgroup>
  4. <col width="20%">
  5. <col width="20%">
  6. <col width="20%">
  7. <col width="20%">
  8. </colgroup>
  9. <thead>
  10. <tr class='warning'>
  11. <th>Country</th>
  12. <th>Population</th>
  13. <th>Date</th>
  14. <th>Age</th>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. <tr>
  19. <td>Chinna</td>
  20. <td>1,363,480,000</td>
  21. <td>March 24,2014</td>
  22. <td>19.1</td>
  23. </tr>
  24. <tr>
  25. <td>India</td>
  26. <td>1,241,900,2014</td>
  27. <td>17.4</td>
  28. </tr>
  29. <tr>
  30. <td>United States</td>
  31. <td>317,746,2014</td>
  32. <td>4.44</td>
  33. </tr>
  34. <tr>
  35. <td>Indonesia</td>
  36. <td>249,866,000</td>
  37. <td>July 1,2013</td>
  38. <td>3.49</td>
  39. </tr>
  40. <tr>
  41. <td>Brazil</td>
  42. <td>201,032,714</td>
  43. <td>July 1,2013</td>
  44. <td>2.81</td>
  45. </tr>
  46. </tbody>
  47. </table>
  48. </div>

并按钮运行:

  1. <button onclick="javascript:demoFromHTML()">PDF</button>

和在线工作示例:

tabel to pdf jspdf

或尝试这样:HTML Table Export

猜你在找的HTML相关文章