简体   繁体   English

在Chrome中无法在隐藏的DIV中打印图像

[英]Printing Images in a hidden DIV failing in Chrome

Chrome 24 镀铬24

I'm working on a site re-write right now that has a curious bit of functionality. 我现在正在重新编写一个功能有些奇怪的网站。 There is a page where is a user is given a list of options to select images to print. 在一个页面上,向用户提供了一个选项列表,以选择要打印的图像。

The user checks off the checkboxes of the images they'd like, and then they click a button that makes a call back to the server and for the given images injects the wanted HTML into a hidden DIV. 用户选中所需图像的复选框,然后单击一个按钮以回调服务器,并针对给定图像将所需的HTML注入到隐藏的DIV中。

To complicate matters a bit further the image src attributes are actually a link to a .NET MVC server side action to retrieve the images from a database where we grab the image and then render some text on the image in a few different places. 进一步使事情复杂化的是,图像src属性实际上是指向.NET MVC服务器端操作的链接,以从数据库中检索图像,我们在其中抓取图像,然后在几个不同的位置上在图像上呈现一些文本。

The output HTML ends up looking like this. 输出的HTML最终看起来像这样。

<div style="visibility: hidden;">
    <div id="PrintArea">
         <div><img width="1000" src="/Controller/GetImage/2"></div>
        <div><img width="1000" src="/Controller/GetImage/3"></div>
        <div><img width="1000" src="/Controller/GetImage/5"></div>
    </div>    
</div>

That's step one in the javascript, the ajax call back to the server to populate this PrintArea DIV. 这是javascript的第一步,即ajax返回服务器以填充此PrintArea DIV。 After this, we do the following: 此后,我们执行以下操作:

//Print
$('#PrintArea').waitForImages(function () {
    $("#PrintArea").jqprint({ importCSS: true });
    //$.unblockUI();
});

To accommodate the server side loading the images, we use the waitForImages jquery plugin and then use jqprint to print the content. 为了适应服务器端加载图像,我们使用了waitForImages jquery插件,然后使用jqprint来打印内容。

To complete the equation, we have a print stylesheet (the main stylesheet is media="screen" so it should be the only stylesheet in play). 为了完成方程式,我们有一个打印样式表(主要样式表为media =“ screen”,因此它应该是唯一的样式表)。

body * {
    visibility:hidden;
  }

#PrintArea, #PrintArea * { visibility: visible; }

#PrintArea { position: absolute;left: 0;top: 0; }

img { display: block !important; }

This works fine in Firefox and IE. 在Firefox和IE中可以正常工作。

Chrome does not work consistently . 浏览器不工作始终 What will happen is that when three images are selected the first two will be displayed but the third which should go on a second page sometimes is displayed and printed, but other times is not. 将会发生的情况是,当选择三个图像时,将显示前两个图像,但是有时会显示并打印应在第二页上显示的第三个图像,而其他时候则不会。 It seems sporadic and quite random. 似乎是零星的,而且相当随机。

Any suggestions or ideas or how to get Chrome to consistently print the images across page breaks? 有什么建议或想法,或者如何让Chrome在分页符之间连续打印图像? Our line of thinking internally here was that Chrome's print preview window was actually rendering before the waitForImages call finished and thus the content was not displaying, but I haven't come up with a concrete way of testing this. 我们内部的思路是,Chrome的打印预览窗口实际上是在waitForImages调用完成之前渲染的,因此内容未显示,但是我没有想出具体的测试方法。

Thanks 谢谢

Maybe try window load: 也许尝试窗口加载:

$(window).load(function() {
    // executes when complete page is fully loaded, including all frames, objects and images
    $('#PrintArea').waitForImages(function () {
        $("#PrintArea").jqprint({ importCSS: true });
        //$.unblockUI();
    });
});

The window load event executes a bit later when the complete page is fully loaded, including all frames, objects and images. 当完整页面(包括所有框架,对象和图像)完全加载后,窗口加载事件将在稍后执行。 Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself. 因此,与图像或其他页面内容有关的功能应放置在窗口或内容标签本身的加载事件中。

source: http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/ 来源:http: //4loc.wordpress.com/2009/04/28/documentready-vs-windowload/

About chrome issue: http://forum.jquery.com/topic/document-ready-and-window-onload-difference 关于Chrome问题: http //forum.jquery.com/topic/document-ready-and-window-onload-difference

Hope it helps ! 希望能帮助到你 !

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

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