我正在使用reportlab==3.2.0
,我想知道如何能够使Table
de居中platypus
。我在 Django 的 AJAX 函数中有以下内容:
# Response
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
# Crear PDF
pdf = SimpleDocTemplate(
response,
pagesize=A4,
rightMargin=inch/2,
leftMargin=inch/2,
topMargin=inch,
bottomMargin=inch/2
)
Story = []
# Estilos
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Center', alignment=TA_CENTER))
# Cabecera
text = '''
<strong><font size=14>REPORTE DE ASISTENCIA</font></strong>
'''
Story.append(Paragraph(text, styles['Center']))
Story.append(Spacer(1, 15))
data = [(
Paragraph('<strong><font size=6>#</font></strong>', styles['Center']),
Paragraph('<strong><font size=6>DNI</font></strong>', styles['Center']),
Paragraph('<strong><font size=6>APELLIDO PATERNO</font></strong>', styles['Center']),
Paragraph('<strong><font size=6>APELLIDO MATERNO</font></strong>', styles['Center']),
Paragraph('<strong><font size=6>NOMBRES</font></strong>', styles['Center']),
Paragraph('<strong><font size=6>JORNADA</font></strong>', styles['Center']),
Paragraph('<strong><font size=6>HORA DE INGRESO</font></strong>', styles['Center']),
Paragraph('<strong><font size=6>HORA DE SALIDA</font></strong>', styles['Center']),
)]
# Registros
for counter, record in enumerate(records, 1):
nro_documento = record['nro_documento']
apellido_paterno = record['apellido_paterno']
apellido_materno = record['apellido_materno']
nombres = record['nombres']
jornada = record['fecha'].strftime('%d/%m/%Y')
fecha_hora_entrada = ''
fecha_hora_salida = ''
if record['fecha_hora_entrada']:
fecha_hora_entrada = record['fecha_hora_entrada'].strftime('%d/%m/%Y %H:%M:%S')
if record['fecha_hora_salida']:
fecha_hora_salida = record['fecha_hora_salida'].strftime('%d/%m/%Y %H:%M:%S')
data.append((
Paragraph('<font size=6>%s</font>' % counter, styles['Normal']),
Paragraph('<font size=6>%s</font>' % nro_documento, styles['Normal']),
Paragraph('<font size=6>%s</font>' % apellido_paterno, styles['Normal']),
Paragraph('<font size=6>%s</font>' % apellido_materno, styles['Normal']),
Paragraph('<font size=6>%s</font>' % nombres, styles['Normal']),
Paragraph('<font size=6>%s</font>' % jornada, styles['Normal']),
Paragraph('<font size=6>%s</font>' % fecha_hora_entrada, styles['Normal']),
Paragraph('<font size=6>%s</font>' % fecha_hora_salida, styles['Normal'])
))
table = Table(
data,
colWidths=50 # Valor del ancho de las columnas
)
table.setStyle(
TableStyle([
('VALIGN',(0, 0), (-1, -1),'MIDDLE'),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
])
)
Story.append(table)
pdf.build(Story)
正如你所看到的,我使用的是静态宽度,50
因为我找不到使表格居中的方法,这个想法是这个宽度因列的类型而异。这就是我的 PDF 的样子,左右边框上有太多可用空间,如果我增加列的宽度,那么它就会超过文档的宽度:
在您的情况下,如果您不指定列的宽度,它将为您动态计算它,我认为它也不会调整到您期望的结果。我认为最好的办法是指定每列所需的宽度,我认为这样的事情应该适合你:
(或者好吧,你需要的调整)
感谢@AntonioBeamud,我能够得到明确的答案,我不知道可以在定义中传递一个列表
Table
。在我看来,这是最优化的解决方案,尽管我对更有效的解决方案持开放态度。(英寸)
reporlab
的值为:inch
72.0
我使用的边距如下:
使用我们在左右边距中的值
36.0
(inch/2
):现在根据下表 A4 格式的纸张尺寸为 8.3 x 11.7(英寸):
因此,通过简单的计算,我们可以确定纸张的总宽度以及相对于边距的剩余总空间:
左右边距的总空间为
72.0
(inch / 2 * 2
)。使用我们将拥有的总空间:尽管如此,计算每列的总宽度更像是一个反复试验的问题,而不是其他任何事情。经过测试,我的列得到了以下最终结果:
列的总和:
结果是一个充分利用了相对于边距的全宽空间的表格:
我希望它可以为其他人提供参考,避免头痛。
参考: