社区
Web 开发
帖子详情
标签使用contentWindow.print打印文件的问题
qq_40968955
2020-08-07 04:20:17
最近做一个客户端打印服务端文件的需求,发现没办法实现,最后找到一种用<iframe>标签来实现的办法
但是试了下,文件可以下载成功,但是打印出来是空白
请问有没有这方面经验的人用过,是什么问题?
...全文
1137
回复
打赏
收藏
<iframe>标签使用contentWindow.print打印文件的问题
最近做一个客户端打印服务端文件的需求,发现没办法实现,最后找到一种用标签来实现的办法 但是试了下,文件可以下载成功,但是打印出来是空白 请问有没有这方面经验的人用过,是什么问题?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
图形jquery.gvChart-1.0.1.min.js
/* @license * jQuery.
pr
int
, version 1.3.2 * (c) Sathvik Ponangi, Doers' Guild * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) *--------------------------------------------------------------------------*/ (function ($) { "use strict"; // A nice closure for our definitions function getjQueryObject(string) { // Make string a vaild jQuery thing var jqObj = $(""); try { jqObj = $(string) .clone(); } catch (e) { jqObj = $("
") .html(string); } return jqObj; } function
pr
int
Frame(frame
Window
) { //
Pr
int
the selected
window
/
iframe
var def = $.Deferred(); try { setTimeout(function () { // Fix for IE : Allow it to render the
iframe
frame
Window
.focus(); try { // Fix for IE11 -
pr
int
ng the whole page instead of the
iframe
content
if (!frame
Window
.document.execCommand('
pr
int
', false, null)) { // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891 frame
Window
.
pr
int
(); } } catch (e) { frame
Window
.
pr
int
(); } frame
Window
.close(); def.resolve(); }, 250); } catch (err) { def.reject(err); } return def; } function
pr
int
Content
InNew
Window
(
content
) { // Open a new
window
and
pr
int
selected
content
var w =
window
.open(); w.[removed](
content
); w.document.close(); return
pr
int
Frame(w); } function isNode(o) { /* http://stackoverflow.com/a/384380/937891 */ return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string"); } $.
pr
int
= $.fn.
pr
int
= function () { //
Pr
int
a given set of elements var options, $this, self = this; // console.log("
Pr
int
ing", this, arguments); if (self instanceof $) { // Get the node if it is a jQuery object self = self.get(0); } if (isNode(self)) { // If `this` is a HTML element, i.e. for // $(selector).
pr
int
() $this = $(self); if (arguments.length > 0) { options = arguments[0]; } } else { if (arguments.length > 0) { // $.
pr
int
(selector,options) $this = $(arguments[0]); if (isNode($this[0])) { if (arguments.length > 1) { options = arguments[1]; } } else { // $.
pr
int
(options) options = arguments[0]; $this = $("html"); } } else { // $.
pr
int
() $this = $("html"); } } // Default options var defaults = { globalStyles: true, media
Pr
int
: false, stylesheet: null, no
Pr
int
Selector: ".no-
pr
int
",
iframe
: true, append: null,
pr
epend: null, manuallyCopyFormValues: true, deferred: $.Deferred() }; // Merge with user-options options = $.extend({}, defaults, (options || {})); var $styles = $(""); if (options.globalStyles) { // Apply the stlyes from the current sheet to the
pr
int
ed page $styles = $("style, link, meta, title"); } else if (options.media
Pr
int
) { // Apply the media-
pr
int
stylesheet $styles = $("link[media=
pr
int
]"); } if (options.stylesheet) { // Add a custom stylesheet if given $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">')); } // Create a copy of the element to
pr
int
var copy = $this.clone(); // Wrap it in a span to get the HTML markup string copy = $("
") .append(copy); // Remove unwanted elements copy.find(options.no
Pr
int
Selector) .remove(); // Add in the styles copy.append($styles.clone()); // Appedned
content
copy.append(getjQueryObject(options.append)); //
Pr
epended
content
copy.
pr
epend(getjQueryObject(options.
pr
epend)); if (options.manuallyCopyFormValues) { // Manually copy form values
int
o the HTML for
pr
int
ing user-modified input fields // http://stackoverflow.com/a/26707753 copy.find("input") .each(function () { var $field = $(this); if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) { if ($field.
pr
op("checked")) { $field.attr("checked", "checked"); } } else { $field.attr("value", $field.val()); } }); copy.find("select").each(function () { var $field = $(this); $field.find(":selected").attr("selected", "selected"); }); copy.find("textarea").each(function () { // Fix for https://github.com/DoersGuild/jQuery.
pr
int
/issues/18#issuecomment-96451589 var $field = $(this); $field.text($field.val()); }); } // Get the HTML markup string var
content
= copy.html(); // Notify with generated markup & cloned elements - useful for logging, etc try { options.deferred.notify('generated_markup',
content
, copy); } catch (err) { console.warn('Error notifying deferred', err); } // Destroy the copy copy.remove(); if (options.
iframe
) { // Use an
iframe
for
pr
int
ing try { var $
iframe
= $(options.
iframe
+ ""); var
iframe
Count = $
iframe
.length; if (
iframe
Count === 0) { // Create a new
iFrame
if none is given $
iframe
= $('<
iframe
height="0" width="0" border="0" wmode="Opaque"/>') .
pr
ependTo('body') .css({ "position": "absolute", "top": -999, "left": -999 }); } var w, wdoc; w = $
iframe
.get(0); w = w.
content
Window
|| w.
content
Document || w; wdoc = w.document || w.
content
Document || w; wdoc.open(); wdoc.write(
content
); wdoc.close();
pr
int
Frame(w) .done(function () { // Success setTimeout(function () { // Wait for IE if (
iframe
Count === 0) { // Destroy the
iframe
if created here $
iframe
.remove(); } }, 100); }) .fail(function (err) { // Use the pop-up method if
iframe
fails for some reason console.error("Failed to
pr
int
from
iframe
", err);
pr
int
Content
InNew
Window
(
content
); }) .always(function () { try { options.deferred.resolve(); } catch (err) { console.warn('Error notifying deferred', err); } }); } catch (e) { // Use the pop-up method if
iframe
fails for some reason console.error("Failed to
pr
int
from
iframe
", e.stack, e.message);
pr
int
Content
InNew
Window
(
content
) .always(function () { try { options.deferred.resolve(); } catch (err) { console.warn('Error notifying deferred', err); } }); } } else { // Use a new
window
for
pr
int
ing
pr
int
Content
InNew
Window
(
content
) .always(function () { try { options.deferred.resolve(); } catch (err) { console.warn('Error notifying deferred', err); } }); } return this; }; })(jQuery);
jQuery打印图片pdf、txt示例代码
html加如下代码
pr
int
>
<input type=button id=
pr
int
_button value=
Pr
int
onclick=document.getElementById('FILEto
Pr
int
').focus(); document.getElementById('FILEto
Pr
int
').
content
Window
.
pr
int
(); />
[removed] function
pr
int
Some(path){ //传入文件路径 $(#to_
html 打印相关操作与实现详解
原理为调用
window
.
pr
int
() 方法,但是该方法只能对当前页面全部打印,所以有了以下方案来解决局部打印 1: 利用
iframe
将需要打印的元素和样式注入 再调用打印 // 示例代码 function
pr
int
() { let ifElement = document.getElementById('ifId') const addHtml
Pr
int
= () => { const
content
= ifElement.
content
Window
|| ifElement.
content
Document
content
.doc
使用
window
.
pr
int
()打印局部页面,ifrme打印ie报错
问题
:
使用
window
.
pr
int
();打印页面,将不需要的数据隐藏,将需要的数据显示,打印完成之后将刚才隐藏的数据还原,显示的数据隐藏, 谷歌浏览器、部分ie能一气呵成, 火狐浏览器以及少数ie浏览器出现了:执行到
window
.
pr
int
();时后面的js还未开始执行,类似与alert的阻塞,要等到用户点完页面弹出的打印框才会执行,这样导致隐藏的数据没有显示,显示的数据没有隐藏, ...
js页面实现打印
iframe
+
window
.
pr
int
()
使用
iframe
+
window
.
pr
int
() 实现打印功能和避坑
Web 开发
81,092
社区成员
341,716
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章