Print contents of an iframe.

Use this board to ask questions or have discussions with other Genie users.
Post Reply
Wayne C.
Experienced User
Posts: 139
Joined: Mon Aug 16, 2010 12:04 pm
First Name: Wayne
Last Name: Colasinski
Company Name: Sofworx / Tantara Transport
Contact:

Print contents of an iframe.

Post by Wayne C. »

We have an application (our Driver Master file maintenance) where we are uploading various documents. We've added a module where a user can display any one of a driver's documents in an iframe. This is working. Now we would like the ability to print the document (the contents of the iframe). I've added a button in what I guess you would call the parent window (the button is not in the iframe) and tried various scripts found online and always seem to get an error. The id of the iframe is "IFrame1".

window.top.document.getElementById("IFrame1").contentWindow.focus();
window.top.document.getElementById("IFrame1").contentWindow.print();

document.IFrame1.focus();
document.IFrame1.print();

window.frames[IFrame1].focus();
window.frames[IFrame1].print();

message: "Unable to get property "focus" of undefined or null reference"

window.frames["IFrame1"].focus();
window.frames["IFrame1"].print();

message: "Object doesn't support property or method 'print'

I've tried these (and a few more) in Edge, Chrome and Firefox without success. I'll keep plugging away but any help would be appreciated. Thanx.
User avatar
matt.denninghoff
Profound Logic Staff Member
Posts: 115
Joined: Wed Feb 10, 2016 3:53 pm
First Name: Matthew
Last Name: Denninghoff
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Print contents of an iframe.

Post by matt.denninghoff »

Do the parent page and child iframes have different origins--i.e. are either the port, protocol, or domain names different? If so, then via JavaScript you cannot access the child frame's DOM from the parent and vice versa. There are complicated ways to communicate across different origins, such as postMessage: https://developer.mozilla.org/en-US/doc ... ostMessage .
User avatar
matt.denninghoff
Profound Logic Staff Member
Posts: 115
Joined: Wed Feb 10, 2016 3:53 pm
First Name: Matthew
Last Name: Denninghoff
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Print contents of an iframe.

Post by matt.denninghoff »

Also, this is the code I would use to address an iframe tag whose "id" attribute is "IFrame1":

Code: Select all

document.getElementById("IFrame1")
If the parent page is in Genie or a Rich Display File application, the you can also use the Profound UI API function, getObj, which is just a shorthand for document.getElementById:

Code: Select all

getObj("IFrame1")
Wayne C.
Experienced User
Posts: 139
Joined: Mon Aug 16, 2010 12:04 pm
First Name: Wayne
Last Name: Colasinski
Company Name: Sofworx / Tantara Transport
Contact:

Re: Print contents of an iframe.

Post by Wayne C. »

I would say no to your question. Using the Genie Upload widget, we're uploading scanned paper documents to the following directory:

profoundui/htdocs/profoundui/userdata/images/RDOC/(driver's id)/

I was just thinking... I don't necessarily have to print the contents of the/an iframe. I just have to print that document and was hoping to avoid doing it in RPG. I thought I could do it in javascript. In my iframe, the URL directory is "js: get('D_2_1') + get('D_3_1');" which is the directory + the file name. So I have the necessary info on hand. All I need is when that "Print" button is clicked, that the document is retrieved and printed. In the easiest and simplest way possible ;)

I don't know if that makes the task any easier. I'm open to suggestions.
User avatar
matt.denninghoff
Profound Logic Staff Member
Posts: 115
Joined: Wed Feb 10, 2016 3:53 pm
First Name: Matthew
Last Name: Denninghoff
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Print contents of an iframe.

Post by matt.denninghoff »

You do not need to load the contents of an iframe to print one of those images. Instead, you could simply setup a button to open the image in a new window that would prompt to print then automatically close. I tried suggestions from this page, https://stackoverflow.com/questions/290 ... int-images , and I simplified a solution.

You would need to define a JavaScript function somewhere, such as in your Genie skin's custom.js file.

Code: Select all

function imagePopupPrint(imgsrc){
  var popup = window.open();
  var docstr = '<!doctype html><html><head><meta charset="utf-8">'
   + '<script type="text/javascript">function printme(){ window.print(); window.close(); }</scr'+'ipt>'
   +'</head><body ><img src="'+ imgsrc + '" onload="printme()" />'
   + '</body></html>';
  popup.document.write(docstr );
}
Then on your customized screens, you could add buttons to call that function. In the onclick property of a button widget, you'd use:

Code: Select all

imagePopupPrint( get('D_2_1') + get('D_3_1') );
I tried the code in Firefox, Chrome, Edge, and Internet Explorer 11 successfully.
Wayne C.
Experienced User
Posts: 139
Joined: Mon Aug 16, 2010 12:04 pm
First Name: Wayne
Last Name: Colasinski
Company Name: Sofworx / Tantara Transport
Contact:

Re: Print contents of an iframe.

Post by Wayne C. »

Thanks Matt. I'm going to give that a whirl.

I was experimenting this morning using a pop-up window. I'm already doing it elsewhere in our system and discovered at the very least that I could right-click on the pop-up and select "Print" to print the complete image or document (which I now know you can't do on an iframe). And any document saved as a PDF has the menu bar to select print.

I like your way because it seems to work uniformly across the different types of documents. Thanks again for the help and the lesson.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests