如何使用pdfmake将html表转换为pdf [英] How to convert html table to pdf using pdfmake

查看:190
本文介绍了如何使用pdfmake将html表转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用pdfmake http://pdfmake.org/将我的html表转换为pdf,但我无法进行如何使用

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> 

从我发布的代码中,我的pdf文件上获取的是html代码而不是html表,对不起,不好用JavaScript?

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.使用PDFMAKE时,不能将HTML表放在文档定义对象的内容中 2.您需要做的是生成一个数组数组. 主要数组是文档定义对象中表对象中的主体数组.因此,现在明智地提取html表的每一行,并将每个单元格(行)推入数组中.在完成该行以及特定行的所有单元格数据之后被推到主体数组中,这需要对所有行都完成,就是这样! 一个简单的例子是

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>

这篇关于如何使用pdfmake将html表转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