How to convert html table to pdf using pdfmake

后端 未结 2 1220
旧巷少年郎
旧巷少年郎 2020-12-22 12:53

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





        
相关标签:
2条回答
  • 2020-12-22 13:32

    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 讨论(0)
  • 2020-12-22 13:35

    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>
    
    0 讨论(0)
提交回复
热议问题