在reportlab中,将尽可能多的空行添加到长表中,以适合最后一页

发布于 2024-11-14 18:15:09 字数 295 浏览 8 评论 0原文

我正在准备一份项目列表的 PDF 报告,它本质上是一个长表,其中一些单元格填充了数据,一些单元格为空。该报告将被打印出来,用户将填写纸上的空白单元格。她还将添加新行并填写新数据。

所以我将提供一些(例如3)空行作为网格来填充数据。但如果页面上仍有空间,我想用空行填充它。

如何在最后一页上添加尽可能多的行?

不同的方法:
当使用每行带有单行表的段落时,也许这种行为更容易实现。但我需要在每个页面上重复第一行(这在带有 repeatRows=1 的表格中很容易)。

有什么想法吗?

I'm preparing a PDF report of a list of items, which essentially is a long table, with some cells filled with data and some cells empty. This report will be printed out and the user will fill the empty cells on paper. She will also add new rows and fill in new data.

So I will provide some (e.g. 3) empty rows as a grid to fill in data. But if there is still room left on the page, I'd like to fill it with empty rows.

How can I add as many rows as fit on the last page?

Different approach:
Maybe this behaviour is easier achieved when using a Paragraph with a one-row-table for each row. But I need the first row repeated on every page (which is easy in tables with repeatRows=1).

Any ideas?

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

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

发布评论

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

评论(2

过期以后 2024-11-21 18:15:09

我必须去检索一些代码,但我似乎记得必须通过测量当前的 X / Y 位置来执行操作,根据我使用的任何边距进行计算,然后确定是否可以容纳更多信息或如果我需要一个新页面。我的项目是对长文本块进行自动换行,这很相似但又不太相似。我很快就会在这里更新一些代码。

def newline(self, options, text = ''):
  if getattr(self, 'lpp', None) == self.lines[self.pages]:
    self.newpage()

  if getattr(self, 'y', None) > self.h - self.bm * inch:
    self.newpage()

在本例中,我可能已设置了 lp​​p(每页行数)属性,因此我首先检查该值是否存在,如果存在,则我是否处于当前页面的行数。如果对每页的总行数没有限制,那么我会测试我的 Y 位置以及底部边距是多少。如有必要,请修补页面。这里遗漏了一些内容,但这是一个普遍的想法。

def newline(self, options, text = ''):
  if getattr(self, 'lpp', None) == self.lines[self.pages]:
    self.newpage()

  if getattr(self, 'y', None) > self.h - self.bm * inch:
    self.newpage()

  self.addLine()

  self.putText(self.x, self.h - self.y, text)

def putText(self, x, y, text):
  # If we actually place some text then we want to record that.
  if len(text.strip()) > 0 and not self.hasText[self.pages]:
    self.hasText[self.pages] = True
  # Something here to handle word wrap.
  if self.wrap:
    lines = self._breakScan(text)

    if len(lines) > 1:
      self.c.drawString(x, y, lines[0])

      self.newline('', ' '.join(lines[1:]))
    elif lines:
      self.c.drawString(x, y, lines[0])
  else:
    self.c.drawString(x, y, text)

在这里,self.c 是我的画布。我会跟踪我在页面上写了多少行,因为有时我们会重新包装可能包含分页符的文档,所有这些都在我们的自定义标记中。

I'll have to go retrieve some of my code, but I seem to recall having to do things by measuring the current X / Y Position, calculate that against whatever margin I was using, and then determine whether or not more information could fit or if I needed a new page. My project was word wrapping long blocks of text, which is similar but not quite analogous. I'll update with some code here shortly.

def newline(self, options, text = ''):
  if getattr(self, 'lpp', None) == self.lines[self.pages]:
    self.newpage()

  if getattr(self, 'y', None) > self.h - self.bm * inch:
    self.newpage()

In this case I had attributes for lpp (Lines Per Page) which may have been set, so I first checked to see if that value existed, and if so, if I was at the line count for the current page. If there was no restriction on total lines per page then I tested for what my Y position would be and what the bottom margin was. If necessary, patch in a page. There's some left out here, but that's a general idea.

def newline(self, options, text = ''):
  if getattr(self, 'lpp', None) == self.lines[self.pages]:
    self.newpage()

  if getattr(self, 'y', None) > self.h - self.bm * inch:
    self.newpage()

  self.addLine()

  self.putText(self.x, self.h - self.y, text)

def putText(self, x, y, text):
  # If we actually place some text then we want to record that.
  if len(text.strip()) > 0 and not self.hasText[self.pages]:
    self.hasText[self.pages] = True
  # Something here to handle word wrap.
  if self.wrap:
    lines = self._breakScan(text)

    if len(lines) > 1:
      self.c.drawString(x, y, lines[0])

      self.newline('', ' '.join(lines[1:]))
    elif lines:
      self.c.drawString(x, y, lines[0])
  else:
    self.c.drawString(x, y, text)

Here, self.c is my canvas. I'm keeping track of how many lines I've put down on the page because there are times where we're re-wrapping a document that may contain page breaks, all in our custom markup.

谁对谁错谁最难过 2024-11-21 18:15:09

根据该表格在文档中的结束位置,一个快速、简单的解决方案可能是在表格中粘贴足够的行,以确保它填满页面并溢出到下一页。然后,在构建文档后,剪掉最后一页并创建一个仅缺少该页面的新 PDF。

当然,这只适用于表格位于文档末尾的情况(或者,如果您使用的是 ReportLab 的商业版本,您可以将 PDF 拼接在一起,在这种情况下这并不重要),而且它有点像正如我所说,丑陋,但它完成了工作..

Depending on where this table ends in the document, a quick, hacky solution might be to just stick enough rows in the table to make sure it fills the page and spills over onto the next page. Then, after building the document, cut off the last page and create a new PDF just missing that page.

Of course, this only works if the table is at the end of the document (or, if you're using the commercial version of ReportLab, you could stitch together PDFs, in which case it wouldn't matter), and it's kind of ugly like I said, but it gets the job done..

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