Rails:WickedPDF:分页符

发布于 2024-11-03 19:25:37 字数 336 浏览 6 评论 0原文

在我的 Ruby (1.9.2) Rails (3.0.x) 中,我想使用 wicked_pdf 将网页呈现为 PDF,并且我想控制分页符的位置。我控制分页符的 CSS 代码如下:

<style>
  @media print
  {
    h1 {page-break-before:always}
  }
</style>

但是,当我使用 wicked_pdf 渲染页面时,它不会将文档分成两页。我还必须做些什么才能使用 wicked_pdf 进行分页吗?

In my Ruby (1.9.2) Rails (3.0.x) I would like to render a web page as PDF using wicked_pdf, and I would like to control where the page breaks are. My CSS code to control page breaks is as follows:

<style>
  @media print
  {
    h1 {page-break-before:always}
  }
</style>

However, when I render the page with wicked_pdf, it does not separate the document into two pages. Is there something else that I must do to get page breaks with wicked_pdf?

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

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

发布评论

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

评论(6

柠檬心 2024-11-10 19:25:37

由于某种原因,“@media print”并没有完全做到这一点。我只是采用了

.page-break { display:block; clear:both; page-break-after:always; }

CSS 规则,然后我将以下内容粘贴在页面中以进行分页:

<div class="page-break"></div>

这确实有效。

For some reason, the "@media print" didn't quite do it. I just went with

.page-break { display:block; clear:both; page-break-after:always; }

for the CSS rule, and then I stuck the following in my page for a page break:

<div class="page-break"></div>

That just worked.

命比纸薄 2024-11-10 19:25:37

尝试了杰伊的解决方案,但无法让它工作(可能与其他CSS冲突)

我让它与此一起工作:

<p style='page-break-after:always;'></p>

Tried Jay's solution but could not get it to work (maybe a conflict with other css)

I got it to work with this:

<p style='page-break-after:always;'></p>
花伊自在美 2024-11-10 19:25:37

我遇到了同样的问题,我发现了一些可能有帮助的东西。这是我的分页 CSS 代码:

.page-break {
  display: block;
  clear: both;
  page-break-after: always;
}

由于两个原因,这不起作用:

I。在其中一个 SASS 导入文件中,我有这样一行代码:

html, 正文
  溢出-x:隐藏!重要

二.另一个问题是 bootstrap ,

@import "bootstrap"

看起来是因为 float: left 中的:

.col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 {
  float: left;
}

分页符不再起作用。因此,只需导入 bootstrap 之后添加此内容即可。

.col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 {
  float: initial !important;
}

I had the same problem and I discovered something that might help. This was my page break CSS code:

.page-break {
  display: block;
  clear: both;
  page-break-after: always;
}

This didn't work because of TWO reasons:

I. In one of the SASS imported file I had this line of code:

html, body
  overflow-x: hidden !important

II. The other problem was bootstrap

@import "bootstrap"

It looks like because of the float: left in:

.col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 {
  float: left;
}

the page break is no longer working. So, just add this after you import bootstrap.

.col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 {
  float: initial !important;
}
Smile简单爱 2024-11-10 19:25:37

这些解决方案都不适合我。我确实找到了一个适用于许多人的 gem 问题的解决方案 - https://github。 com/wkhtmltopdf/wkhtmltopdf/issues/1524

您想要在需要分页符的元素上添加 CSS。就我而言,它是表行,所以我添加了:

tr {
 page-break-inside: avoid;
}

None of these solutions worked for me. I did find a solution that worked for many people in the gem issues here - https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1524

you want to add CSS on the element that needs the page break. In my case, it was table rows, so I added:

tr {
 page-break-inside: avoid;
}
入怼 2024-11-10 19:25:37

如果您使用外部样式表,请确保样式表链接标记包含 media='print" 或 media='all':

<%= stylesheet_link_tag 'retailers_pdf',media: 'all' %>

否则

<%= stylesheet_link_tag 'retailers_pdf',media: 'print' %>

wicked_pdf 将不会拾取它。

另请注意,如果您位于表格或 div 的中间带有边框,分页属性将不起作用,在这种情况下,是时候打破 jQuery 并开始分割这个答案:https://stackoverflow.com/a/13394466/2016616 有一个很好的位置测量片段,我正在研究干净且可重复的位置测量。表格分割代​​码,完成后将发布它。

Make sure that the stylesheet link tag includes media='print" or media='all' if you are using an external stylesheet:

<%= stylesheet_link_tag 'retailers_pdf',media: 'all' %>

or

<%= stylesheet_link_tag 'retailers_pdf',media: 'print' %>

otherwise wicked_pdf will not pick it up.

Also note that if you are in the middle of a table or div with a border, that the page-break attributes will not work. In this case, it's time to break out jQuery and start splitting things up. This answer: https://stackoverflow.com/a/13394466/2016616 has a good snippet for position measurement. I am working on clean and repeatable table-splitting code and will post it when I have finished it.

蝶…霜飞 2024-11-10 19:25:37

我遇到了同样的问题,最终对我有用的是确保我的元素

page-break: always;

位于文档的根目录上,似乎当它嵌套在其他元素内部时,尤其是分配了高度的元素时,声明会被忽略。

希望这对某人有帮助。

i had the same issue and what ended up working for me was to make sure that my element with

page-break: always;

was on the root of the document, seems when its nested inside of other elements, especially ones with height assigned, the declaration gets ignored.

hope this helps someone.

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