我是reportlab的新手,我遇到的问题是尽管能够使用reportlab创建报告,但我只能将它们保存在项目的根文件夹中,但是当我将其上传到测试服务器时出现此错误:
错误和回溯
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") ...
我的部分观点(更新)
# -*- 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
更新
将视图方法更改为返回响应时,它被执行了 2 次
如果您想要将信息流从 PDF 发送到客户端以供下载,您要做的就是返回一个
HttpResponse
.例子:
我有类似你要求的东西,
这是我在 Django 模板中的 HTML 代码,我对接收我在此处发送的参数的 URL 执行 POST
这将是我的 Python 代码,它还必须将方法添加到 URL。