Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  29] [ 2]  / answers: 1 / hits: 20986  / 11 Years ago, mon, december 16, 2013, 12:00:00

I want to print image with javascript.
So I used this code to open image in new window and print:



win = window.open(img.src,_blank);
win.onload = function() { win.print(); }


This works fine with the default image file:



<img id=image1 src=myimage.jpg>


But when i replace the default image with image data read from disk:



var fileElem = document.getElementById(fileElem).files[0];
var reader = new FileReader();
reader.onload = function(event) {
img.src = event.target.result;
};
reader.readAsDataURL(fileElem);


And then open new window & print - the image apears fine in the new window, but no print operation is done.
How to make the win.print() to work?


More From » image

 Answers
14

OK!



I'v tried this on Chrome and it's work well :



<html>
<head>
<script language=javascript type=text/javascript>
function printImage() {
var fileElem = document.getElementById(fileElem).files[0];
var reader = new FileReader();
reader.onload = function(event) {
var html = <html><head> +
</head> +
<body style ='-webkit-print-color-adjust:exact;'>+
<img src= + event.target.result + onload=javascript:window.print();/> +
</body>;
var win = window.open(about:blank,_blank);
win.document.write(html);

};
reader.readAsDataURL(fileElem);
}
</script>
</head>
<body>
<input type=file id=fileElem/>
<button onclick=printImage()>PRINT</button>
</body>
</html>


Regards.


[#73716] Friday, December 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
havenbilliec

Total Points: 324
Total Questions: 106
Total Answers: 94

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;