css 设置打印a4,CSS设置A4纸大小。

I need simulate an A4 paper in web and allow to print this page as it is show on browser (Chrome, specifically). I set the element size to 21cm x 29.7cm, but when I send to print (or print preview) it clip my page.

我需要在web上模拟A4纸,并允许在浏览器(特别是Chrome)上打印这个页面。我将元素大小设置为21cm x 29.7cm,但是当我发送到print(或print preview)时,它会剪切我的页面。

看到这个生活的例子!

HTML

HTML

Page 1/2
Page 2/2

CSS

CSS

body {

margin: 0;

padding: 0;

background-color: #FAFAFA;

font: 12pt "Tahoma";

}

* {

box-sizing: border-box;

-moz-box-sizing: border-box;

}

.page {

width: 21cm;

min-height: 29.7cm;

padding: 2cm;

margin: 1cm auto;

border: 1px #D3D3D3 solid;

border-radius: 5px;

background: white;

box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);

}

.subpage {

padding: 1cm;

border: 5px red solid;

height: 256mm;

outline: 2cm #FFEAEA solid;

}

@page {

size: A4;

margin: 0;

}

@media print {

.page {

margin: 0;

border: initial;

border-radius: initial;

width: initial;

min-height: initial;

box-shadow: initial;

background: initial;

page-break-after: always;

}

}

I think I'm forgetting something. But what would it be?

我想我忘记什么了。但是会是什么呢?

Chrome: clipping page, double page (it's just what I need it to work)

Chrome:剪切页面,双页(这正是我需要的)

Firefox: it works perfectly.

Firefox:完美的工作。

IE10: believe it or not, it's perfect!

信不信由你,它太完美了!

Opera: very buggy on print preview

Opera:在打印预览版上很麻烦。

3 个解决方案

#1

172

I looked into this a bit more and the actual problem seems to be with assigning initial to page width under the print media rule. It seems like in Chrome width: initial on the .page element results in scaling of the page content if no specific length value is defined for width on any of the parent elements (width: initial in this case resolves to width: auto ... but actually any value smaller than the size defined under the @page rule causes the same issue).

我进一步研究了这个问题,实际的问题似乎是在打印媒体规则下初始化页面宽度。在Chrome width中,如果没有为父元素的宽度定义特定的长度值(width:在本例中为initial,解析为width: auto…但实际上,任何小于@page规则下定义的大小的值都会导致相同的问题)。

So not only the content is now too long for the page (by about 2cm), but also the page padding will be slightly more than the initial 2cm and so on (it seems to render the contents under width: auto to the width of ~196mm and then scale the whole content up to the width of 210mm ~ but strangely exactly the same scaling factor is applied to contents with any width smaller than 210mm).

现在不仅内容页面太长(约2厘米),而且页面填充将略高于最初的2厘米等等(这似乎呈现内容下宽度:汽车规模~ 196毫米的宽度,然后整个内容的宽度210毫米~但奇怪的是完全相同的比例因子应用于内容与任何宽度小于210毫米)。

To fix this problem you can simply in the print media rule assign the A4 paper width and hight to html, body or directly to .page and in this case avoid the initial keyword.

要解决这个问题,只需在打印媒体规则中将A4纸张宽度和高度指定为html、正文或直接指定为.page,在本例中避免初始关键字。

@page {

size: A4;

margin: 0;

}

@media print {

html, body {

width: 210mm;

height: 297mm;

}

/* ... the rest of the rules ... */

}

This seems to keep everything else the way it is in your original CSS and fix the problem in Chrome (tested in different versions of Chrome under Windows, OS X and Ubuntu).

这似乎使其他的一切都保持在原来的CSS中,并解决了Chrome的问题(在Windows、OS X和Ubuntu下测试了不同版本的Chrome)。

#2

24

CSS

CSS

body {

background: rgb(204,204,204);

}

page[size="A4"] {

background: white;

width: 21cm;

height: 29.7cm;

display: block;

margin: 0 auto;

margin-bottom: 0.5cm;

box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);

}

@media print {

body, page[size="A4"] {

margin: 0;

box-shadow: 0;

}

}

HTML

HTML

演示

#3

15

https://github.com/cognitom/paper-css似乎解决了我所有的需求。

