0

I've fiddle print table but barcode column showing blank. How can i print table with barcode image?

Sample Javascript code like that

function printData()
{
   var divToPrint=document.getElementById("printTable");
   newWin= window.open("");
   newWin.document.write(divToPrint.outerHTML);
   newWin.print();
   newWin.close();
}

$('button').on('click',function(){
printData();
})

2 Answers 2

1

http://jsfiddle.net/0tc9hb7u/12/

First you need to get all images in your new window

var images = newWin.document.getElementsByTagName("img");

Then you should call newWin.print() only when all images are loaded.

var imagesLoaded = 0;
for (var i = 0; i < images.length; i++) {
    var image = images[i];
    var img = new Image(); 

    img.onload = function () {
        imagesLoaded++;
        // Ok, image was loaded, let's check if it was the last image to load
        if (imagesLoaded === images.length) {
            newWin.print();
        }
    }
    var oneSrc = image.getAttribute('src');

    img.src = oneSrc;
};
Sign up to request clarification or add additional context in comments.

6 Comments

Updated my answer with print call when image is loaded ;)
Thank you :) it is worked for me. I've question i added an image like watermark on jsfiddle. But it is not showing can you check it jsfiddle.net/0tc9hb7u/9
jsfiddle.net/0tc9hb7u/10 The problem was that you add only table to print page (without watermark)
Thank you for answer. Yes i forgot add in printTable :) But watermark is not looking as watermark on print preview/print out watermark image on top table. Do you have any idea for the watermark? @Arūnas Smaliukas
jsfiddle.net/0tc9hb7u/12 Your styles was not in printTable either. Moreover I have updated script to wait until ALL images are loaded and then show print dialog.
|
1

Try setting "newWin" as newWin name property ; utilizing setTimeout to delay calling .print() until images loaded in newly opened window

function printData() {
    var divToPrint = document.getElementById("printTable");
    newWin = window.open("", "newWin");
    newWin.document.write(divToPrint.outerHTML);
    setTimeout(function () {
        newWin.print();
    }, 1000)
    // newWin.close();
}

$('button').on('click', function () {
    printData();
})

jsfiddle http://jsfiddle.net/0tc9hb7u/4/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.