javascript - 下载 PDF 格式的 HTML 表格

标签 javascript html jspdf export-to-pdf html-pdf

过去 3 天我一直在尝试解决这个问题,但一直得到相同的结果......

我有一个使用 JavaScript 完成的 HTML 表格。

HTML

<div id="tb_container">
    <table id="tb_Logbook" class="table table-striped" onload="LoadPilotLogBook()">
        <thead>
            <tr>
              <th><center>DATE</center></th>
              <th colspan="2">DEPARTURE</th>
              <th colspan="2">ARRIVAL</th>
              <th colspan="2">AIRCRAFT</th>
              <th colspan="2">SINGLE PILOT FLIGHT TIME</th>
              <th>MULTI-PILOT</th>
              <th>TOTAL TIME</th>
              <th>NAME OF</th>
              <th colspan="2">LANDINGS</th>
              <th>APPROACH</th>
              <th colspan="2">OPERATIONAL CONDITION TIME</th>
              <th colspan="4">PILOT FUNCTION TIME</th>
              <th colspan="3">FLIGHT SIM TRAINING</th>
              <th>REMARKS AND</th>
              <th colspan="2">ACTIONS</th>
            </tr>
            <tr>
              <th><center>dd/mm/yyyy</center></th>
              <th>Place</th>
              <th>Time</th>
              <th>Place</th>
              <th>Time</th>
              <th>Model</th>
              <th>Registration</th>
              <th>SE</th>
              <th>ME</th>
              <th>FLIGHT TIME</th>
              <th>OF FLIGHT</th>
              <th>Pilot in command</th>
              <th>Day</th>
              <th>Night</th>
              <th>IFR</th>
              <th>Night</th>
              <th>IFR</th>
              <th>PIC</th>
              <th>Co-Pilot</th>
              <th>Dual</th>
              <th>Instructor</th>
              <th>Date</th>
              <th>Type</th>
              <th>Time</th>
              <th>ENDORSMENTS</th>
              <th>EDIT</th>
              <th>SUPPR</th>
            </tr>
        </thead>
        <tbody>
            //AutoGenerated via JS 
        </tbody>
        </table>
    </div>

我已经包含了所有这些文件:

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
<script src="https://rawgit.com/someatoms/jsPDF-AutoTable/master/dist/jspdf.plugin.autotable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.0.10/jspdf.plugin.autotable.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>

JavaScript

    window.html2canvas = html2canvas
var doc = new jsPDF('l', 'mm', [297, 210]);
title = "Your Logbook",
source = $('#tb_container')[0];

specialElementHandlers = {

    '#bypassme': function (element, renderer) {

        return true
    }
};



margins = {
    top: 100,
    bottom: 10,
    left: 10,
    right: 10
};

$('#cmd').click(function () {

doc.addHTML($('#tb_Logbook')[0], function () {
    title,
    source,
    margins.left, 
    margins.top, { 
        'width': margins.width, 
        'elementHandlers': specialElementHandlers
    },
 doc.save('LOGBOOK_' + userLastName + '_' + userFirstName + '.pdf');
 });


});

当我点击按钮导出为 PDF 时,我得到了这个文件:

My PDF exported

我现在真的不知道怎么处理这个问题。

感谢您的帮助!

最佳答案

你也可以使用jsPDF html PlugIn , 一个 replacement of addHtml() .我找不到 html2canvas 1.0.0-alpha.12 的 CDN所以我无法在这里制作代码片段。以下代码对我有用。而且pdf内容可搜索,更清晰。不过,您可能需要调整页面大小。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/"
    crossorigin="anonymous"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script><!-- html2canvas 1.0.0-alpha.11 or higher version is needed -->
<script>
    function download() {
        let pdf = new jsPDF('l', 'pt', [1920, 640]);
        pdf.html(document.getElementById('tb_Logbook'), {
            callback: function (pdf) {
                pdf.save('test.pdf');
            }
        });
    }
</script>

关于javascript - 下载 PDF 格式的 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54540067/

相关文章:

javascript - 如何在 EXTJS 组合框中将 html 显示为文本

javascript - 在ajax过程中加载图像无法正常工作

javascript - 使用 jspdf 将 html 转换为 pdf 时,TypeError : pdf. fromHTML 不是函数

javascript - JsPDF - 无法读取未定义的属性 'charAt'

javascript - 防止在移动设备回发时调整大小

javascript - 访问加载范围内定义的函数的技巧

电子邮件收件箱中未显示 HTML 背景

html - <div> float : left not going left

python - 如何在 python-django 中水平查看元素

javascript - 如何检测jsPDF的第一页