iframe print not working in Chrome when url starts with https


Raj
  1. I need to print the page without using window.open and window.print functions.

  2. I achieved the same using the code below. This works if the url doesn't start with https (at least what I thought). If the url starts with https, chrome prints out a blank page. Sometimes, clicking multiple times to print a link will display page data. This code works fine in IE and FireFox.

I uploaded a sample test page with this code below the url.

http://mediateqindia.com/testPrint.html (works fine)

https://mediateqindia.com/testPrint.html (click on the blank page on the print link)

Both links point to the same page.

How can I fix this to start using https in chrome?

The code on the testPrint.html page is given below.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bindddddddddd</title>
</head>
<body>
<div id="testPrintDiv" >
Hiiiiiiiiiiiiiii
<br /><br />
Hellooooooooooooo
<br /><br />
World !!!!!!!!!!!!!!!!!!!!!!!!!!!s
<a href="javascript:printTest();">Print</a>
</div>
</body>
</html>



<script>
function printTest(){
var scale=1;
var contents=document.getElementById("testPrintDiv").innerHTML;
if(!scale){ 
    scale=".9";
}
    var printframe = document.createElement('iframe');
    printframe.name = "printframe";
    printframe.style.position = "absolute";
    printframe.style.top = "-1000000px";
    document.body.appendChild(printframe);
    var frameDoc = printframe.contentWindow ? printframe.contentWindow : printframe.contentDocument.document ? printframe.contentDocument.document : printframe.contentDocument;
    frameDoc.document.open();
    frameDoc.document.write('<meta http-equiv="X-UA-Compatible" content="IE=edge" /><html><head><title></title>');
    frameDoc.document.write('<link rel="stylesheet" type="text/css" href="/user/data/css/styles-min.css">');
    frameDoc.document.write('<style type="text/css">@media print { body {transform: scale('+scale+');} @page{margin-left: 0cm;} body {margin-left:0;padding:0;}'
                +'</style></head><body>');
       frameDoc.document.write(contents);
        frameDoc.document.write('</body></html>');
       frameDoc.document.close();

       setTimeout(function () {
            window.frames["printframe"].focus(); 
            window.frames["printframe"].print();       
             setTimeout(function () {
                document.body.removeChild(printframe);
             },5000);
        }, 750);

    //return false;     
}

</script>
Raj

The problem is the loading time of the css file.

When commenting the line below, everything started working fine.

