我正在尝试创建一个标题,该标题将出现在我使用Reportlab生成的PDF文档的每个页面上。 我想要实现的是拥有一个由表组成的标题。
我有一个方法可以像这样创建标题:
def make_header(self, canvas, doc):
small_size = '8'
big_size= '10'
paragraphs = {}
canvas.saveState()
for key, name in (('doc_type', 'Document type'), ('owner', 'Owner'),
('classification', 'Classification'),
('document_nr', 'Document Number'),
('date', 'Date'),
('revision', 'Revision'),
('title', 'Title')):
t = self.header_info[key] if self.header_info[key] else ''
paragraphs[key] = [Paragraph('<font size={}>{}</font>'.format(small_size, name), style=styles['Normal']),
Paragraph('<font size={}>{}</font>'.format(big_size, t), style=styles['Normal'])]
im = self.scale_image("image.jpg", 10*cm)
page = [Paragraph('<font size={}>{}</font>'.format(small_size, 'Page'), style=styles['Normal']),
Paragraph('<font size={}>{}</font>'.format(big_size, doc.page), style=styles['Normal'])]
t = Table([[im, paragraphs['doc_type'], paragraphs['owner'], paragraphs['date']],
['', paragraphs['classification'], paragraphs['document_nr'], paragraphs['revision']],
['', paragraphs['title'],'', page]])
t.setStyle([('SPAN', (0,0), (0,2)),
('SPAN', (1,2), (2,2)),
('ALIGN', (0,0), (0,0), 'CENTER'),
('VALIGN', (0,0), (0,0), 'MIDDLE'),
('VALIGN', (1,0), (-1,-1), 'TOP')])
self.table_grid(t)
t.wrapOn(canvas, self.width-50, self.height)
t.drawOn(canvas, *self.coord(10,10, mm))
canvas.restoreState()
然后执行以下操作:
def build(self):
t = Paragraph('test', style=styles['Normal'])
self.doc.build([t], onFirstPage=self.make_header)
我文档中唯一显示的是“test”字符串,但不是标题。 我已经通过在make_header函数中使用print进行了测试,因此我知道它被调用并运行。
我找不到什么遗失,所以请帮助我朝正确的方向发展。
答案 0 :(得分:0)
您好,您的问题与缺少的元素有点混淆。我认为问题在于你如何使用wrapOn和drawOn。这是我的黑客版本,至少做了一些事情;有关reportlab的更多帮助和讨论,您可以在用户列表https://pairlist2.pair.net/mailman/listinfo/reportlab-users
中询问from reportlab.platypus import Spacer, SimpleDocTemplate, Table, TableStyle
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import inch
styles = getSampleStyleSheet()
class Test:
header_info = dict(
doc_type='MMMMM',
)
def make_header(self, canvas, doc):
small_size = '8'
big_size= '10'
paragraphs = {}
canvas.saveState()
pageWidth, pageHeight = canvas._pagesize
for key, name in (('doc_type', 'Document type'), ('owner', 'Owner'),
('classification', 'Classification'),
('document_nr', 'Document Number'),
('date', 'Date'),
('revision', 'Revision'),
('title', 'Title')):
t = self.header_info.get(key,'')
paragraphs[key] = [Paragraph('<font size={}>{}</font>'.format(small_size, name), style=styles['Normal']),
Paragraph('<font size={}>{}</font>'.format(big_size, t), style=styles['Normal'])]
#im = self.scale_image("image.jpg", 10*cm)
page = [Paragraph('<font size={}>{}</font>'.format(small_size, 'Page'), style=styles['Normal']),
Paragraph('<font size={}>{}</font>'.format(big_size, doc.page), style=styles['Normal'])]
t = Table([['im', paragraphs['doc_type'], paragraphs['owner'], paragraphs['date']],
['', paragraphs['classification'], paragraphs['document_nr'], paragraphs['revision']],
['', paragraphs['title'],'', page]],
style=[('SPAN', (0,0), (0,2)),
('SPAN', (1,2), (2,2)),
('ALIGN', (0,0), (0,0), 'CENTER'),
('VALIGN', (0,0), (0,0), 'MIDDLE'),
('VALIGN', (1,0), (-1,-1), 'TOP'),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 2, colors.black),
])
#self.table_grid(t)
w,h = t.wrapOn(canvas, pageWidth-20, pageHeight-20)
t.drawOn(canvas, 10, pageHeight-h-10)
canvas.restoreState()
def __init__(self):
self.doc = SimpleDocTemplate('stackoverflow-48184260.pdf')
def build(self):
t = Paragraph('test', style=styles['Normal'])
self.doc.build([t], onFirstPage=self.make_header)
if __name__=='__main__':
test = Test()
test.build()