Javascript 从 JS 设置打印首选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6336995/
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-08-23 21:18:13  来源:igfitidea点击:

Set Printing Preferences from JS

javascriptjavascript-events

提问by frosty

So... from what I can find on the Google, I can't find a way to use Javascript to set my printing preferences (read: margins, orientations, footer, etc).

所以......从我在谷歌上可以找到的内容来看,我找不到使用 Javascript 来设置我的打印首选项(阅读:边距、方向、页脚等)的方法。

I am about to tell my boss that what he wants to do isn't possible. I wanted to check will you fine folks priors to doing this. Please let me know.

我正要告诉我的老板他想做的事情是不可能的。我想在做这件事之前检查一下你们会不会很好。请告诉我。

I found that in FF you can use some user_pref(key,val) settings to set up the user's preferences, but that doesn't work for over 90% of my users. I am looking for something that will cover the IE's (as many as possible), FF and Chrome.

我发现在 FF 中您可以使用一些 user_pref(key,val) 设置来设置用户的首选项,但这对我 90% 以上的用户不起作用。我正在寻找能够覆盖 IE(尽可能多)、FF 和 Chrome 的东西。

Thank you to the ninja's who reply.

感谢回复的忍者。

采纳答案by frosty

So... after all day of looking, I think that I found the answer to my own question. In short, the answer is that JS and/or CSS will not allow you to override the default page setup of the client's browser.

所以......经过一整天的寻找,我想我找到了自己问题的答案。简而言之,答案是 JS 和/或 CSS 不允许您覆盖客户端浏览器的默认页面设置。

Here is what I tried. If you create a simple HTML file with and empty head, and a body that only contains the text "Test", you will be able to see what I tested.

这是我尝试过的。如果您创建一个带有空头的简单 HTML 文件,以及一个仅包含文本“Test”的正文,您将能够看到我测试的内容。

  1. Print Style Sheets cannot solve your problem. Setting the margin on the body to 0px 0px 0px 0px is not the same thing as clicking File > Page Setup and setting all of your margins to 0. Despite what Print Style Sheeters would like you to believe, they are different. Try it for yourself. This is why Print Style Sheets do not solve your problem.

  2. JS cannot solve your problem. Can you image if every page that you visited modified your local JS properties? To allow each page to have access to your local print setups would be a security breach, and is not allowed. Because of this, JS cannot solve your problem.

  1. 打印样式表不能解决您的问题。将正文的边距设置为 0px 0px 0px 0px 与单击“文件”>“页面设置”并将所有边距设置为 0 不同。尽管 Print Style Sheeters 希望您相信什么,但它们是不同的。自己试试吧。这就是打印样式表不能解决您的问题的原因。

  2. JS 不能解决你的问题。如果您访问的每个页面都修改了您的本地 JS 属性,您能想象吗?允许每个页面访问您的本地打印设置将是安全漏洞,并且是不允许的。因此,JS 无法解决您的问题。

I would love to have someone respond here and let me know that I am wrong. Otherwise... this sucks... and it needs to happen. I have a ton of old users... and getting them to set the margins in their page setup is a pain. Also the customer don't want us to refactor the page so that they don't need to. I am in-between a rock and a hard spot.

我希望有人在这里做出回应,让我知道我错了。否则......这很糟糕......它需要发生。我有很多老用户......让他们在他们的页面设置中设置边距是一种痛苦。此外,客户不希望我们重构页面,以便他们不需要。我介于岩石和硬点之间。

回答by JamesHalsall

You can set page orientation and margin with CSS:

您可以使用 CSS 设置页面方向和边距:

@page {
   size: landscape;
   margin: 10%
}

You can also set a footer at the bottom of each page by using the <tfoot>element.

您还可以使用<tfoot>元素在每个页面的底部设置页脚。

EDIT: As pointed out by Beejamin, this is a half-way house. You can't really change printer preferences (those that appear in the Print Dialog when clicking Print). Also, IE is severely lacking in support for @pageselector, (had a look around to see if IE9 supported it and couldn't find anything). As an alternative you could always try applying it to the body in your print stylesheet.

编辑:正如 Beejamin 所指出的,这是一个中途的房子。您无法真正更改打印机首选项(单击打印时出现在打印对话框中的首选项)。此外,IE 严重缺乏对@page选择器的支持(环顾四周,看看 IE9 是否支持它,但找不到任何东西)。作为替代方案,您始终可以尝试将其应用于打印样式表中的正文。

body {
   size: landscape;
   margin: 10%
}

Although off my head I'm not sure how effective this is.

虽然我很困惑,但我不确定这有多有效。

回答by Ben Hull

I think you're pretty much out of luck trying to do this in HTML and CSS. A big part of the issue is that printer margins are all specific to the type of printer: Most printers have a minimum margin they can set (equivalent to the space they need to grip the page, and the left/right movement of the head.

我认为您尝试在 HTML 和 CSS 中执行此操作非常不走运。问题的很大一部分是打印机边距都是特定于打印机类型的:大多数打印机都有他们可以设置的最小边距(相当于他们需要抓住页面的空间,以及头部的左/右移动。

Acceptable margins will also vary based on paper size (the margin can typically be smaller on a smaller sheet than the maximum the printer will accept). For instance, if you load A5 paper into an A4 printer, the margins you can set will be different than if you load A4.

可接受的边距也会因纸张尺寸而异(较小纸张上的边距通常会小于打印机可接受的最大边距)。例如,如果您将 A5 纸装入 A4 打印机,您可以设置的边距将与装入 A4 时不同。

None of that information is available to the webpage, via CSS, javascript, or anything.

这些信息都无法通过 CSS、javascript 或任何方式提供给网页。

Now, as far as a solution goes, PDF files do allow you to embed some printer default settings within the file - Acrobat pro will allow you to specify scaling options, default numbers of copies, etc. I wouldn't mind betting that there are more potential settings available in the file format that Acrobat doesn't expose.

现在,就解决方案而言,PDF 文件确实允许您在文件中嵌入一些打印机默认设置 - Acrobat pro 将允许您指定缩放选项、默认份数等。我不介意打赌有Acrobat 未公开的文件格式中可用的更多潜在设置。

There are plenty of full-featured PDF generation tools for whichever server-side technology you use. the good ones will even allow you to provide a URL, and render HTML+CSS to PDF content - that would automate the generation to some extent. PDF generation is pretty CPU intensive though, and it wouldn't be seamless to the user, either (and would require PDF plugins for most browsers).

无论您使用哪种服务器端技术,都有大量功能齐全的 PDF 生成工具。好的甚至允许您提供一个 URL,并将 HTML+CSS 呈现为 PDF 内容 - 这将在某种程度上自动生成。尽管如此,PDF 生成是相当占用 CPU 的,而且对用户来说也不是无缝的(并且大多数浏览器都需要 PDF 插件)。

I know it's not ideal, but it's the avenue I'd look into. Good luck!

我知道这并不理想,但这是我要研究的途径。祝你好运!

回答by nacojohn

Using CSS will help you achieve that.

使用 CSS 将帮助您实现这一目标。

@page {
  margin:0cm;
}