I am new to reportlab, the problem I have is that despite being able to create reports with reportlab, I have only been able to save them in the root folder of my project, but when I upload it to a test server I get this error:
Error and Traceback
IOError at /PHVA/pdfJornadas
[Errno 13] Permission denied: u'Reporte Jornadas de Trabajo Cliente 1 S.A.S.pdf'
Request Method: GET
Request URL: http://cliente1.mydomain.com/PHVA/pdfJornadas
Django Version: 1.6.5
Exception Type: IOError
Exception Value:
[Errno 13] Permission denied: u'Reporte Jornadas de Trabajo Cliente 1 S.A.S.pdf'
Exception Location: /usr/local/lib/python2.7/dist-packages/reportlab/pdfbase/pdfdoc.py in SaveToFile, line 218
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/var/www/html/cliente1/my_project']
Server time: Jue, 10 Mar 2016 09:46:10 -0500
Traceback Switch to copy-and-paste view
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
/var/www/html/cliente1/my_project/phva/views/reportes.py in exportarJornadasTrabajo
doc.build(Story) ...
/usr/local/lib/python2.7/dist-packages/reportlab/platypus/doctemplate.py in build
BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker) ...
/usr/local/lib/python2.7/dist-packages/reportlab/platypus/doctemplate.py in build
self._endBuild() ...
/usr/local/lib/python2.7/dist-packages/reportlab/platypus/doctemplate.py in _endBuild
if getattr(self,'_doSave',1): self.canv.save() ...
/usr/local/lib/python2.7/dist-packages/reportlab/pdfgen/canvas.py in save
self._doc.SaveToFile(self._filename, self) ...
/usr/local/lib/python2.7/dist-packages/reportlab/pdfbase/pdfdoc.py in SaveToFile
f = open(filename, "wb") ...
part of my views (Updated)
# -*- encoding: utf-8 -*-
from datetime import timedelta, date, datetime
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
#from reportlab.pdfgen import canvas
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.enums import TA_JUSTIFY, TA_CENTER
from reportlab.lib.pagesizes import letter, landscape
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image
from phva.forms import *
from matriz_legal.models import *
from gestion_riesgos.models import *
from capacitaciones.models import MatrizFormacion
from ripso.utilities import *
def exportarJornadasTrabajo(request):
empresa=PlanearEmpresa.objects.all()[0]
title = "Reporte Jornadas de Trabajo "+empresa.razon_social
filename = title+".pdf"
# Creamos el response
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
# Usando el response en lugar del filename
doc = SimpleDocTemplate(response,pagesize=letter, rightMargin=72,leftMargin=72, topMargin=72,bottomMargin=18)
Story=[]
#...
#Jornadas
jornadas=PlanearJornadaTrabajo.objects.all().order_by('grupo')
if jornadas:
datos_jorn=[]
datos_jorn.append(['Grupo', 'Nombre', 'Horario'])
for i in jornadas:
datos_jorn.append([i.get_grupo_display(), Paragraph(i.nombre, styles['Justify']), Paragraph(i.horario, styles['Justify'])])
t=Table(datos_jorn, colWidths=(4*cm, 4*cm, 8*cm))
t.setStyle(TableStyle([('BACKGROUND',(0,0),(-1,0),colors.gray),
('GRID', (0,1), (-1,-1), 0.25, colors.gray)]))
Story.append(t)
Story.append(Spacer(1, 12))
else:
p=ParagraphStyle('test')
ptext = '<font size=12>No se encuentran datos disponibles registrados.</font>'
Story.append(Paragraph(ptext, p))
Story.append(Spacer(1, 12))
#...
doc.build(Story)
return response
Updated
The views method is being executed 2 times when changing it to return response
If what you want is to send the flow of information from the PDF to the client for download, what you have to do is return a
HttpResponse
.Example:
I have something similar to what you ask for,
This is my HTML code in a Django Template and I do a POST to a URL that receives the parameter that I send here
and this would be my Python code that also has to add the method to the URL.