简体   繁体   English

如何使用javascript打印带有图像的表格?

[英]How to print table with image using javascript?

I've fiddle print table but barcode column showing blank. 摆弄了打印表,但条形码列显示空白。 How can i print table with barcode image? 如何打印带有条形码图像的表格?

Sample Javascript code like that 这样的示例Javascript代码

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

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

http://jsfiddle.net/0tc9hb7u/12/ 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. 然后,仅当加载所有图像时,才应调用newWin.print()

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;
};

Try setting "newWin" as newWin name property ; 尝试将"newWin"设置为newWin name属性; utilizing setTimeout to delay calling .print() until images loaded in newly opened window 利用setTimeout延迟调用.print()直到在新打开的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/ jsfiddle http://jsfiddle.net/0tc9hb7u/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM