nr.iras.sk
nr.iras.sk

Reputation: 8488

window.print() prints blank page

I get blank page while printing. The border(it may be the frame) is printed. But not the content inside the frame. if we manually print the new page it is getting printed correctly. Why this happen?

var printwindow = window.open('', '', 'fullScreen=no');
printwindow.document.write('<iframe id="docPrint" onLoad="window.print()" width="100%" height="100%" src="http://localhost:8080/hiring/docs/Keneth _1340800082258/Keneth _resume_1340800082258.pdf"></iframe>');

Upvotes: 4

Views: 9105

Answers (5)

Elyor Ernazarov
Elyor Ernazarov

Reputation: 1

I also had a blank page. This was removed after adding this script in the head of the HTML:

<script type="text/javascript">
   window.onload = function() {
      window.print(); // PDF ni chop etish oynasini ochadi
   };
</script>

Upvotes: -1

Diego Vin&#237;cius
Diego Vin&#237;cius

Reputation: 2223

I was looking for other problem and found it open, so try to do it.

Watch to load of your iframe and get the content inside of it, cuz you have to handle the loading from this iframe or you can ajax GET the entire page and save it at iframeContent

let iframeContent = document.getElementById("docPrint").innerHTML;
const iframePage = window.open('', 'PRINT', 'height=400,width=600');
iframePage.document.write(iframeContent);
iframePage.document.close();
iframePage.focus();
iframePage.onload = relatorio.print;

Upvotes: -1

Rab
Rab

Reputation: 35572

That is not possible with JavaScript.

I don't know which language your server is written in, but it may be possible to make your PDF auto print.

Upvotes: 3

Blaster
Blaster

Reputation: 9110

Since you are injecting it dynamically, try escaping / in </iframe>:

'.....<\/iframe>'

Also apply onload to window not iframe:

printwindow.onload = printwindow.print;

So try this instead:

var printwindow = window.open('', '', 'fullScreen=no');
printwindow.document.write('<iframe id="docPrint" width="100%" height="100%" src="http://localhost:8080/hiring/docs/Keneth _1340800082258/Keneth _resume_1340800082258.pdf"></iframe>');
printwindow.onload = printwindow.print;

I am not sure if browser will take your pdf file and print it, if pdf open directly in browser then there is a print option in browser pdf plugin separately.

Upvotes: 1

Haroldo_OK
Haroldo_OK

Reputation: 7230

The problem here is that the window.print() from the iframe is referring to the window that contains the iFrame, not the content inside of it.

Upvotes: 2

Related Questions