如何在画布上使用pdfgen在Reportlab中绘制表格

时间:2018-10-05 02:24:41

标签: python django reportlab

我的桌子是从下往上打印的,随着项目数的增加,行数会增加,我知道画布是在绝对位置绘制的,但是在这种情况下,我无法理解为什么以这种方式打印,不是我知道即时代码是错误的。

def nota_pedido_report(request,id):

master = NotaPedido.objects.get(id=id)
detail = master.notapedidodetalle_set.all()
cant_elementos = len(detail)

response = HttpResponse(content_type = 'application/pdf')
#pdf_name = "nota_pedido.pdf" 
#response['Content-Disposition'] = 'attachment; filename=%s' % pdf_name
elements = []
buffer = BytesIO()
#doc = getDoc(buff,'nota de pedido')
c = canvas.Canvas(buffer, pagesize=A4)
y = 600
#Header
c.setTitle("Nota Pedido")
c.setLineWidth(.3)
c.setFont("Times-Bold", 12)
titulo1 = c.drawString(220,790,'NOTA DE PEDIDO')
c.drawString(250, 770,'Nº')
nro = c.drawString(270, 770, str(master.nroPedido))
print(settings.STATIC_ROOT)
archivo_imagen = settings.STATIC_ROOT+'/app/images/logo.png'
#imagen = Image(archivo_imagen, width=50, height=50,hAlign='LEFT')
c.drawString(400, 780,  master.fecha.strftime('%d-%m-%Y'))
c.drawImage(archivo_imagen, 80, 770, width=70, height=45)

#Table Header
styles = getSampleStyleSheet()
styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER
styleBH.fontSize = 12


parrafoStyle = ParagraphStyle('parrafos',alignment = TA_JUSTIFY,fontSize = 8,fontName="Times-Roman")
headings = ('Cantidad', 'Unidad de Medida', 'Descripción')
results = [(d.cantidad, d.unidadMedida, d.articulo.descripcion) for d in detail]

c.setFont("Times-Bold", 12)
c.drawString(80, 740, "Para: ")
c.drawString(270, 740, "De: ")
c.setFont("Times-Roman", 12)
c.drawString(110, 740, str(master.departamentoDestino))
c.drawString(295, 740, str(master.departamentoOrigen))
c.drawString(80, 720, "Solicitamos nos provean de los siguientes: ")

elements.append(headings)
elements.append(results)
c.setFont("Times-Roman", 6)


#Detalle Tabla
#for i in range(1, 10):
table = Table([headings]+results, colWidths=[1.35*inch, 1.35*inch, 3.5*inch], rowHeights=0.18*inch,splitByRow=10)
table.setStyle(TableStyle(
                    [ ('GRID', (0, 0), (4, -3), 1, colors.black),
                    ('INNERGRID', (0, 0), (-1, -1), 1, colors.black),
                    ('BOX', (0, 0), (-1, -1), 1, colors.black),
                    ('LINEBELOW', (0, 0), (-1, 0), 1, colors.black),
                    ('BACKGROUND', (0, 0), (-1, 0), colors.transparent),
                    ]
                ))

#elements.append(table)
table.wrapOn(c, 600, 800)
table.drawOn(c, 80, 580)


#table.wrapOn(c, 800, 600)
#if cant_elementos < 5:
#    table.drawOn(c, 80, y-58)
#else:
#    table.drawOn(c, 80, 400)

c.setFont("Times-Bold", 12)
c.drawString(80, 480, "Para uso en: ")
c.drawString(80, 460, "Precio aproximado: ")
c.setFont("Times-Roman", 12)
c.drawString(190, 480, to_str(master.descripcionUso))
c.drawString(190, 460, to_str(master.precioAproximado))

c.line(80, 435, 220, 435)
c.line(270, 435, 350, 435)
c.line(410, 435, 500, 435)
c.setFont("Times-Roman", 8)
c.drawString(85, 420, "Vo.Bo. Gte. Administrativo")
c.drawString(280, 420, "Jefe de Est.")
c.drawString(415, 420, "Gerente de Área")

#Header2
#c.setLineWidth(.3)
#c.setFont("Times-Roman", 12)
c.setFont("Times-Bold", 12)
titulo1 = c.drawString(220,380,'NOTA DE PEDIDO')
c.drawString(250, 360,'Nº')
nro = c.drawString(270, 360, str(master.nroPedido))
c.drawString(400, 380,  master.fecha.strftime('%d-%m-%Y'))
c.drawImage(archivo_imagen, 80, 360, width=70, height=45)

c.drawString(80, 340,  "Para: ")
c.drawString(280, 340, "De: " )
c.setFont("Times-Roman", 12)
c.drawString(110, 340, str(master.departamentoDestino))
c.drawString(300, 340, str(master.departamentoOrigen))
c.drawString(80, 320, "Solicitamos nos provean de los siguientes: ")

#Detalle Tabla 2
table2 = Table([headings]+results, colWidths=[1.35*inch, 1.35*inch, 3.5*inch], rowHeights=0.20*inch,splitByRow=1)
table2.setStyle(TableStyle(
                    [ ('GRID', (0, 0), (1, -3), 1, colors.black),
                    ('INNERGRID', (0, 0), (-1, -1), 1, colors.black),
                    ('BOX', (0, 0), (-1, -1), 1, colors.black),
                    ('LINEBELOW', (0, 0), (-1, 0), 1, colors.black),
                    ('BACKGROUND', (0, 0), (-1, 0), colors.transparent),
                    ]
                ))

#elements.append(table2)
table2.wrapOn(c, 800, 600)
table2.drawOn(c, 80, 230)


c.setFont("Times-Bold", 12)
c.drawString(80, 70, "Para uso en: ")
c.drawString(80, 50, "Precio aproximado: ")
c.setFont("Times-Roman", 12)
c.drawString(190, 70, to_str(master.descripcionUso))
c.drawString(190, 50, to_str(master.precioAproximado))
#elements.append(Paragraph(c.drawString(80, 55, "Precio aproximado: " + to_str(master.precioAproximado)) ,parrafoStyle))

c.line(80, 30, 220, 30)
c.line(270, 30, 350, 30)
c.line(410, 30, 500, 30)

c.setFont("Times-Roman", 8)
c.drawString(85, 15, "Vo.Bo. Gte. Administrativo")
c.drawString(280, 15, "Jefe de Est.")
c.drawString(415, 15, "Gerente de Área")

c.save()
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response

我附上打印时的图像:

案例1:

enter image description here

案例2:

enter image description here

我需要帮助!

0 个答案:

没有答案