如何使用javascript打印图像
How to print an image using javascript
我正在尝试打印ASP。. NET Image
当我将上面的例子用于div时,它可以工作,但是我不能调整它来打印其他任何东西。我怎么能让它打印一个Image
?我包装的Image
在一个div和改变"。text()"部分在脚本到别的东西吗?
因为您向打印函数传递了错误的参数。在JavaScript中打印一些东西就像调用window.print();
方法一样简单。要测试它,只需使用浏览器的开发人员工具并在其控制台中写入:
window.print();
现在,当你想打印一些特定的东西,你有两种方式:
- 为在同一页面中打印创建一个特殊的样式表,它隐藏其他元素,只显示指定的区域。
- 或者,打开一个新窗口,复制你想要的,然后打印。
现在,你能做的就是写你自己的函数:
function printImage(image)
{
var printWindow = window.open('', 'Print Window','height=400,width=600');
printWindow.document.write('<html><head><title>Print Window</title>');
printWindow.document.write('</head><body ><img src=''');
printWindow.document.write(image.src);
printWindow.document.write(''' /></body></html>');
printWindow.document.close();
printWindow.print();
}
var image = document.getElementById('image');
printImage(image);
你也可以在这里看到这个函数的运行
让浏览器打开弹出窗口,并注意我只将图像元素的src
值传递给新窗口。
可以。
下面的java脚本将帮助您在图像控制中打印图像。
var printContent = document.getElementById('divImage');
var img = document.getElementById('imgMain');
var windowUrl = 'MyPage.aspx';
var uniqueName = new Date();
var windowName = "MyPage" + uniqueName.getTime();
printWindow = window.open(windowUrl, windowName, 'location=1,status=1,scrollbars=1,width=800,height=600');
printWindow.document.write("<div style='width:100%;'>");
printWindow.document.write("<img id='img' src='" + img.src + "'/>");
printWindow.document.write("</div>");
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
return false;
这是更好的解决方案,也工作良好的谷歌chrome:
var printWindow = window.open('', 'Print Window', 'height=533,width=800');
printWindow.document.write('<html><head><title>Print Window</title>');
printWindow.document.write("<script src='script/jquery-1.11.3.min.js' type='text/javascript'><'/script>");
printWindow.document.write("<script type='text/javascript'>");
printWindow.document.write("var DoPrint = false;");
printWindow.document.write("$(document).ready(function () {");
printWindow.document.write("DoPrint = true;");
printWindow.document.write("});");
printWindow.document.write("<'/script>");
printWindow.document.write('</head><body ><img src=''');
printWindow.document.write(document.getElementById('image').src);
printWindow.document.write(''' /></body></html>');
printWindow.document.close();
function Print() {
if (typeof printWindow.DoPrint != "undefined" && printWindow.DoPrint == true) {
clearInterval(PrintintervalNumber);
printWindow.print();
printWindow.close();
}
}
PrintintervalNumber = setInterval(Print, 1000);
是的,而不是使用.text()
,您使用.html()
,其中您将<img />
(当然包括源代码)传递给准备打印的假文档。
为什么不这么简单呢?
<input type="button" value="Print Div" onclick="PrintElem('<img src=myimage.jpg>')" />
,然后像这样调用Popup:
弹出(elem);(如果像这样传递图像要打印,则实际上不需要这个Popup()函数)
相关文章:
- 在通过child.print()打印之前,我如何等待图像加载到我的新窗口中
- 在两整页上打印两张图像
- 谷歌浏览器控制台,打印图像
- 打开新窗口进行打印时不显示 JavaScript 图像
- 如何打印网页中的所有图像?(打印机)
- 使用 window.print() 在双面纸上打印图像
- 使用Javascript打印图像
- 在画布上绘制图像并导出打印质量的图像
- 我应该如何在打印时删除打印机图像
- 从弹出窗口打印图像
- 如何打印数据's以及php中的背景图像
- Div背景图像打印空白
- 保存给定URL中的图像并以JavaScript打印
- 在新页面中打开图像,单击时打印
- 在浏览器中打印图像
- 打印您的网页屏幕并发送带有打印图像的电子邮件
- 将图像打印为 pdf 文件
- 图像打印我的网页中的文档
- 将网页中的图像打印在一起
- 窗口与图像-打印()问题在Chrome浏览器