Javascript: printing the contents of iframe on page load not working - javascript

background: i have a page in my web app with an iframe in it that displays a pdf file.
I require a browser's print preview window to appear as soon as this page in my web app loads. The print preview window will contain the contents of the iframe and print it.
Currently, i bind a function on the iframe's load event as follows:
$('#iframe').load(function () {
$("#iframe").get(0).contentWindow.print();
});
This works fine when i debug locally. However when i upload to server, the print preview screen will not automatically popup. In fact i have to first click on a button or element for it appear. Is there a way around this?
Thanks in advance..

Try this,
window.frames['#iframe'].focus();
window.frames['#iframe'].print();

Numerous methods using either jQuery or plain javascript did not fix the issue. However, after i set a 1 second delay to the print function, it worked!
setTimeout(function () {
$("#iframe").get(0).contentWindow.print();
}, 1000);
I suppose it has something to do with the server. It probably needs extra time to process the pdf in the iframe

Related

Oracle apex open file browser automatically on page load

I need to open "File Browse" item ("P50_BLOB") on page load.
I tried clicking the item with dynamic action on page load with javascript:
$('#P50_BLOB').click();
it didn't work, though it does work using the console.
I have also tried using async / wait / promise / wait for document to load.
I even tried to do it with another apex item that will make that click when it is changed, and manually changing the item is working, but on page load it's not.
This is supposed to be very simple but nothing works.
Thanks.
If you try that with the developer tools open in Chrome you will see an error:
Googling that led to this SO question:
File chooser dialog can only be shown with a user activation error while using web scraping through Javascript

Opening and printing large files in javascript

I am trying to open a local pdf file and print it.It works for files upto 5 kb,but it takes time to load for big files within 1000ms and hangs.I want it to be dynamic and need not fix the timeout .
I tried window.onload but it does not work.Also i dont want to add any onload event to the printing page.
Is there a efficient solution
var win=window.open(printUrl);
setTimeout(function() {
win.print();
win.close();
}, 1000);
Found an workaround for this.Have used PDF Javascripts and embedded a Javascript to print the file.This makes sure that the file is only printed once the page has been loaded.
I had used PDFbox to parse PDF contents and added the below:
PDActionJavaScript javascript = new PDActionJavaScript("this.print();");
Today it seems it's not possible to be notified when a PDF file is loaded in your pages.
Events DOMContentLoad and load are not triggered in iframes, embed, object or popup windows when handling PDF files.
However, there's hope for the future, the HTML5 specs says about the embed tag:
Fetching the resource must delay the load event of the element's
document.
But so far, in Chrome 38 and IE11 it's not true yet, the document load event is fired as soon as the HTML is loaded.

Open .html page within same window only once. File run locally

I have a html file that will be run locally using IE. I want it to function more like an app, it will not be published to a site, I'm only using IE to view it. The code that follows will provide basic functions to do simple calculations. I've not been able to successfully use the window.onload event to create a new window that removes the scroll bar, title bar, menu, etc without it looping. I'm not sure if an If statement or a while statement is best for testing if the page is already open to stop the loop and I'm having a hard time understanding the syntax of how to test if the window.onload already has the window open. I guess I'm looking for some guidance on setting this up or a reference easily understood by a beginner. Thank you.
I at present have two html files. The first, its only purpose in life is to trigger the second to load as I want it to show.
function openWindow()
{
window.open("CouchShifts.html", "", "status=no,toolbar=no,menubar=no,navigationbar=no,location=no,resizable=no,scrollbars=n, width=440,height=200'");
}
window.onload = openWindow()
window.close("test.html")
I had tried to incorporate this idea into the original html file without success. Any starts on how to better handle this so that when the standalone html file is double clicked from the desk top it open as specified above only once without looping?
It's not wise to check window is open or not in the same windows onLoad. You should check it rather in the event that actually opens the window.
Here are some solutions for this.
Check if window is already open window.open
https://stackoverflow.com/a/10467344/672455

close tab after printing Firefox/Chrome (content is application/pdf)

I know there are some links and answers around here but they dont fit my problem...
I have an open tab in a browser with a pdf to print and I want to close it automatically as soon as the user hits the 'OK' (print) button.
I know there are ways to use javascript onbeforeprint() and onafterprint() for this, but as you can imagine I it is impossible to call those from a pdf file :D - I see plugin for a browser as my best shot... Any ideas anybody?
why dont you set the PDF into an iframe and print it that way,
i have a work around for your print box close page issue.
<script>
$(document).ready(function(){
// timeout is used to give the browser a chance to load everything before executing the close
setTimeout(function(){ window.close();},300);
// before unload print the window, then the window closes if it was opened with window.open()
$(window).bind("beforeunload",function(){
window.print();
});
});
</script>
unless your using the browsers internal PDF viewer then, i'm at a loss.

Automatic printing PDF programmatically in IE6

I would like to be able to print a PDF document automatically when a user clicks on a print button. Currently what the way I am doing it is I render the PDF and save to the server disk and have it appear in an iframe then I tried to print the content of the iframe using javascript:print(). however what is printed is an empty html page.
I am doing this because using the norm HTML print is wrecking the layout of the webpage i am trying to print. so i'm rendering the page to a pdf format to print the webpage. i don't want the users to be able to save the pdf hence i am trying to slient print the pdf page. hence i am loading it in an iframe by changing the src in the code behind and re-rendering the page and then triggering the js script.
function printPDF(){
document.iframe_printArea.focus();
document.iframe_printArea.print();
}
I am wondering if it is possible to print a pdf document loaded in an iframe using print() or whether this is even possible. I have extensively googled on this and have yet to come up with any solutions that works for a web application. Most of the resources are devoted to C# windows app. The platform I am using is .NET C#.
First of all I'm very sorry for whom that have to deal with IE6.
There is an non-standard DOM event developed by Microsoft that fire before print. It's onbeforeprint event (docs). What you can do is hiding everything but the iframe and shrink the iframe to the window size before print. and after print reverse the document to normal statues with onafterprint event.
function window.onbeforeprint()
{
// hide other elements and shrink the iframe
}
function window.onafterprint()
{
// unde what heppened onbeforeprint
}

Categories