下载HTML表PDF

问题描述 投票:1回答:2

我花了过去的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

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

谢谢你的帮助 !

javascript html jspdf export-to-pdf html-pdf
2个回答
1
投票

您还可以使用jsPDF html PlugIn,一个replacement of addHtml()。我找不到CDN的html2canvas 1.0.0-alpha.12,所以我不能够让一个代码段在这里。下面的代码为我工作。和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>

0
投票

你只是缺少background-color:white。我认为这个问题是html2canvas是给你一个PNG图像是透明的,JsPDF将其转换为JPG格式。因此,所有的透明像素转换为黑色。

总之,尽量定身,HTML,表background-colorwhite

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_.pdf');
   });
 
 });
*, html, body, table{
  background-color:white;
  width:100%;
}

#cmd{
  border:1px solid blue;
  color:white;
  background-color:lightblue;
  padding:10px;
  border-radius:5px;
  width:200px;
  text-align:center;
  cursor:pointer;
}
<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>
<div class="cmd" id="cmd">Click to Export</div>
<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>

您还可以测试你的这个答案here

© www.soinside.com 2019 - 2024. All rights reserved.