hello ,
I have a problem......
I actually have a servlet ... and this servlet actually retrieves the values from my database and displayes them in a table. All this works fine... but now ,below this table , i want to have button saying " Print this Page" , so that when i click this button, i need an hard copy of the page..... i actually do not know how to write the code for activating the printer and printing the web page content when i click this button....could anyone please give me a clue.....

Dani AI

Generated

described a servlet that renders database rows in a table; and pointed out the quick client-side route that invokes the browser print dialog. That is the fastest way, but for reliable, readable hard copies it is best to combine a print-trigger with dedicated print styles or—when exact pagination/layout is required—a server-generated PDF.

A few practical rules that work well for servlet output:

  • Add a print stylesheet (or rules under the print media) to hide navigation, remove buttons, force readable fonts, and control page breaks.
  • If only the table should print, hide other page elements during the print job rather than removing them from the screen template.
  • Ensure any client-side rendering or AJAX population of the table completes before the print action is triggered. Browsers will present the print dialog for confirmation; ordinary pages cannot print silently.

Example print-style techniques:

@media print {
  .no-print, nav, footer { display: none !important; }
  table { width: 100%; font-size: 12pt; color: #000; }
  tr, td, th { page-break-inside: avoid; break-inside: avoid; }

  /* print only a container with id="printable" */
  body * { visibility: hidden; }
  #printable, #printable * { visibility: visible; }
  #printable { position: absolute; left: 0; top: 0; }
}

For absolute consistency (control of margins, headers, footers, multi‑page headers), generate a PDF in the servlet and stream it with Content-Type: application/pdf. Open-source Java options include Apache PDFBox. More on the browser print API and print media rules is available at MDN: Window.print and . Troubleshoot blank or missing content by checking print CSS (elements accidentally hidden), ensuring DOM readiness before printing, and reviewing browser print settings (background graphics, margins).

Recommended Answers

All 2 Replies

it's a single line of Javascript. Any decent book or tutorial should be able to tell you what it is.

Member Avatar for Member #117553

hello ,
I have a problem......
I actually have a servlet ... and this servlet actually retrieves the values from my database and displayes them in a table. All this works fine... but now ,below this table , i want to have button saying " Print this Page" , so that when i click this button, i need an hard copy of the page..... i actually do not know how to write the code for activating the printer and printing the web page content when i click this button....could anyone please give me a clue.....

Put the following on your page:

<input type="button" value="Print this page"
onclick="window.print()" />
That is all...
Good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.