2014-09-23 38 views
0

我想知道是否有辦法導出使用jsPDF的文本對齊方式。在導出excel文件時,它更容易,因爲它接受表格的內聯css,但將表格導出爲pdf時,對齊方式全部位於左側。這是我的腳本:導出一個HTML表格爲文本對齊的PDF

<script> 
    function demoFromHTML() { 
     $(document).find('tfoot').remove(); 

     var pdf = new jsPDF('p', 'pt', 'letter'); 
     // source can be HTML-formatted string, or a reference 
     // to an actual DOM element from which the text will be scraped. 
     source = $('#table')[0]; 

     // we support special element handlers. Register them with jQuery-style 
     // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.) 
     // There is no support for any other type of selectors 
     // (class, of compound) at this time. 
     specialElementHandlers = { 
      // element with id of "bypass" - jQuery style selector 
      '#bypassme': function (element, renderer) { 
       // true = "handled elsewhere, bypass text extraction" 
       return true 
      } 
     }; 
     margins = { 
      top: 80, 
      bottom: 60, 
      left: 85, 
      width: 522 
     }; 
     // all coords and widths are in jsPDF instance's declared units 
     // 'inches' in this case 
     pdf.fromHTML(
     source, // HTML string or DOM elem ref. 
     margins.left, // x coord 
     margins.top, { // y coord 
      'width': margins.width, // max width of content on PDF 
      'elementHandlers': specialElementHandlers 
     }, 

     function (dispose) { 
      // dispose: object with X, Y of the last line add to the PDF 
      //   this allow the insertion of new lines after html 
      var name = document.getElementById("name").innerHTML; 
      pdf.save(name); 
     }, margins); 
     setTimeout("window.location.reload()",0.0000001); 

    } 
    </script> 

我的表看起來像這樣:

<div id="table"> 
    <table data-filter="#filter" class="footable" style="background-color:white;"> 
     <thead> 
      <tr> 
       <th>Employee ID</th> 
       <th>Branch Code</th> 
       <th>Client ID</th> 
       <th>Amount</th> 
       <th>Report Timestamp</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php 
      if(count($result)==0) 
      { echo "<tr><td colspan='5' style='text-align:center; font-weight:bold;'>No data available</td></tr>"; } 
      else 
      { 
      for($i=0; $i<count($result); $i++){?> 
      <tr> 
       <td align="left">1</td> 
       <td align="left">2</td> 
       <td align="left">3</td> 
       <td align="right">4</td> 
       <td align="left">5</td> 
      </tr> 

      <?php } 
      } 
      ?> 
     </tbody> 
     <tfoot> 
      <tr> 
       <td colspan="8"> 
        <div class="pagination pagination-centered"></div> 
       </td> 
      </tr> 
     </tfoot> 
    </table> 
    </div> 

回答

0

對準錶的細胞沒有被目前默認jsPDF支持。相反,您可以使用HTMLTable [1]這樣的第三方庫來執行此操作。

[1]:https://github.com/ArtBIT/jsPDF/blob/master/jspdf.plugin.htmltable.js

+0

如何實施?我只會這樣寫嗎? 'script type =「text/javascript」src =「js/jspdf.plugin.htmltable.js」>' – user3771102 2014-09-24 04:32:19

+0

在腳本中我可以看到jsPDFAPI.setStyle = function(name,value){align}我這樣做? – user3771102 2014-09-24 04:53:32