Guest User

Untitled

a guest
Oct 30th, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.14 KB | None | 0 0
  1. <html>
  2. <head>
  3.     <title>Convert Table to PDF using JavaScript</title>
  4.     <style>
  5.         table {
  6.             width: 300px;
  7.         }
  8.         table, th, td {
  9.             border: solid 1px #DDD;
  10.             border-collapse: collapse;
  11.             padding: 2px 3px;
  12.             text-align: center;
  13.         }
  14.     </style>
  15. </head>
  16. <body>
  17.     <div id="tab">
  18.         <table>
  19.             <tr>
  20.                 <th>Name</th>
  21.                     <th>Age</th>
  22.                         <th>Job</th>
  23.             </tr>
  24.             <tr>
  25.                 <td>Brian</td>
  26.                     <td>41</td>
  27.                         <td>Blogger</td>
  28.             </tr>
  29.             <tr>
  30.                 <td>Matt</td>
  31.                     <td>25</td>
  32.                         <td>Programmer</td>
  33.             </tr>
  34.             <tr>
  35.                 <td>Arun</td>
  36.                     <td>39</td>
  37.                         <td>Writter</td>
  38.             </tr>
  39.         </table>
  40.     </div>
  41.  
  42.     <p>
  43.         <input type="button" value="Create PDF"
  44.            id="btPrint" onclick="createPDF()" />
  45.     </p>
  46. </body>
  47. <script>
  48.     function createPDF() {
  49.         var sTable = document.getElementById('tab').innerHTML;
  50.  
  51.         var style = "<style>";
  52.         style = style + "table {width: 100%;font: 17px Calibri;}";
  53.         style = style + "table, th, td {border: solid 1px #DDD; border-collapse: collapse;";
  54.         style = style + "padding: 2px 3px;text-align: center;}";
  55.         style = style + "</style>";
  56.  
  57.         // CREATE A WINDOW OBJECT.
  58.         var win = window.open('', '', 'height=700,width=700');
  59.  
  60.         win.document.write('<html><head>');
  61.         win.document.write('<title>Profile</title>');   // <title> FOR PDF HEADER.
  62.         win.document.write(style);          // ADD STYLE INSIDE THE HEAD TAG.
  63.         win.document.write('</head>');
  64.         win.document.write('<body>');
  65.         win.document.write(sTable);         // THE TABLE CONTENTS INSIDE THE BODY TAG.
  66.         win.document.write('</body></html>');
  67.  
  68.         win.document.close();   // CLOSE THE CURRENT WINDOW.
  69.  
  70.         win.print();    // PRINT THE CONTENTS.
  71.     }
  72. </script>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment