Generate Pdf in Django

0

someone can help me generate a pdf with django, but I want the pdf to be saved in a server path and not downloaded to the client machine. I currently have this:

def pdf_export(request,pac_id):   
orden = OrdenPaciente.objects.get(id=pac_id)
filename="resultados_"+pac_id+"_"+orden.paciente.numero_identificacion+".pdf"
response=HttpResponse(content_type='application/pdf')
response['Content-Disposition']='attachment; filename="%s"' % filename
doc = SimpleDocTemplate(
    response,
    pagesize=letter,
    rightMargin=72,
    leftMargin=72,
    topMargin=2,
    bottomMargin=18,

)
Story=[]
im=Image(settings.MEDIA_ROOT + 'Laboratorio.png', width=150, height=90)
Story.append(im)
styles=getSampleStyleSheet()
pac = OrdenPaciente.objects.get(id=pac_id)
datos1=Paragraph('NOMBRE Y APELLIDO(S) DEL CLIENTE: '+pac.paciente.nombres+' '+pac.paciente.apellidos,styles['Normal'])
datos2=Paragraph('SEXO: ' + pac.paciente.genero, styles['Normal'])
datos3=Paragraph('DIRECCION: ' + pac.paciente.direccion, styles['Normal'])
datos4 = Paragraph('MAIL: ' +pac.paciente.email, styles['Normal'])
datos5 = Paragraph('DOCTOR: ' + pac.doctor.nombres+' '+pac.doctor.apellidos, styles['Normal'])
Story.append(Spacer(1, 20))
Story.append(datos1)
Story.append(datos2)
Story.append(datos3)
Story.append(datos4)
Story.append(datos5)
Story.append(Spacer(1, 20))

for temp in transaccional.objects.filter(ordenpaciente_id=pac.id).order_by('examen_id').distinct('examen_id'):
    datos6 = Paragraph('Examen: ' + temp.examen.descripcion, styles['Normal'])
    encabezados = ('Item', 'Valor', 'Valor Referencial')
    lista_nombres = []
    for var in transaccional.objects.filter(ordenpaciente_id=pac.id,examen_id=temp.examen_id):
        lista_nombres.append((var.parametro_id.nombre, var.valor_parametro,var.referencial.valores_referenciales))
    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(datos6)
    Story.append(detalle_orden)
    Story.append(Spacer(1, 20))
doc.build(Story)
return response

It works very well to export the pdf, but what I want is that it not be downloaded but stored automatically in a server path.

    
asked by Roberto Feijoo 29.10.2018 в 18:23
source

0 answers