2016-03-08 88 views
-1
HTML表格轉換爲PDF

我試圖用pdfmake http://pdfmake.org/到我的HTML表格轉換爲PDF格式,但我不能使它到如何使用如何使用pdfmake

<!DOCTYPE html> 
<html> 
<?php $pdffile = md5(time()).'.pdf'; ?> 
<script src="jquery-1.7.2.min.js" type="text/ecmascript"></script> 
<script src="pdfmake.min.js" type="text/javascript"></script> 
<script src="vfs_fonts.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function(e) { 
      var table = document.getElementById('table').outerHTML; 
      var table = document.getElementById('table').innerHTML; // Same response with outerHTML 
      var table = document.getElementById('table').value; // nothing is happening here 
     docDefinition = { 
      content: table 
     } 
     pdfMake.createPdf(docDefinition).download('<?php print $pdffile; ?>'); 
    }); 
</script> 
<?php 
    $con = mysqli_connect('localhost','root','','db'); 
    if($con){ 
     echo 'connected'.'<br/>'; 

     $query = "SELECT * FROM user"; 
     $query_db = mysqli_query($con,$query); 
     if(mysqli_num_rows($query_db) > 0){ 
?> 
     <table cellpadding="0" cellspacing="0" border="1" id="table"> 
      <thead> 
       <tr> 
        <th>ID</th> 
        <th>Name</th> 
        <th>Email</th> 
       </tr> 
      </thead> 
      <tbody> 
       <?php while($data = mysqli_fetch_array($query_db)): ?> 
       <tr> 
        <td><?php print $data['user_id']; ?></td> 
        <td><?php print $data['username']; ?></td> 
        <td><?php print $data['email']; ?></td> 
       </tr> 
       <?php endwhile; ?> 
      </tbody>   
     </table> 
<?php   

     }else echo 'not data'; 
    }else echo 'error'; 
?> 
</html> 

從我已經發布我得到的html代碼的代碼不是我的pdf文件的HTML表格任何出路抱歉,不擅長JavaScript?

+0

我們需要更多的信息。你能否嘗試重新解釋你所看到和期望的輸出?目前,這沒有多大意義。 – DevDonkey

+0

devdonkey當pdf創建時獲取代碼不是表 – SyntaxError

+0

我認爲這已經在這裏得到解答[http://stackoverflow.com/questions/34049956/generate-pdf-from-html-using-pdfmake-in-angularjs]( http://stackoverflow.com/questions/34049956/generate-pdf-from-html-using-pdfmake-in-angularjs) – Bogdan

回答

0

下面是正確的語法傳遞表數據: -

var docDefinition = { 
    content: [ 
{ 
    table: { 


    body: [ 
     [ 'First', 'Second', 'Third', 'The last one' ], 
     [ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ], 
     [ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ] 
    ] 
    } 
} 

] };

這將打印HTML表格而不是PDF文件上的html代碼。您需要以此格式傳遞表格數據。

+0

Bhawana,嘿你的意思是循環我的表用戶視圖後,我也應該循環它的PDF下載? – SyntaxError

+0

是的,你需要創建一個JavaScript函數來將表格數據放入所需的格式。 – Webdev

+0

你可以參考這個http://stackoverflow.com/questions/31610129/pdfmake-html-table-to-pdfmake-table – Webdev

1

1.使用PDFMAKE時,不能將HTML表格放在文檔定義對象的內容中。2.您需要做的是生成一個數組數組。 主要數組是文檔定義對象中的表格對象中的主體數組。現在,我們將明智地提取您的html表格的每一行,並將每個單元格(按行)推送到一個數組中。行完成後,所有單元格數據行被推入到body數組中。這需要爲所有行完成。就是這樣! 一個簡單的例子是

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
     <title id='title'>HTML Page setup Tutorial</title> 
     <script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.20/pdfmake.min.js'></script> 
     <script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.20/vfs_fonts.js'></script> 
     <script type="text/javascript"> 

    function myFunction() 
    { 


     var docDefinition = { 
    content: [ 
{ 
    table: { 


    body: [ 
     [ 'First', 'Second', 'Third', 'The last one' ], 
     [ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ], 
     [ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ] 
    ] 
    } 
}]} 
    pdfMake.createPdf(docDefinition).download('Report.pdf'); 

    } 
    </script> 
    </head> 
<body> 

<button type="button" onclick="myFunction()">Click Me!</button> 
</body> 
</html>