【问题标题】:Reportlab pdf layout guideReportlab pdf 布局指南
【发布时间】:2014-10-21 22:03:10
【问题描述】:

我想创建一个用于打印抽认卡的 pdf - 类似于:

  • 第 1 页 - 标题 - 大文本水平居中,徽标略低于水平居中。
  • 第 2 页 - 卡片 1 的正面,图像水平居中,垂直居中略高于中心,图像下方的小文本(如标题)。
  • 第 3 页 - 卡片 1 的背面,大文本在页面上水平和垂直居中
  • 第4页(与第2页相同,内容不同)
  • 第5页(与第3页相同,内容不同)
  • 以此类推到卡片的末尾。
  • 我可能还会在每张卡片上打印静态页脚内容

我已经知道如何嵌入字体、创建自定义页面大小和其他一些内容,但我对段落、框架、表格、样式等感到不知所措。

什么是处理这种布局的pythonic方法?或者什么是有效的结构?

编辑: 正如 GDDC 评论的那样,这个问题太宽泛了——这正是我的问题——作为报告实验室的初学者,我对处理看似简单的布局的方法感到不知所措。有人可以建议使用 PLATYPUS 的良好结构吗?桌子?段落?相框? ???

谢谢。

【问题讨论】:

  • 我承认这不是关于 SO 的最大问题,但我无法找到可以提供帮助的文档。
  • 实际上有数百种方法可以解决这个问题。一个封装了reportlab 功能、辅助函数、模块,甚至只是一些平面函数的类,您将文档实例传递到其中以处理按顺序添加的片段。这对于目前形式的有意义的答案来说太宽泛了。

标签: python reportlab


【解决方案1】:

这是我的解决方案的大纲,代码在底部:

第 1 页 - 标题 - 大文本水平居中,徽标稍低且水平居中:(注意:在代码中位置颠倒了 - 徽标在前。)

  • 文本段落。
  • 徽标的图像对象。
  • 将这两个项目插入到一个包含两行一列的表格对象中。
  • 将表格绘制到第 1 页
  • 使用 showPage() 移动到第二页

第 2 页 - 卡片 1 的正面,图像水平居中,垂直居中略高于中心,图像下方的小文本(如标题)。

  • 与第 1 页相同,使用适当的内容。

第 3 页 - 卡片 1 的背面,大文本在页面上水平和垂直居中

  • 文本段落
  • 在 1 行 1 列的表格中插入段落
  • 将表格插入框架

第4页(与第2页相同,内容不同)

第 5 页(与第 3 页相同,内容不同) - 根据需要重复。

以下是在 Django 项目中使用的 - 视图位于底部。

class fcMaker(object):
""""""
def __init__(self, response):
    self.PAGE_SIZE = (2.75*inch, 3.75*inch)
    self.c = canvas.Canvas(response, pagesize=self.PAGE_SIZE)
    self.styles = style
    self.width, self.height = self.PAGE_SIZE

def createDocument(self):
    """"""
    # Title Page
    title = """Title goes here"""
    p = Paragraph(title, styleH)

    logo = Image("my_cool_logo.jpg")
    logo.drawHeight = 99
    logo.drawWidth = 99

    data = [[logo], [p]]
    table = Table(data, colWidths=2.25*inch)
    table.setStyle([("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
                    ("ALIGN", (0, 0), (-1, -1), "CENTER"),
                    ("TOPPADDING", (0, 0), (-1, -1), 20)])
    table.wrapOn(self.c, self.width, self.height)
    table.drawOn(self.c, *self.coord(.25, 2.75, inch))

    self.c.showPage()

    #Page Two
    side1_text = """Text goes here"""
    p = Paragraph(side1_text, styleF)

    side1_image = Image("first_image.jpg")
    side1_image.drawHeight = 99
    side1_image.drawWidth = 99

    data = [[side1_image], [p]]
    table = Table(data, colWidths=2.25*inch)
    table.setStyle([("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
                    ("ALIGN", (0, 0), (-1, -1), "CENTER"),
                    ("TOPPADDING", (0, 0), (-1, -1), 3)])
    table.wrapOn(self.c, self.width, self.height)
    table.drawOn(self.c, *self.coord(.25, 2.75, inch))

    self.c.showPage()

    #Page Three
    side2_text = """<font size = '14'>This is where and how the main text will appear on the rear of this card.
    </font>"""
    p_side2 = Paragraph(side2_text, styleH)
    data = [[p_side2]]
    table_side2 = Table(data, colWidths=2.25*inch, rowHeights=2.55*inch)
    table_side2.setStyle([("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
                    ("ALIGN", (0, 0), (-1, -1), "CENTER"),
                    ("TOPPADDING", (0, 0), (-1, -1), 3),
                    ("BOX", (0, 0), (-1,-1), 0.25, colors.red)])
    front_page = []
    front_page.append(table_side2)

    f = Frame(inch*.25, inch*.5, self.width-.5*inch, self.height-1*inch, showBoundary=1)
    f.addFromList(front_page, self.c)

def coord(self, x, y, unit=1):
    """
    Helper class to help position flowables in Canvas objects
    """
    x, y = x * unit, self.height -  y * unit
    return x, y

def savePDF(self):
    """"""
    self.c.save()

def fc_maker_view(request):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="pdf1.pdf"'
doc = fcMaker(response)
doc.createDocument()
doc.savePDF()
return response

向 Mike Driscoll 致敬,他的博客文章让我开始了,他与我分享了一些技巧。 http://www.blog.pythonlibrary.org/2012/06/27/reportlab-mixing-fixed-content-and-flowables/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2020-10-13
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多