PDF with ReportLab How to make text within a table do not get out

0

Hello, I need to know how to make the PDF document that I generate on my site, the text that goes inside a column does not come out and if the column is not wide enough that it continues below. Look at the table SERVICE AGREEMENTS This photo is from the PDF and you will see what the problem is.

The code in the view.py is as follows:

def pdf_export(request):
id_cont=request.GET['id']
filename="Contrato de CubanCloud_"+request.user.first_name+"_"+request.user.last_name+".pdf"
# Creamos el response
response=HttpResponse(content_type='application/pdf')
response['Content-Disposition']='attachment; filename="%s"' % filename
# Observa que ahora en vez de usar el nombre del archivo usamos el response
doc=SimpleDocTemplate(
    response,
    pagesize=letter,
    rightMargin=72,
    leftMargin=72,
    topMargin=2,
    bottomMargin=18,
)
Story=[]
im=Image(settings.MEDIA_ROOT + '/LogoCubanCloudPDF1.png', width=550, height=70)
Story.append(im)
styles=getSampleStyleSheet()
datos1=Paragraph('NOMBRE Y APELLIDO(S) DEL CLIENTE: '+request.user.first_name+' '+request.user.last_name,styles['Normal'])
datos2=Paragraph('NOMBRE DE USUARIO: '+request.user.username,styles['Normal'])
Story.append(datos1)
Story.append(datos2)
datos3=Paragraph('E-MAIL: '+request.user.email,styles['Normal'])
Story.append(datos3)
noContrato=Paragraph('NO. CONTRATO: '+str(id_cont),styles['Normal'])
Story.append(noContrato)
p=Image(settings.MEDIA_ROOT+'/espacioPDF.png',width=550, height=30)
Story.append(p)
encabezados=('Servicios Contratados', 'ID.Servicio', 'Plan', 'Precio')
lista_nombres=[]
for var in Servicio_Contratado.objects.filter(contrato_id=id_cont):
    lista_nombres.append((var.nombre, var.pk, str(var.plazo) + " días", var.precio))
lista_nombres.reverse()
detalle_orden=Table([encabezados] + lista_nombres,colWidths=[170,100,100,100])
# Aplicamos estilos a las celdas de la tabla
detalle_orden.setStyle(TableStyle(
    [
        ('GRID', (0, 0), (3, -1), 1, colors.dodgerblue),
        ('LINEBELOW', (0, 0), (-1, 0), 2, colors.darkblue),
        ('BACKGROUND', (0, 0), (-1, 0), colors.dodgerblue)
        # # La primera fila(encabezados) va a estar centrada
        # ('ALIGN', (0, 0), (0, 0), 'CENTER'),
        # # Los bordes de todas las celdas serán de color negro y con un grosor de 1
        # ('GRID', (0, 0), (-1, -1), 0, colors.transparent),
        # # El tamaño de las letras de cada una de las celdas será de 10
        # ('FONTSIZE', (0, 0), (0, 0), 10),

    ]
))
Story.append(detalle_orden)
p=Image(settings.MEDIA_ROOT + '/espacioPDF.png', width=550, height=30)
Story.append(p)
p=Paragraph('ACUERDOS DE NIVEL DE SERVICIOS',styles['Normal'])
Story.append(p)
encabezados=['No.', 'AsL']
lista_acl=[]
for var in Asl.objects.filter(generales=True):
    lista_acl.append((var.pk, var.descripcion))
lista_acl.reverse()
detalle_orden=Table([encabezados] + lista_acl,colWidths=[70,400,0])
# Aplicamos estilos a las celdas de la tabla
detalle_orden.setStyle(TableStyle(
    [
        ('GRID', (0, 0), (3, -1), 1, colors.dodgerblue),
        ('LINEBELOW', (0, 0), (-1, 0), 2, colors.darkblue),
        ('BACKGROUND', (0, 0), (-1, 0), colors.dodgerblue)
        # # La primera fila(encabezados) va a estar centrada
        # ('ALIGN', (0, 0), (0, 0), 'CENTER'),
        # # Los bordes de todas las celdas serán de color negro y con un grosor de 1
        # ('GRID', (0, 0), (-1, -1), 0, colors.transparent),
        # # El tamaño de las letras de cada una de las celdas será de 10
        # ('FONTSIZE', (0, 0), (0, 0), 10),

    ]
))
Story.append(detalle_orden)
doc.build(Story)
return response
    
asked by Roberto Cárdenas 12.05.2017 в 16:20
source

1 answer

1

In the table of the SERVICE LEVEL AGREEMENTS, you have to add a line of code, it looks like this:

    for var in Asl.objects.filter(generales=True):
    # creo variable p para guardar la descripcion
    p=Paragraph(var.descripcion, styles['Normal'])
    # añado a la lista la llave primaria de acl y ademas la descripcion contenida en p
    lista_acl.append((var.pk, p))
detalle_orden=Table([encabezados] + lista_acl,colWidths=[70,400])

The detail is in the for cycle, within it we must pass to a Paragraph type variable the content of the description attribute. And then pass to the list_acl the variable of type Paragraph. The result is this:  I hope it serves you. Blessings

    
answered by 12.05.2017 в 20:44