Add print button to document viewer

Arun Kumar D
Tera Contributor

Hello Everyone,

Have you ever implemented a print button to document viewer? If yes, can you let me know the steps which you've performed.

 

Thanks

Arun

1 REPLY 1

shantanu_patel8
Mega Guru

Hi @Arun Kumar D ,

 

I have found some solutions from other questions raised in community.

 

You can refer to solution provided by @Ravi Gaurav 

 

To add a print button to a document viewer in ServiceNow instead of a download option, you can follow these general steps. The exact implementation may vary based on your specific ServiceNow instance and the way your document viewer is set up. Here’s a basic outline of how you can achieve this:

 

Steps to Add a Print Button in ServiceNow Document Viewer

  1. Identify the Document Viewer: Determine where the document viewer is located in your ServiceNow instance. This could be a UI page, a widget, or a specific form.

  2. Modify the UI Page or Widget:

    • If you are using a UI page, navigate to System UI > UI Pages and find the relevant page.
    • If you are using a widget, go to Service Portal > Widgets and locate the widget that contains the document viewer.
  3. Add a Print Button:

    • In the HTML section of the UI page or widget, add a button element for printing. For example:
    • <button id="printButton" onclick="printDocument()">Print Document</button>
  4. Implement the Print Functionality:

    • You will need to create a JavaScript function that handles the print action. This function can be added in the script section of the UI page or widget. Here’s a simple example:
    • function printDocument() {
      // Assuming the document viewer is in an iframe or a specific div
      var printContent = document.getElementById('documentViewerId'); // Replace with your document viewer's ID
      var WinPrint = window.open('', '', 'width=900,height=650');
      WinPrint.document.write('<html><head><title>Print Document</title>');
      WinPrint.document.write('</head><body >');
      WinPrint.document.write(printContent.innerHTML); // Get the content to print
      WinPrint.document.write('</body></html>');
      WinPrint.document.close();
      WinPrint.print();
      }

Please mark the answer helpful and correct if it resolves the issue. Happy scripting 🙂

 

-Shantanu