Click here to Skip to main content
15,851,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Ihave Html Table Row

Idno | Name | Image

when i want print to pdf, i can avoid image...Need only Idno and Name...How its possible?..Code i posted below

JavaScript
var myApp = new function () {
  this.printTable = function () {
     var tab = document.getElementById('mytable');
     var win = window.open('', '', 'height=700,width=700');
     win.document.write(tab.outerHTML);
     win.document.close();
     win.print();
  }
 }


What I have tried:

JavaScript
1. <input type="button" value="Print Table" onclick="myApp.printTable()" />
2. <script>
3. var myApp = new function () {
4. this.printTable = function () {
5. var tab = document.getElementById('mytable');
6. var win = window.open('', '', 'height=700,width=700');
7. win.document.write(tab.outerHTML);
8. win.document.close();
9. win.print();
10 }
11 }
12 </script>
Posted
Updated 1-Dec-23 22:08pm
v2

1 solution

Your code will never print to a PDF document as there are no references made to the PDF libraries, what it will do is in summary, your 'printTable' method opens a new browser window, copies the HTML content of your table with the 'id 'mytable'' to that window, and then prints the content of that return -

a Full explanation of what your function does -
1) It retrieves an HTML table element with the id 'mytable' using 'document.getElementById('mytable')' and stores it in your variable 'tab'.
2) It opens a new browser window using 'window.open('', '', 'height=700,width=700')'. The parameters passed to 'window.open' are used to specify the height and width of your new window.
3) It then writes the HTML content of your table 'tab.outerHTML' to the document of the newly opened window using 'win.document.write(tab.outerHTML)'.
4) It then closes the document of that new window using 'win.document.close()'.
5) It then triggers the print dialog for the new window using 'win.print()' - No PDF involved anywhere.

To use PDF for the printing purpose, look at this tutorial from start to end - How to Generate PDF File using JavaScript | Tutorial[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900