是否有可能使一个HTML页面的行为,例如,像一个a4大小的页面在MS Word?

实际上,我希望能够在浏览器中显示HTML页面,并以A4大小页面的尺寸概述内容。

为了简单起见,我假设HTML页面只包含文本(没有图像等),例如,不会有<br>标记。

另外,当HTML页面被打印出来时,它将显示为a4大小的纸质页面。


当前回答

I know that this subject is quite old but I want to share my experience about that topic. Actually, I was searching a module that can help me with my daily reports. I'm writting some documentations and some reports in HTML + CSS (instead of Word, Latex, OO, ...). The objectif would be to print them on A4 paper to share it with friends, ... Instead of searching, I decided to have a small funny coding session to implement a simple lib that can handle "pages", page number, summary, header, footer, .... Finally, I did it in ~~2h and I know that's not the best tool ever but it's almost ok for my purpose. You can take a look at this project on my repo and don't hesitate to share your ideas. It's maybe not what you are searching for at 100% but I think that this module can help you.

基本上我创建了一个页面的主体“宽度:200mm;”和容器的高度:290mm(小于A4)。然后我使用了page-break-after: always;因此,浏览器的“打印”选项知道何时拆分页面。

回购:https://github.com/kursion/jsprint

其他回答

I know that this subject is quite old but I want to share my experience about that topic. Actually, I was searching a module that can help me with my daily reports. I'm writting some documentations and some reports in HTML + CSS (instead of Word, Latex, OO, ...). The objectif would be to print them on A4 paper to share it with friends, ... Instead of searching, I decided to have a small funny coding session to implement a simple lib that can handle "pages", page number, summary, header, footer, .... Finally, I did it in ~~2h and I know that's not the best tool ever but it's almost ok for my purpose. You can take a look at this project on my repo and don't hesitate to share your ideas. It's maybe not what you are searching for at 100% but I think that this module can help you.

基本上我创建了一个页面的主体“宽度:200mm;”和容器的高度:290mm(小于A4)。然后我使用了page-break-after: always;因此,浏览器的“打印”选项知道何时拆分页面。

回购:https://github.com/kursion/jsprint

为每一页创建节,并使用下面的代码来调整页边距、高度和宽度。

如果你打印的是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;
}

寻找类似的问题,我发现这个- http://www.indigorose.com/forums/archive/index.php/t-13334.html

A4是一种文档格式,作为屏幕图像,这取决于图像的分辨率,例如A4文档调整为:

72 dpi (web) = 595 X 842像素 300 dpi(打印)= 2480 X 3508像素 (这是我所知道的“A4”。210mm X 297mm @ 300 dpi) 600 dpi (打印)= 4960 X 7016像素

我在谷歌上搜索“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)。

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

在你的normal style.css中:

table.tableclassname {
  width: 400px;
}

在你的print.css中:

table.tableclassname {
  width: 16 cm;
}