CSS打印显示白色条。

huangapple go评论68阅读模式
英文:

CSS print shows white Bar

问题

这是HTML和CSS通过Python(cgi、jinja2、weasyprint)创建的PDF模板,用户可以通过网站上的按钮下载。HTML部分如下:

<body>
    <div id="ticket"></div>
</body>

CSS部分如下:

body {
    margin: 0;
    padding: 0;
    height: 82mm;
    width: 203mm;
    background: red;
}
#ticket {
    height: 82mm;
    width: 203mm;
    background-color: red;
    margin: 0 auto;
}
@page {
    size: 203mm 82mm;
    margin: 0;
    padding: 0;
}

如果遇到打印预览中出现奇怪的边框,你可以参考上述的HTML和CSS代码,看看是否有错误。

英文:

I'm trying to create an ticket width the dimensions (203mmx82mm). This is in HTML and CSS via python (cgi, jinja2, weasyprint) the template will be converted to an pdf, which the user can download via a button on the website.

The HTML Body:

&lt;body&gt;
    &lt;div id=&quot;ticket&quot;&gt;&lt;/div&gt;
&lt;/body&gt;

And my CSS:

body{
    margin: 0;
    padding: 0;
    height: 82mm;
    width: 203mm;
    background: red;
}
#ticket{
    height: 82mm;
    width: 203mm;
    background-color: red;
    margin: 0 auto;
}
@page{
    size: 203mm 82mm;
    margin: 0;
    padding: 0;
}

I've chosen red to see it better. Even when the Body has the same color as the ticket, I still get an odd border in the printpreview (see Image blow)
CSS打印显示白色条。

The white bar is also seen on the actual pdf.
Can anyone help me?

答案1

得分: 0

以下是翻译好的内容:

默认页面边距通过将@page规则的margin属性设置为0来移除。类似于此,要移除body元素的默认边距,需要将body规则的margin属性设置为0。通过这样做,您应该确保您的票据填满整个页面,白色的边栏消失了。

此外,您可以考虑将#ticket元素的box-sizing属性设置为border-box,这会将填充和边框包括在元素的总宽度和高度中,因此您不必担心它们影响票据的整体尺寸。以下是您的CSS代码,带有以下更改:

@page {
  margin: 0;
}

body {
  margin: 0;
  padding: 0;
  height: 82 mm;
  width: 203 mm;
  background: red;
}

#ticket {
  height: 82 mm;
  width: 203 mm;
  background-color: red;
  margin: 0 auto;
  box-sizing: border-box;
}

希望这对您有帮助。

英文:

The default margins on the page are removed by setting the margin attribute of the @page rule to 0. Similar to this, removing the default margins from the body element requires setting the margin property of the body rule to 0. By doing this, you should guarantee that your ticket fills the whole page and that the white bar is gone.

Additionally, you can think about setting the #ticket element's box-sizing attribute to border-box, which includes the padding and border in the element's total width and height so you won't have to worry about them influencing the ticket's overall dimensions. Here is your CSS changed with the following changes:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

@page {
  margin: 0;
}

body {
  margin: 0;
  padding: 0;
  height: 82 mm;
  width: 203 mm;
  background: red;
}

#ticket {
  height: 82 mm;
  width: 203 mm;
  background color: red;
  margin: 0 auto;
  box-sizing: border-box;
}

<!-- end snippet -->

答案2

得分: 0

在删除以下内容后:

@page{
    size: 203mm 82mm;
    margin: 0;
    padding: 0;
}

并重新启动计算机后,我重新插入了这段代码。现在它可以正常工作。

英文:

After deleting the

@page{
    size: 203mm 82mm;
    margin: 0;
    padding: 0;
}

and a pc restart i inserted agian the snippet. It works now.

huangapple
  • 本文由 发表于 2023年5月13日 23:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定