我可以将 HTML 文档强制为一页吗?

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

Can I force an HTML document to be one page long?

htmlprintingdocument

提问by Joe Morgan

I am generating a document from HTML. Is there any way to force the HTML page to be one rendered as one printed page long?

我正在从 HTML 生成一个文档。有没有办法强制将 HTML 页面呈现为一个打印页面长?

I've done most of the page with <table>and <div>tags.

我已经用<table><div>标签完成了大部分页面。

回答by bobince

Not exactly, but you can use a print-stylesheet:

不完全是,但您可以使用打印样式表:

<link rel="stylesheet" type="text/css" media="print" href="print.css" />

To set font sizes, paddings, etc. to physical units like 'pt' (that should never be used in a screen-stylesheet!) so that it ends up being more or less an A4 page, when rendered in a typical browser, with typical fonts, with a following wind. Nothing is guaranteed but you can optimise for the common case.

将字体大小、填充等设置为像“pt”这样的物理单位(永远不应在屏幕样式表中使用!),以便在典型浏览器中呈现时,它最终或多或少是一个 A4 页面,使用典型的字体,带有以下风。没有任何保证,但您可以针对常见情况进行优化。

In print you don't have to worry so much about font size preferences or dpi settings, but you still can't control the margins the browser uses or the actual page size (US Letter is smaller than A4), so leave a good amount of wiggle room and test on the major browsers.

在打印时,您不必太担心字体大小首选项或 dpi 设置,但您仍然无法控制浏览器使用的边距或实际页面大小(US Letter 小于 A4),因此请留出足够的数量的回旋余地并在主要浏览器上进行测试。

回答by Welbog

No. You can't control the user's zoom level, printer settings, paper size (is he using A4 paper in Europe or legal in the US?) and all of the various other things that affect printer output using HTML.

The best you can do with HTML is make a very simple "printer-friendly" page and keep the content short. Alternatively you can build a PDF, which is designed to let you control how it's printed.

不可以。您无法控制用户的缩放级别、打印机设置、纸张尺寸(他在欧洲使用 A4 纸还是在美国使用合法纸?)以及影响使用 HTML 的打印机输出的所有其他因素。

使用 HTML 可以做的最好的事情是制作一个非常简单的“打印机友好”页面并保持内容简短。或者,您可以构建一个 PDF,它旨在让您控制它的打印方式。

回答by Eppz

You can use CSS to style the page and set it to a certain height / width... There's a good tutorial here.

您可以使用CSS样式的页面,并将其设置到一定的高度/宽度...有一个很好的教程在这里

回答by DMeier

No, but you can make the print view a lot smoother by using a dedicated CSS file for print media, like suggested above.

不,但是您可以通过使用专门的用于打印媒体的 CSS 文件来使打印视图更加流畅,就像上面建议的那样。

Then you could for example at least prevent a table from spanning over 2 pages by using "page-break-before: always;".

然后,例如,您至少可以通过使用“page-break-before: always;”来防止表格跨越 2 页 .

And finally you could also use "display:none;"on elements that are unnecessary like ads, nav bars, etc. to keep the page itself shorter (short enough for a single page, hopefully).

最后你也可以使用 "display:none;" 在不必要的元素上,如广告、导航栏等,以保持页面本身更短(希望对于单个页面足够短)。

No guarantees, though.

不过没有保证。

回答by Keltia

Use PDF. You can force many things like font size, font type and many other parameters but any browser can decide they don't care and use whatever they want and so violate your prerequesites or decisions. HTML is not (repeat after me: is NOT) a presentation language.

使用PDF。你可以强制很多东西,比如字体大小、字体类型和许多其他参数,但任何浏览器都可以决定他们不关心并使用他们想要的任何东西,因此违反了你的先决条件或决定。HTML 不是(在我之后重复:不是)一种演示语言。

In many cases, it will mostly work but nothing is certain.

在许多情况下,它通常会起作用,但没有什么是确定的。

回答by Bombe

Sure. You have a couple of possibilities:

当然。您有几种可能性:

  • Convert your page into a large image.
  • Convert your page to PDF.
  • Annoy all your users with a page with fixed dimensions that scrolls left and right because it is too wide and makes it extremely hard to transport the information.
  • 将您的页面转换为大图像。
  • 将您的页面转换为 PDF。
  • 使用固定尺寸左右滚动的页面来惹恼您的所有用户,因为它太宽并且很难传输信息。

回答by cyberbobcat

No there isn't a way as you couldn't possibly know the printer type or paper size at the time of rendering the HTML.

不,没有办法,因为在呈现 HTML 时您不可能知道打印机类型或纸张尺寸。

My only suggestion would be to set the printer properties to scale the content to fit one page at the time of printing.

我唯一的建议是设置打印机属性以在打印时缩放内容以适合一页。