How to convert html table to pdf using pdfmake

◇◆丶佛笑我妖孽 提交于 2019-12-25 18:39:08

问题


Am trying to convert my html table to pdf using pdfmake http://pdfmake.org/ but i cant make it to how to use

<!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> 

From the code i have posted am getting html code not html table on my pdf file any way out sorry am not good at javascript?


回答1:


1.You cannot put HTML table in content of document definition object when using PDFMAKE 2.What you need to do is generate an array of arrays. The main array is body array in table object in document definition object.So now extract each row of your html table wise and push each cell (row-wise) into an array..after the row is finished and all cell data of particular row is pushed into the body array.This needs to be done for all rows.That's it! A simple example is

<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>



回答2:


Below is the correct syntax to pass the table data :-

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' ]
    ]
  }
}

] };

This will print the HTML table and not the html code on the pdf file.You need to pass the table data in this format.



来源:https://stackoverflow.com/questions/35863144/how-to-convert-html-table-to-pdf-using-pdfmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!