CSS 打印布局 - 在单页上打印

发布于 2024-12-19 10:22:37 字数 1389 浏览 1 评论 0原文

我陷入了困境,SO 档案对我没有帮助。也许我找错地方了。这是一个小故事:

  • 我有一个视图需要打印在一个整页上。我不能有第二页,我需要它在页面上尽可能大。
  • 解决方案必须具有合理的跨浏览器兼容性(IE9+、Safari、Chrome、FF)。
  • 我已经有了一个 PDF 解决方案,但我还需要一个普通的打印解决方案。
  • 该页面是用 Bootstrap 构建的,但我已经覆盖了大多数打印类。

HTML 页面的结构如下。我添加了一些内联 CSS 来自定义其中一些信息。

<div class="container">
  <div class="row">
    <div class="span16">

      <style type="text/css" media="all">
        @media print {
          html, body {
            margin: 0;
            padding: 0;
            background: #FFF; 
            font-size: 9.5pt;
          }
          .container, .container div {
            width: 100%;
            margin: 0;
            padding: 0;
          }
          .template { overflow: hidden; }
          img { width: 100%; }
        }
      </style>

      <div class="template_holder">
        <div class="template">
          <img src="some_big_image">
          <div>
            [PLAIN TEXT IN HERE, POSITION:ABSOLUTE OVER THE IMAGE]
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

我意识到在这里包含内联 CSS 的形式有点糟糕,但由于各种原因它必须覆盖它之前的一堆其他 CSS。我可以把它拉回来,但要点是这样的。当我打印时,我得到看起来正确的东西,再加上额外的第二页。当我缩小图像时,一切正常,但我需要图像来填充 DIV。

我认为将宽度设置为 100% 是问题所在,但我确保图像长宽比小于页面(即使有任何边距)。基本上,全宽图像不应导致分页。我做错了什么?我需要改变什么?任何帮助表示赞赏...

I'm badly stuck and the SO archives aren't helping me. Maybe I'm looking in the wrong place. Here's the short story:

  • I've got a view that I need to get printed on a single full page. I can't have a second page and I need it to be as large as possible on the page.
  • Solution has to be reasonably cross-browser compatible (IE9+, Safari, Chrome, FF).
  • I already have a PDF solution, but I need a plain vanilla print solution as well.
  • The page is built with Bootstrap, but I've overridden most of the classes for Print.

The structure of the HTML page is as follows. I dropped in some in-line CSS in order to customize some of this information.

<div class="container">
  <div class="row">
    <div class="span16">

      <style type="text/css" media="all">
        @media print {
          html, body {
            margin: 0;
            padding: 0;
            background: #FFF; 
            font-size: 9.5pt;
          }
          .container, .container div {
            width: 100%;
            margin: 0;
            padding: 0;
          }
          .template { overflow: hidden; }
          img { width: 100%; }
        }
      </style>

      <div class="template_holder">
        <div class="template">
          <img src="some_big_image">
          <div>
            [PLAIN TEXT IN HERE, POSITION:ABSOLUTE OVER THE IMAGE]
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

I realize it's somewhat poor form to include in-line CSS here, but it has to override a bunch of other CSS that came before it for various reasons. I could pull it back out, but the gist of it is this. When I print, I get something that looks right, plus an extra second page. When I shrink the image down, everything is ok, but I need the image to fill the DIV.

I thought setting width to 100% was the issue, but I made sure the image aspect ratio was smaller than the page (even with any margins). Basically, the image at full width should not cause a page break. What am I doing wrong & what do I need to change? Any help is appreciated...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞别 2024-12-26 10:22:37

我认为 CSS 的这个细节令人沮丧的是,答案必须适合我自己的项目。感谢@blahdiblah 和其他人的建议。混合解决方案带来了近乎完美的结果,尽管 IE 当然仍然给我带来了问题......

这与所有填充/边距样式的硬重置以及大量 !important 标记有关用于填充、宽度、高度等。基本上我用高度填充页面,然后放入 100% 宽的对象。结果是 8.5x11 页面上的最大覆盖范围,而到第二页的溢出为零。

@media print {
  * { margin: 0 !important; padding: 0 !important; }
  #controls, .footer, .footerarea{ display: none; }
  html, body {
    /*changing width to 100% causes huge overflow and wrap*/
    height:100%; 
    overflow: hidden;
    background: #FFF; 
    font-size: 9.5pt;
  }

  .template { width: auto; left:0; top:0; }
  img { width:100%; }
  li { margin: 0 0 10px 20px !important;}
}

I think the frustration with this detail of CSS is that the answer has to be tailored to my own project. Thanks to @blahdiblah and other for suggestions. A mix of solutions led to near-perfect results, though of course IE still gives me problems...

It had to do with a hard reset of all padding/margin styles and then lots of !important markers for padding, width, height, etc. Basically I filled up the page with height and then dropped in 100% wide objects. The result is maximum coverage on an 8.5x11 page with zero spillover to a second page.

@media print {
  * { margin: 0 !important; padding: 0 !important; }
  #controls, .footer, .footerarea{ display: none; }
  html, body {
    /*changing width to 100% causes huge overflow and wrap*/
    height:100%; 
    overflow: hidden;
    background: #FFF; 
    font-size: 9.5pt;
  }

  .template { width: auto; left:0; top:0; }
  img { width:100%; }
  li { margin: 0 0 10px 20px !important;}
}
薄荷港 2024-12-26 10:22:37

听起来你的图片太大了。既然图像是自定义大小的,为什么不设置

img { height: 100%; }

而不是宽度?这至少可以确保它不会溢出到第二页。

还要记住,打印机比浏览器更具可变性。您也许能够耗尽打印机上的每一寸空间,但其他人的打印边距可能会明显不同,从而毁掉您所有的辛勤工作。

It sounds like your image is just too big. Since the image is custom-sized, why not set

img { height: 100%; }

instead of width? That would at least ensure that it didn't spill over to a second page.

Bear in mind also that printers are more variable than browsers. You may be able to eke out every last inch of space on your printer, but someone else's might have significantly different print margins and ruin all your hard work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文