Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple image resize and print, and print image 4 times in one print

Status
Not open for further replies.

shdale

Programmer
Nov 18, 2008
4
GB
I am sure that this must be really simple but I'm not a java programmer and have absolutely no idea where to start. Please help.

I am using this code:

<script type="text/javascript">
var pwin;
function printImg() {
pwin = window.open(document.getElementById("imgMain").src,"_blank");
setTimeout("pwin.print()",20);
}
</script>


I want to adapt it so that I can resize an image on print in one option and print multiple images in another option. I have no idea how to do this. Can anyone help? Many thanks.
 
...I'm not a java programmer...
Nor am I. I'm a javascript developer. Please understand the difference (there is a link at the bottom of this post that explains the difference).

Here is a stand-alone test page that you can use to see how you might do this:

Code:
<html>
<head>
<title>Image print test</title>
<script type="text/javascript">
var pwin;
function printImg(printOption) {
	var imageToPrint = document.getElementById("imgMain").src;
	var html = imageToPrint;
	if (printOption == 'option2') {
		html = '<html><head><style type="text/css">img { float: left; margin: 0; padding: 0;}</style></head><body>';
		html += '<img src="' + imageToPrint + '" alt="" width="105" height="57">';
		html += '<img src="' + imageToPrint + '" alt="" width="105" height="57">';
		html += '<br clear="both">';
		html += '<img src="' + imageToPrint + '" alt="" width="105" height="57">';
		html += '<img src="' + imageToPrint + '" alt="" width="105" height="57">';
		html += '</body></html>';
		pwin = window.open('', "_blank");
		pwin.document.write(html);
		pwin.document.close();
	} else {
		pwin = window.open(html, "_blank");
	}
	setTimeout("pwin.print()" ,20);
}
</script>
</head>
<body>
<a href="javascript://;" onclick="printImg()">Print a single image</a>
<br>
<a href="javascript://;" onclick="printImg('option2')">Print the same image (4 on the page)</a>
<br>
<img id="imgMain" src="[URL unfurl="true"]http://www.tek-tips.com/images/logo.gif"[/URL] width="210" height="114" alt=""/>
</body>
</html>

Cheers,
Jeff

[tt]Visit my blog [!]@[/!] Visit Code Couch [!]@[/!] [/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Hi Jeff, you are a star! I tried this code and it work like a treat - very happy and relieved to say the least. One last thing though, how can I get the new, pop-up window to close once it has printed?
Kind regards,
Sheila.
 
Just a quick follow-up on this... I don't know of any way you can auto-close the popup when printing has completed. There may be some IE specific solution using some ActiveX objects, but I don't know of anything that will work for all browsers etc.

Cheers,
Jeff

[tt]Visit my blog [!]@[/!] Visit Code Couch [!]@[/!] [/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Sheila,

Why have two threads going for one question?

thread216-1515225

Can you red-flag one of these, as having two open is pointless.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top