Chrome打印空白页面

6
我有一段旧的JavaScript代码来打印图片,如果用户点击缩略图的话。它以前运行得很好,但是最近(只在Chrome中)在预览中出现了空白页面。
这里有一个JsBin演示:http://jsbin.com/yehefuwaso/7 点击打印机图标。现在在Firefox中尝试;它将按预期工作。
Chrome:41.0.2272.89 m
Firefox:30.0、36.0.1
function newWindow(src){

  win = window.open("","","width=600,height=600");
    var doc = win.document;
    // init head
    var head = doc.getElementsByTagName("head")[0];
    // create title
    var title = doc.createElement("title");
    title.text = "Child Window";
    head.appendChild(title);
    // create script
    var code = "function printFunction() { window.focus(); window.print(); }";
    var script = doc.createElement("script");
    script.text = code;
    script.type = "text/javascript";
    head.appendChild(script);
    // init body
    var body = doc.body;
    //image
    doc.write('<img src="'+src+'" width="300">');

    //chrome
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {

      win.printFunction();               

    } else {
        win.document.close();
        win.focus();
        win.print();
        win.close();
    }
}
3个回答

4

看起来它试图在<img>加载之前进行打印,将打印调用移动到windowload事件的事件处理程序中,通过打开链接作为data URIBlob来实现,例如

var code = '\
    <html>\
        <head>\
            <title></title>\
            <script>\
    function printFunction() {\
        window.focus();\
        window.print();\
        window.close();\
    }\
    window.addEventListener(\'load\', printFunction);\
            </script>\
        </head>\
        <body><img src="'+src+'" width="300"></body>\
    </html>';

window.open('data:text/html,' + code, '_blank', 'width=600,height=600');

不要忘记你可能需要对code标签中的标签进行HTML编码


你可能只需要监听<img>load事件,但如果你做的事情比打印单个图像更复杂,你可能会发现它在将来再次出现故障。

doc.write('<img onload="printFunction();" src="'+src+'" width="300">');

其中printFunction是适用于所有浏览器的打印函数


当我尝试打印一个iframe时,遇到了类似的问题。从$(iframe [0] .contentWindow).load()内部调用 print() 可以解决这个问题。 - Coded Monkey
@Paul,你能帮忙解决这个问题吗?https://stackoverflow.com/questions/63187758/issue-while-printing-pdf-files-from-blob-agnular - Sunil Garg

3

您需要等待图像加载完成:

var img = new Image();
img.src = src;
img.style.width = '300px';
img.onload = function(){
  doc.write(img);
};

如果我有一个PDF文件,该怎么办? - Sunil Garg

3

我在Chrome中遇到了同样的问题。你可以尝试这些方法,第一个方法对我有用。setTimeout对我没有用(后来不得不编辑)。

function printDiv(divName) {

    var printContents = document.getElementById(divName).innerHTML;
    w = window.open();

    w.document.write(printContents);
    w.document.write('<scr' + 'ipt type="text/javascript">' + 'window.onload = function() { window.print(); window.close(); };' + '</sc' + 'ript>');

    w.document.close(); // necessary for IE >= 10
    w.focus(); // necessary for IE >= 10

    return true;
}

setTimeout:

<div id="printableArea">
      <h1>Print me</h1>
</div>

<input type="button" onclick="printDiv('printableArea')" value="print a div!" />



    function printDiv(divName) {

        var printContents = document.getElementById(divName).innerHTML;
        w = window.open();
        w.document.write(printContents);

        w.document.close(); // necessary for IE >= 10
        w.focus(); // necessary for IE >= 10

        setTimeout(function () { // necessary for Chrome
            w.print();
            w.close();
        }, 0);

        return true;
    }

PDF文件的大小写问题应该怎么处理? - Sunil Garg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接