javascript:window.print() 打印 2 页,而我有 1 页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14662091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:16:11  来源:igfitidea点击:

javascript:window.print() prints 2 pages while I have 1

javascriptcssprinting

提问by Payam Shakibafar

I have a simple HTML document which includes an image tag only. I want to print the image after document's loaded. my code:

我有一个简单的 HTML 文档,其中仅包含一个图像标签。我想在文档加载后打印图像。我的代码:

<body>    
    <img id="form1" src="form1.jpg" alt="form1" style="margin: 0 auto;display:block;" onLoad="javascript:window.print()" />
</body>

it works but the problem is that it prints both the image ( webpage ) and another paper which is empty and only has a text on top which is giving the address of image and html title. how can I prevent from printing the extra paper ?

它可以工作,但问题是它同时打印图像(网页)和另一张空纸,顶部只有一个文本,提供图像地址和 html 标题。如何防止打印多余的纸张?

P.S : I used jquery plugins like PrintElement.js, jqPrint.js, etc, but they have same problem...

PS:我使用了像 PrintElement.js、jqPrint.js 等 jquery 插件,但他们有同样的问题......

采纳答案by karthikr

You can use CSS for controlling the image size, etc for print - using @media print

您可以使用 CSS 来控制图像大小等进行打印 - 使用 @media print

Give the image a max-width.

给图像一个max-width.

That way, you can control how the image renders on the page. Example:

这样,您就可以控制图像在页面上的呈现方式。例子:

@media print { 
    body img {
       width: 90%; max-width: 1048px; 
    } 
}

回答by Licson

Scale down the image a little bit and center it with margin.

将图像缩小一点,并以边距居中。

<img src="form1.jpg" width="90%" style="margin:auto;"/>

回答by mohammad mohsenipur

You Can Make a iframe tag by js and Put Image On it Then Print only iframe.

您可以通过 js 制作 iframe 标签并在其上放置图像,然后仅打印 iframe。

I hope it Work

我希望它有效

<iframe id="iframeprint" style="display:none"></iframe>

maybe You Need This js Code

也许你需要这个js代码

access To inside Of iframe

访问 iframe 内部

document.getElementById('iframeprint').contentWindow.document.body.innerHTML=document.getElementById('form1');

print iframe only

仅打印 iframe

document.getElementById('iframeprint').contentWindow.print() ;