最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert html table to pdf using pdfmake - Stack Overflow

programmeradmin1浏览0评论

Am trying to convert my html table to pdf using pdfmake / 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?

Am trying to convert my html table to pdf using pdfmake http://pdfmake/ 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?

Share Improve this question edited Mar 8, 2016 at 9:19 SyntaxError asked Mar 8, 2016 at 9:07 SyntaxErrorSyntaxError 372 gold badges3 silver badges10 bronze badges 3
  • we need way more info than that. Can you please try to re-explain what output you're seeing and expecting? At the moment, it doesnt make much sense. – DevDonkey Commented Mar 8, 2016 at 9:11
  • devdonkey when the pdf is created am getting code not table – SyntaxError Commented Mar 8, 2016 at 9:15
  • I think this was already answered here http://stackoverflow./questions/34049956/generate-pdf-from-html-using-pdfmake-in-angularjs – Bogdan Commented May 18, 2017 at 14:55
Add a ment  | 

2 Answers 2

Reset to default 4

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./ajax/libs/pdfmake/0.1.20/pdfmake.min.js'></script>
        <script src='https://cdnjs.cloudflare./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>

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.

发布评论

评论列表(0)

  1. 暂无评论