frameDoc.document.write('<link rel="stylesheet" type="text/css"
href="/user/data/css/styles-min.css">');

The above code works intermittently when increasing the interval specified for the setTimeout function. Since the time it takes to get the css file varies with the server location, I rewrote the code as follows and it works fine now.

<script>
function printTest(){
var scale=1;
var contents=document.getElementById("testPrintDiv").innerHTML;
if(!scale){ 
    scale=".9";
}
    var printframe = document.createElement('iframe');
    printframe.name = "printframe";
    printframe.style.position = "absolute";
    printframe.style.top = "-1000000px";
    document.body.appendChild(printframe);
    var frameDoc = printframe.contentWindow ? printframe.contentWindow : printframe.contentDocument.document ? printframe.contentDocument.document : printframe.contentDocument;
    $.when( $.ajax({url:'/user/data/css/styles-min.css',cache:true}) )
            .done(function(response) {
    frameDoc.document.open();
    frameDoc.document.write('<meta http-equiv="X-UA-Compatible" content="IE=edge" /><html><head><title></title>');
    frameDoc.document.write('<link rel="stylesheet" type="text/css" href="/user/data/css/styles-min.css">');
    frameDoc.document.write('<style type="text/css">@media Print {body {transform:scale('+scale+')}} @page{margin-left: 0cm;} body {margin-left:0;padding:0;}'
                +'</style></head><body>');
       frameDoc.document.write(contents);
        frameDoc.document.write('</body></html>');
       frameDoc.document.close();

       setTimeout(function () {
            window.frames["printframe"].focus(); 
            window.frames["printframe"].print();       
             setTimeout(function () {
                document.body.removeChild(printframe);
             },1500);
        }, 1500);

    });     
}

</script>

Related


iframe print not working in Chrome when url starts with https

Raj I need to print the page without using window.open and window.print functions. I achieved the same using the code below. This works if the url doesn't start with https (at least what I thought). If the url starts with https, chrome prints out a blank page.

iframe print not working in Chrome when url starts with https

Raj I need to print the page without using window.open and window.print functions. I achieved the same using the code below. This works if the url doesn't start with https (at least what I thought). If the url starts with https, chrome prints out a blank page.

iframe print not working in Chrome when url starts with https

Raj I need to print the page without using window.open and window.print functions. I achieved the same using the code below. This works if the url doesn't start with https (at least what I thought). If the url starts with https, chrome prints out a blank page.

Detect when an iframe starts loading a new url

Philip: How can I detect that an iframe on a page starts loading a new page? Our situation is: When the iFrame content starts changing, we need to show the "loading" animation and hide the search box. I know how to handle the "iframe has finished loading" even

Detect when an iframe starts loading a new url

Philip: How can I detect that an iframe on a page starts loading a new page? Our situation is: When the iFrame content starts changing, we need to show the "loading" animation and hide the search box. I know how to handle the "iframe has finished loading" even

Detect when an iframe starts loading a new url

Philip: How can I detect that an iframe on a page starts loading a new page? Our situation is: When the iFrame content starts changing, we need to show the "loading" animation and hide the search box. I know how to handle the "iframe has finished loading" even

Chrome error when embedding video url using iframe

Mark Nuokin When I embed some urls in a webpage with an iframe, I am getting the following error in the chrome console. Why is this so? GET chrome-extension://boadgeojelhgndaghljhdicfkmllpafd/cast_sender.js net::ERR_FAILEDfj @ www-embed-player-new.js:151ej @ w

Chrome error when embedding video url using iframe

Mark Nuokin When I embed some urls in a webpage with an iframe, I am getting the following error in the chrome console. Why is this so? GET chrome-extension://boadgeojelhgndaghljhdicfkmllpafd/cast_sender.js net::ERR_FAILEDfj @ www-embed-player-new.js:151ej @ w

Chrome error when embedding video url using iframe

Mark Nuokin When I embed some urls in a webpage with an iframe, I am getting the following error in the chrome console. Why is this so? GET chrome-extension://boadgeojelhgndaghljhdicfkmllpafd/cast_sender.js net::ERR_FAILEDfj @ www-embed-player-new.js:151ej @ w

HTTPS iframe inside HTTPS page not working

Garudan How do we properly use github pages embedded in iframes? I've hosted a website in firebase using a custom domain over https such as https://www.example.com . This site uses react and other stuff, but for one avenue (landing page is one) I want to use a

HTTPS iframe inside HTTPS page not working

Garudan How do we properly use github pages embedded in iframes? I've hosted a website in firebase using a custom domain over https such as https://www.example.com . This site uses react and other stuff, but for one avenue (landing page is one) I want to use a

HTTPS iframe inside HTTPS page not working

Garudan How do we properly use github pages embedded in iframes? I've hosted a website in firebase using a custom domain over https such as https://www.example.com . This site uses react and other stuff, but for one avenue (landing page is one) I want to use a

@media print not working in Chrome

CMSCSS Many answers have been found/read/tried, so sorry if the solution has been posted but Chrome doesn't display print styles correctly and is missing many elements (but not all). The setting method is as follows. 1. Setting The print styles are set at the

@media print not working in Chrome

CMSCSS Many answers have been found/read/tried, so sorry if the solution has been posted but Chrome doesn't display print styles correctly and is missing many elements (but not all). The setting method is as follows. 1. Setting The print styles are set at the

@media print not working in Chrome

CMSCSS Many answers have been found/read/tried, so sorry if the solution has been posted but Chrome doesn't display print styles correctly and is missing many elements (but not all). The setting method is as follows. 1. Setting The print styles are set at the

@media print not working in Chrome

CMSCSS Many answers have been found/read/tried, so sorry if the solution has been posted but Chrome doesn't display print styles correctly and is missing many elements (but not all). The setting method is as follows. 1. Setting The print styles are set at the

@media print not working in Chrome

CMSCSS Many answers have been found/read/tried, so sorry if the solution has been posted but Chrome doesn't display print styles correctly and is missing many elements (but not all). The setting method is as follows. 1. Setting The print styles are set at the

@media print not working in Chrome

CMSCSS Many answers have been found/read/tried, so sorry if the solution has been posted but Chrome doesn't display print styles correctly and is missing many elements (but not all). The setting method is as follows. 1. Setting The print styles are set at the

How to execute https:// url in iframe

Dexter this is my code <!DOCTYPE html> <html lang="en"> <head> </head> <body> <div id="container"> <iframe src="https://google.com" style="width:1000px;height:1000px"></iframe> </div> </body> </html> and console error, i.e. Refused to display 'https://w

How to execute https:// url in iframe

Dexter this is my code <!DOCTYPE html> <html lang="en"> <head> </head> <body> <div id="container"> <iframe src="https://google.com" style="width:1000px;height:1000px"></iframe> </div> </body> </html> and console error, i.e. Refused to display 'https://w

Chrome extension - execute code when Chrome starts

Benoit Rastier I'm developing a Chrome extension and I'm looking into how code is executed when chrome starts up. I have these files: script.js index.html manifest.json In script.js I have: $(document).ready(function() { $.getJSON("https://api.twitch.tv/k

Chrome extension - execute code when Chrome starts

Benoit Rastier I'm developing a Chrome extension and I'm looking into how code is executed when chrome starts up. I have these files: script.js index.html manifest.json In script.js I have: $(document).ready(function() { $.getJSON("https://api.twitch.tv/k

Selenium (Chrome) and BrowserMob not working with https

Madis Kangro I've been trying to integrate BrowserMob into my selenium tests. It works fine on a website using http, but on an https website the browser stops and the HAR file doesn't contain any requests. I get this error on my browser when navigating to an h

How to print iframe from JavaScript in Safari/Chrome

Andrew Bullock: Can someone please help me with the issue of printing iframe content in Safari/Chrome via a JavaScript call. This works in Firefox: $('#' + id)[0].focus(); $('#' + id)[0].contentWindow.print(); This works in IE: window.frames[id].focus(); wind

How to print iframe from JavaScript in Safari/Chrome

Andrew Bullock: Can someone please help me with the issue of printing iframe content in Safari/Chrome via a JavaScript call. This works in Firefox: $('#' + id)[0].focus(); $('#' + id)[0].contentWindow.print(); This works in IE: window.frames[id].focus(); wind

How to print iframe from JavaScript in Safari/Chrome

Andrew Bullock: Can someone please help me with the issue of printing iframe content in Safari/Chrome via a JavaScript call. This works in Firefox: $('#' + id)[0].focus(); $('#' + id)[0].contentWindow.print(); This works in IE: window.frames[id].focus(); wind

Print application banner when Spring Context starts

Josh Chappelle I'm trying to implement an app banner that prints the version and other information when our app starts. My attempts to implement SmartLifecycle and ApplicationListener failed. The callback method is never called and the banner is never printed.

Print application banner when Spring Context starts

Josh Chappelle I'm trying to implement an app banner that prints the version and other information when our app starts. My attempts to implement SmartLifecycle and ApplicationListener failed. The callback method is never called and the banner is never printed.