【问题标题】:Print background image on every page once在每页上打印一次背景图像
【发布时间】:2013-11-19 05:14:35
【问题描述】:

当我打印大 html 文件时,我需要在每个页面上打印一次背景图像。现在它只打印在第一页上。 所以css的部分是:

@media all {
    body
    {
        text-align:left;
        background-image:url('/C:/logo.png');
        background-repeat:no-repeat;
        background-position:right top;
    }
}

【问题讨论】:

  • 如果您不控制执行打印的设备,您将遇到的一个问题是,默认情况下,大多数浏览器不打印背景图像和颜色,您必须手动打开它.
  • @MyltchyChin 我已经控制了设备,我将自己从 html 打印成 pdf - 这是一个很大的 html 报告,我需要在报告的每一页上都有一个徽标。

标签: html css printing


【解决方案1】:

我找到了一种在 chrome 上打印背景的方法,方法是创建一个 divposition: fixed 作为背景。当background-attachment: fixed 不起作用时,我有了这个想法,但这让我想到了position: fixed div
这样,即使在 chrome 上,背景也会完全打印在每一页上。

https://stackblitz.com/edit/web-platform-vlfqfz?file=index.html

HTML:

<body id="print-layout">
    <div class="bg-container"></div>
    <div class="content">
      <h1>Hello there!</h1>
      Long content...
    </div>
</body>

CSS

body {
  width: 100%;
  height: 100%;
  margin: 0;
}


.bg-container {
  position: fixed;
  height: 100%;
  width: 100%;
  z-index: 0;
  background-image: url(https://imgur.com/cjmB60j.jpg);
  background-size: 100% 100%;
}

.content {
  position: relative;
  z-index: 1;
  padding: 140px 55px;
}

【讨论】:

    【解决方案2】:

    如果您将 background-attachment 属性指定为固定,它会在每个页面上呈现。这种方法的唯一问题是内容可以剪辑在它上面(而且它似乎只在 FireFox 中工作)。

    <style type="text/css" media="print">
        body
        {
            background-image:url('/C:/logo.png');
            background-repeat:no-repeat;
            background-position: right top;
            background-attachment:fixed;
        }
    </style>
    

    另一个选项是让您的背景图像共享您的可打印区域的比例(即,字母大小 8.5x11 的纸张,四边有 0.5 英寸的边距是 7.5:10)并将徽标放在空白字段中(例如http://i.imgur.com/yvVW2mk.png)。然后您将图像设置为垂直重复并且大小为 100%。

    <style type="text/css" media="print">
        body
        {
            background-image:url('/C:/whitespace-logo.png');
            background-repeat:repeat-y;
            background-position: right top;
            background-attachment:fixed;
            background-size:100%;
        }
    </style>
    

    【讨论】:

    • 这在 FireFox 中工作得很好,但在 Chrome 中不起作用……有什么办法让它在 Chrome 中起作用?我的意思是印刷部分。
    • 有没有办法在 chrome 浏览器的每一页上打印图像?因为我将我的 html 转换为 pdf,当我通过 foxitreader 在 Firefox 中执行此操作时,mozila 会禁用 pdf 中的所有链接,当我从 chrome 打印到 pdf 时它工作得很好……但它不打印徽标……我真的需要你对这个问题的帮助......
    • @Donvino - 我给的第二个选项在 Chrome 中工作
    • 能否请您显示它在 chrome 中工作的所有 html 文件? page-break:after 参数可能会以某种方式影响它吗?
    • @Donvino - 大小无关紧要,因为 background-size 属性告诉它拉伸。高宽比必须与打印区域相匹配。因此,如果您有 8.5x11 的字母股票,边距为 0.25,则为 8x10.5,如果边距为 0.5,则为 7.5x10。
    【解决方案3】:

    确保在所有页面上都包含 CSS 文件。

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

    【讨论】:

    • 我有 1 个包含 css 的大 html。
    • 那它是怎么分页的呢?
    • 我认为 OP 有 1 个 HTML 页面,但是在打印时,OP 会从打印机中滚出多个页面。 OP 希望在所有这些页面上都有背景图片。
    • @Banananana - 他说的是当您打印一个 HTML 文档并且它跨越多个页面时。
    • 哦,打印,就像在纸上一样……我的错
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多