仅打印网页的图像



我在一个网站页面上有一张650px X 1000px的照片。照片下面有一个按钮。

除了整页外,我怎么能只打印照片呢。我想将其保存为PDF或只打印图像。不另存为图像。

  1. 使用window.open打开新窗口。

  2. 写入img标签。

  3. 加载图像后,使用window.print()

  4. 使用window.close()关闭窗口

你可以用这样的代码来做到这一点:

编写脚本

function printImg(url) {
var win = window.open('');
win.document.write('<img src="' + url + '" onload="window.print();window.close()" />');
win.focus();
}

Index.html

<img src="https://i.stack.imgur.com/hCYTd.jpg" />
<button onclick="printImg('https://i.stack.imgur.com/hCYTd.jpg')">Print</button>

最新更新