- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Python 2.7.6 和 Django 1.5.5。如何在 SimpleDocTemplate 中写入一行?
我正在尝试这个:
@login_required
def report(request):
rep = Report(request.user.username + "_cities.pdf")
# Title
rep.add_header("Cities")
line = Line(0, 100, 500, 100)
rep.add(line)
# Body
columns = ("City")
cities = [(p.name) for p in City.objects.all()]
table = Table([columns] + cities, style=GRID_STYLE)
table.hAlign = "LEFT"
table.setStyle([('BACKGROUND', (1, 1), (-2, -2), colors.lightgrey)])
rep.add(table)
rep.build()
return rep.response
Line()
是 from reportlab.graphics.shapes import Line
。Report 类只是 SimpleDocTemplate 的包装类:
class Report:
styles = None
response = None
document = None
elements = []
def __init__(self, report_file_name):
self.styles = styles.getSampleStyleSheet()
self.styles.add(ParagraphStyle(name='Title2',
fontName="Helvetica",
fontSize=12,
leading=14,
spaceBefore=12,
spaceAfter=6,
alignment=TA_CENTER),
alias='title2')
self.response = HttpResponse(mimetype="application/pdf")
self.response["Content-Disposition"] = "attachment; filename=" + report_file_name
self.document = SimpleDocTemplate(self.response, topMargin=5, leftMargin=2, rightMargin=1, bottomMargin=1)
self.document.pagesize = portrait(A4)
return
def add_header(self, header_text):
p = Paragraph(header_text, self.styles['Title2'])
self.elements.append(p)
def add(self, paragraph):
self.elements.append(paragraph)
def build(self):
self.document.build(self.elements)
当我调用报告函数时,我收到错误消息:
Line instance has no attribute 'getKeepWithNext'
当我使用 Line()
删除/注释这些行时,不会出现错误。
你能帮我吗?那一行怎么写?
最佳答案
仅将 Line
添加到元素列表是行不通的:您只能将 Flowable
传递给 SimpleDocTemplate.build()
。但您可以将其包装在 Drawing
中,它是一个 Flowable
:
d = Drawing(100, 1)
d.add(Line(0, 0, 100, 0))
rep.add(d)
关于Python 与 ReportLab。如何用 SimpleDocTemplate 写一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763291/
您好,我是报告实验室的新手。我想生成一份包含表格的 pdf 报告。表格中的某些列文本大于列宽,现在我想根据列宽换行文本。 以下是我编写的代码 # Imports from reportlab.pdfg
我正在使用 Reportlab SimpleDocTemplate 创建一个 pdf 文件。我必须按行写入(绘制)多个图像,以便我可以调整文件中的多个图像。 class PrintBarCodes(V
我正在使用 ReportLab 生成 PDF 报告。 我使用了 SimpleDocTemplate 并设置了 pageSize = A5。但是在构建PDF后,当我打开创建的文件时,页面大小为A4。 d
我有一个 django 应用程序,它当前使用用户可以下载的 Canvas 生成 pdf。我创建了一个 StringIO 缓冲区,做一些事情然后发送调用 response.write。 # Set up
有人知道当我使用 SimpleDocTemplate() 制作 PDF 文档时是否可以删除默认间距。 我希望它从一个角打印到另一个角。 response = HttpResponse(mimet
有人知道当我使用 SimpleDocTemplate() 制作 PDF 文档时是否可以删除默认间距。 我希望它从一个角打印到另一个角。 response = HttpResponse(mimet
我正在使用 Python 2.7.6 和 Django 1.5.5。如何在 SimpleDocTemplate 中写入一行? 我正在尝试这个: @login_required def report(r
我正在生成一个 PDF,但我无法为不同的页面提供不同的上边距。 有没有什么方法或者途径可以解决这个问题? response = HttpResponse(mimetype='application/p
//这里使用simpledoctemplate创建pdf。我无法将图像带到页面顶部。请给出解决方案 def report_creator(): styles = getSampleStyleS
我正在使用 django 并在 this example 之后生成报告, 我需要生成最后一页但没有页眉或页脚和不同的内容。 我正在尝试这样做: def print_example(self):
我是一名优秀的程序员,十分优秀!