Pergunta

I need to generate the form seen here using Python/reportlab.

http://www.flickr.com/photos/49740282@N06/4563137758/sizes/o/

I am attempting to do this by creating a custom flowable for the header at the top (with the boxes drawn) and then having a table flowable for the jewelry table below. What shows as the JEWELRY table for this example could potentially be multiple tables. I am having trouble getting my drawn header to "flow". It gets drawn, but then my table data overlays it instead of appearing below it.

This is my first project with reportlab. Before I really get into debugging this, I would like to know from someone with reportlab experience if my approach is correct here. Thanks!

Foi útil?

Solução

I cannot help you with reportlab because I'm not so experienced user (I left idea to use it after some problems that made me crazy:)). But if you consider using some other tool to generate your pdf in python I would avice you to take a look at xhtml2pdf - it might be a good option if you didn't go to far with reportlab. If you are familiar with html this might be easier for you to use. The idea here is simple: it converts html you provide to a pdf file. Of course you need to generate html code somehow (I use django templates for that).

Outras dicas

I don't see the need for a custom flowable here.

You may just use tables (and tablestyles) to do the "header".

Another simple solution, if you need some fancy background, is to draw an image (like a JPG) and then draw the variable strings on top of it.

I agree with dugres that you don't require any customable flowable for that particular form shown in flickr. You can just use Table and TableStyle to accomplish your job.

What to consider, before you start to dive deep into reportlab, is that your table will not be too long that it goes to next page. Then the tablestyle will require manual editing. SPAN cells on the next page's table will return error. But for a one-page solution, reportpdf is a good option.

For the fancy output, nice graphic effect. You'll need to do according to what dugres suggested.

For a kickstart code on developing a table:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
from reportlab.lib import colors

width, height = A4
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER

def coord(x, y, unit=1):
    x, y = x * unit, height -  y * unit
    return x, y

# Headers
hdescrpcion = Paragraph('''<b>descrpcion</b>''', styleBH)
hpartida = Paragraph('''<b>partida</b>''', styleBH)
hcandidad = Paragraph('''<b>candidad</b>''', styleBH)
hprecio_unitario = Paragraph('''<b>precio_unitario</b>''', styleBH)
hprecio_total = Paragraph('''<b>precio_total</b>''', styleBH)

# Texts
descrpcion = Paragraph('long paragraph', styleBH)
partida = Paragraph('1', styleN)
candidad = Paragraph('120', styleN)
precio_unitario = Paragraph('$52.00', styleN)
precio_total = Paragraph('$6240.00', styleN)

data= [[hdescrpcion, hcandidad,hcandidad, hprecio_unitario, hprecio_total],
       [partida, candidad, descrpcion, precio_unitario, precio_total]]

table = Table(data, colWidths=[2.05 * cm, 2.7 * cm, 5 * cm,
                               3* cm, 3 * cm])

table.setStyle(TableStyle([
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ]))

c = canvas.Canvas("a.pdf", pagesize=A4)
table.wrapOn(c, width, height)
table.drawOn(c, *coord(1.8, 9.6, cm))
c.save()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top