Paper CSS for happy printing

Front-end printing solution - previewable and live-reloadable!

前端打印解决方案-预览和实时可重载!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在HTML中定义CSS大小A4,并导出为PDF文件,可以按照以下步骤进行操作: 1. 定义HTML文档中的样式表,将页面大小设置A4大小。可以通过以下代码实现: @media print { @page { size: A4; } } 2. 将HTML文件保存,并使用浏览器打开该文件。 3. 在浏览器中按照要求调整页面布局和排版,以确保页面正常显示。 4. 在浏览器中选择打印选项,将页面导出为PDF文件。在打印设置中,可以选择大小A4,以保证输出的PDF文件也符合A4大小。 5. 点击打印按钮,完成导出PDF文件的操作。 以上就是在HTML中定义CSS大小A4,并导出为PDF文件的方法。需要注意的是,不同浏览器支持的打印设置和导出功能可能有所不同,可以根据实际需求和情况做出调整。 ### 回答2: 要在HTML中定义CSS大小A4并导出,在编写HTML和CSS代码之前,我们需要先了解A4的尺寸和默认的CSS尺寸。 A4的尺寸为210mm × 297mm,我们可以根据这个尺寸来定义CSS。 默认的CSS尺寸为像素(px),因此我们需要将A4的尺寸转换为像素。按照通常的做法,我们可以将1毫米等价于3.78像素,因此A4的尺寸将是794px × 1123px。 现在我们可以开始编写HTML和CSS代码: HTML代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Export A4 paper with CSS size</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="a4-paper"> <!-- Your content here --> </div> </body> </html> CSS代码: .a4-paper { width: 794px; height: 1123px; margin: auto; padding: 20px; background-color: #fff; box-shadow: 0 0 10px rgba(0,0,0,0.1); } 在CSS代码中,我们定义了一个类名为“.a4-paper”的div元素,它的宽度为794px,高度为1123px,使用了居中对齐和边距,并设置了背景颜色和阴影效果。 另外,在CSS代码中,我们还可以定义适合A4的字体、行距、页边距等样式。但要注意,这些样式可能因打印机和打印驱动程序的限制而被忽略或修改。 最后,在导出时,我们需要使用浏览器的打印功能,选择“标签页大小”为A4,并调整其他打印设置,例如边距、副本数等。然后,我们就可以将HTML页面导出为A4大小的PDF或质文档了。 ### 回答3: HTML是一种标记语言,用于创建网页。它可以定义文本,图片,视频等在网页上的位置和样式。而CSS作为HTML的扩展语言,也可以用来控制网页的外观和布局,比如文字大小、颜色、间距和位置等。接下来我会介绍如何定义CSS大小A4并导出。 首先,我们需要用HTML来构建一个网页。为了确保该网页的大小符合A4的标准,我们可以在HTML中设置一些参数。比如设置页面的宽度和高度为210毫米和297毫米,即A4张的标准尺寸。代码如下: ```html <!DOCTYPE html> <html> <head> <title>A4测试</title> <style type="text/css"> @page { size: A4; margin: 0; } body { width: 210mm; height: 297mm; margin: 0; padding: 0; font-size: 12pt; font-family: Arial, sans-serif; } </style> </head> <body> <p>这是一个测试页面,用于测试HTML定义CSS大小A4并导出功能。</p> </body> </html> ``` 在上面的代码中,我们使用了两个CSS属性:@page和body。@page用来定义页面的大小和边距,size属性设置大小A4,margin属性设置边距为0。而body用来定义页面主体的宽度、高度、边距和字体。这里我们把宽度和高度设置为210mm和297mm,边距为0,字体为12pt的Arial字体。 在这个基础上,我们可以使用任意一种浏览器,然后将文档导出为pdf格式。比如在Google Chrome中,我们可以点击菜单栏的“文件”按钮,然后选择“另存为pdf”选项。浏览器将会保存一个pdf文件,它的页面大小和样式就与我们在HTML中设置的一样。 综上所述,我们可以通过HTML和CSS定义A4大小,并通过任意浏览器将其导出为pdf文档。这个技巧在需要打印大量文档时非常有用,可以节省时间和张。同时,通过更加细致的CSS控制,我们可以让打印出来的文档具有更好的可读性和美感。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值