4

I have a simple HTML document which includes an image tag only. I want to print the image after document's loaded. my code:

<body>    
    <img id="form1" src="form1.jpg" alt="form1" style="margin: 0 auto;display:block;" onLoad="javascript:window.print()" />
</body>

it works but the problem is that it prints both the image ( webpage ) and another paper which is empty and only has a text on top which is giving the address of image and html title. how can I prevent from printing the extra paper ?

P.S : I used jquery plugins like PrintElement.js, jqPrint.js, etc, but they have same problem...

karthikr
  • 97,368
  • 26
  • 197
  • 188
Payam Shakibafar
  • 1,115
  • 3
  • 12
  • 25

3 Answers3

4

You can use CSS for controlling the image size, etc for print - using @media print

Give the image a max-width.

That way, you can control how the image renders on the page. Example:

@media print { 
    body img {
       width: 90%; max-width: 1048px; 
    } 
}
karthikr
  • 97,368
  • 26
  • 197
  • 188
1

Scale down the image a little bit and center it with margin.

<img src="form1.jpg" width="90%" style="margin:auto;"/>
Licson
  • 2,231
  • 18
  • 26
  • 1
    the image is completely inside the first page, the second page is something which is not related to the image I guess... the second paper includes the html file address only like files:///C:/Users/.../index.html – Payam Shakibafar Feb 02 '13 at 13:09
0

You Can Make a iframe tag by js and Put Image On it Then Print only iframe.

I hope it Work

<iframe id="iframeprint" style="display:none"></iframe>

maybe You Need This js Code

access To inside Of iframe

document.getElementById('iframeprint').contentWindow.document.body.innerHTML=document.getElementById('form1');

print iframe only

document.getElementById('iframeprint').contentWindow.print() ; 
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22