Page 1 of 1
Print contents of an iframe.
Posted: Wed Oct 04, 2017 4:49 pm
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.
Re: Print contents of an iframe.
Posted: Wed Oct 04, 2017 5:32 pm
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 .
Re: Print contents of an iframe.
Posted: Wed Oct 04, 2017 5:39 pm
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:
Re: Print contents of an iframe.
Posted: Wed Oct 04, 2017 6:29 pm
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.
Re: Print contents of an iframe.
Posted: Thu Oct 05, 2017 11:50 am
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.
Re: Print contents of an iframe.
Posted: Thu Oct 05, 2017 1:50 pm
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.