是否有可能使一个HTML页面的行为,例如,像一个a4大小的页面在MS Word?
实际上,我希望能够在浏览器中显示HTML页面,并以A4大小页面的尺寸概述内容。
为了简单起见,我假设HTML页面只包含文本(没有图像等),例如,不会有<br>标记。
另外,当HTML页面被打印出来时,它将显示为a4大小的纸质页面。
是否有可能使一个HTML页面的行为,例如,像一个a4大小的页面在MS Word?
实际上,我希望能够在浏览器中显示HTML页面,并以A4大小页面的尺寸概述内容。
为了简单起见,我假设HTML页面只包含文本(没有图像等),例如,不会有<br>标记。
另外,当HTML页面被打印出来时,它将显示为a4大小的纸质页面。
当前回答
完全可以将布局设置为a4页面的比例。你只需要设置相应的宽度和高度(可能检查窗口。innerHeight和window。虽然我不确定这是否可靠)。
The tricky part is with printing A4. Javascript for example only supports printing pages rudimentarily with the window.print method. As @Prutswonder suggested creating a PDF from the webpage probably is the most sophisticated way of doing this (other than supplying PDF documentation in the first place). However, this is not as trivial as one might think. Here's a link that has a description of an all open source Java class to create PDFs from HTML: http://www.javaworld.com/javaworld/jw-04-2006/jw-0410-html.html .
显然,一旦你创建了一个A4比例的PDF打印,它将导致一个干净的A4打印页面。这是否值得投入时间是另一个问题。
其他回答
为每一页创建节,并使用下面的代码来调整页边距、高度和宽度。
如果你打印的是A4尺寸。
然后用户
Size : 8.27in and 11.69 inches
@page Section1 {
size: 8.27in 11.69in;
margin: .5in .5in .5in .5in;
mso-header-margin: .5in;
mso-footer-margin: .5in;
mso-paper-source: 0;
}
div.Section1 {
page: Section1;
}
然后创建一个包含所有内容的div。
<div class="Section1">
type your content here...
</div>
or
@media print {
.page-break {
height: 0;
page-break-before: always;
margin: 0;
border-top: none;
}
}
body, p, a,
span, td {
font-size: 9pt;
font-family: Arial, Helvetica, sans-serif;
}
body {
margin-left: 2em;
margin-right: 2em;
}
.page {
height: 947px;
padding-top: 5px;
page-break-after: always;
font-family: Arial, Helvetica, sans-serif;
position: relative;
border-bottom: 1px solid #000;
}
完全可以将布局设置为a4页面的比例。你只需要设置相应的宽度和高度(可能检查窗口。innerHeight和window。虽然我不确定这是否可靠)。
The tricky part is with printing A4. Javascript for example only supports printing pages rudimentarily with the window.print method. As @Prutswonder suggested creating a PDF from the webpage probably is the most sophisticated way of doing this (other than supplying PDF documentation in the first place). However, this is not as trivial as one might think. Here's a link that has a description of an all open source Java class to create PDFs from HTML: http://www.javaworld.com/javaworld/jw-04-2006/jw-0410-html.html .
显然,一旦你创建了一个A4比例的PDF打印,它将导致一个干净的A4打印页面。这是否值得投入时间是另一个问题。
我使用HTML生成报告,在真实的纸张上正确地打印出真实大小。
如果你小心地使用mm作为CSS文件中的单位,你应该是OK的,至少对于单页。不过,人们可以通过改变浏览器中的打印缩放来搞砸你。
我似乎记得我所做的每一件事都是单页的,所以我不必担心分页——那可能要困难得多。
我在谷歌上搜索“A4 CSS页面模板”(codepend .io)后看到了这个解决方案。它使用<page>标记在浏览器中显示A4 (A3,A5,也是纵向)大小的区域。在这个标记中显示内容,但绝对位置仍然相对于浏览器区域。
身体{ 背景:rgb (204204204); } 页面{ 背景:白色; 显示:块; 保证金:0自动; margin-bottom: 0.5厘米; 箱影:0 0 0.5cm rgba(0,0,0,0.5); } 页面(=“A4大小){ 宽度:21厘米; 高度:29.7厘米; } 布局页面(大小= " A4 "][=“肖像”]{ 宽度:29.7厘米; 高度:21厘米; } @media print { 正文,页{ 保证金:0; 不必:0; } } <页面大小= >“A4 A4页面< / > <page size="A4" layout="portrait">A4 portrait</page>
这适用于我没有任何其他css/js库包括在内。适用于当前的浏览器(IE, FF, Chrome)。
从技术上讲,您可以这样做,但是要让所有浏览器打印出与屏幕上显示的一模一样的页面,需要做很多工作。而且,大多数浏览器强制在打印结果上输入URL、打印日期和页码,这并不总是理想的。这不能被改变或禁用。
相反,我建议根据屏幕上的内容创建一个PDF,并将PDF提供给下载和/或打印。虽然大多数可用的PDF库都是付费的,但也有一些免费的替代方案可用于创建基本的PDF。