Generating and Downloading PDF.
Hi guys, I'm creating a pdf using reportlab, after the canvas.close() I put the following code inside a External Method: R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/rtf') R.setHeader('content-length', str(len(data))) R.write(data) But I receive the following error: *Error Type: NameError* *Error Value: global name 'self' is not defined Is there some import wich I have been missing? Fernando Lujan
Fernando Lujan wrote:
Hi guys,
I'm creating a pdf using reportlab, after the canvas.close()
I put the following code inside a External Method:
R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/rtf') R.setHeader('content-length', str(len(data))) R.write(data)
But I receive the following error:
*Error Type: NameError* *Error Value: global name 'self' is not defined
Is there some import wich I have been missing?
The method that contains your code must have at least the first parameter 'self':: def pdfwrite(self): R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/rtf') R.setHeader('content-length', str(len(data))) R.write(data) It's a way of getting access to context. --jcc -- http://plonebook.packtpub.com/
J Cameron Cooper wrote:
The method that contains your code must have at least the first parameter 'self'::
def pdfwrite(self): R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/rtf') R.setHeader('content-length', str(len(data))) R.write(data)
It's a way of getting access to context.
OK, thanks. But how can I pass the self parameter using the dtml-call tag? <dtml-call "pdfwrite(self)"> is correct? Because I'm using this and works fine. Fernando Lujan
Fernando Lujan wrote:
J Cameron Cooper wrote:
The method that contains your code must have at least the first parameter 'self'::
def pdfwrite(self): R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/rtf') R.setHeader('content-length', str(len(data))) R.write(data)
It's a way of getting access to context.
OK, thanks. But how can I pass the self parameter using the dtml-call tag? <dtml-call "pdfwrite(self)"> is correct? Because I'm using this and works fine.
It's Python magic. When you call a function/method on an object, the object is provided as the first parameter of the method. This is implicit, I believe, in DTML calls, either expression or name. It's explicit in TALES and Python:: context/pdfwrite context.pdfwrite() It's different than many other languages, but it turns out to be pretty useful. --jcc -- http://plonebook.packtpub.com/
J Cameron Cooper wrote:
Fernando Lujan wrote:
J Cameron Cooper wrote:
The method that contains your code must have at least the first parameter 'self'::
def pdfwrite(self): R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/rtf') R.setHeader('content-length', str(len(data))) R.write(data)
It's a way of getting access to context.
OK, thanks. But how can I pass the self parameter using the dtml-call tag? <dtml-call "pdfwrite(self)"> is correct? Because I'm using this and works fine.
It's Python magic. When you call a function/method on an object, the object is provided as the first parameter of the method. This is implicit, I believe, in DTML calls, either expression or name. It's explicit in TALES and Python::
context/pdfwrite context.pdfwrite()
So, I put the code here. Please, what I'm missing? :) def gerarTCE(self, id_tce, cnpj_escola='', matricula=''): from reportlab.lib.colors import Color from reportlab.lib.pagesizes import letter from reportlab.platypus import Paragraph, SimpleDocTemplate, XBox from reportlab.lib.styles import ParagraphStyle from reportlab.pdfgen import canvas import os import urllib from reportlab.lib.units import inch, cm import time import string _linha = cm / 2.25 _fname = "tce"+id_tce+".pdf" _c = canvas.Canvas(_fname, pagesize=letter) _width, _height = letter _aW = _width - 2 * cm _aH = 750 _c.bookmarkPage("TCE") _c.bookmarkPage("pagina1") _c.addOutlineEntry("TCE", "TCE") _c.addOutlineEntry("Pagina 1", "pagina1", 1) _c.showOutline() _c.setAuthor("NUBE - Núcleo Brasileiro de Estágios") _c.scale(1, 0.9) _c.setFont("Times-Bold", 16) _c.drawCentredString(_width / 2, 735, "TERMO ADITIVO") _c.setFont("Times-Roman", 9) _c.drawCentredString( _width / 2, 735, "(determinações legais estabelecidas no art. 5º e o §1º do art. 6º do Decreto 87.497/82 que regulamentou a Lei 6.494/77)") _c.drawCentredString( _width / 2, 725, "(Atualizada pela Medida Provisória N.º 1952-22 de 30/03/2000)") _c.showPage() _c.save() R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/pdf') R.Header('content-length', str(len(_fname))) R.write(_fname)
Fernando Lujan wrote:
J Cameron Cooper wrote:
Fernando Lujan wrote:
J Cameron Cooper wrote:
The method that contains your code must have at least the first parameter 'self'::
def pdfwrite(self): R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/rtf') R.setHeader('content-length', str(len(data))) R.write(data)
It's a way of getting access to context.
OK, thanks. But how can I pass the self parameter using the dtml-call tag? <dtml-call "pdfwrite(self)"> is correct? Because I'm using this and works fine.
It's Python magic. When you call a function/method on an object, the object is provided as the first parameter of the method. This is implicit, I believe, in DTML calls, either expression or name. It's explicit in TALES and Python::
context/pdfwrite context.pdfwrite()
So, I put the code here. Please, what I'm missing? :)
It's easier to guess when you say what the problem is. I do see some funny indentation down at the bottom. --jcc
def gerarTCE(self, id_tce, cnpj_escola='', matricula=''):
from reportlab.lib.colors import Color from reportlab.lib.pagesizes import letter from reportlab.platypus import Paragraph, SimpleDocTemplate, XBox from reportlab.lib.styles import ParagraphStyle from reportlab.pdfgen import canvas import os import urllib from reportlab.lib.units import inch, cm import time import string
_linha = cm / 2.25 _fname = "tce"+id_tce+".pdf" _c = canvas.Canvas(_fname, pagesize=letter) _width, _height = letter _aW = _width - 2 * cm _aH = 750
_c.bookmarkPage("TCE") _c.bookmarkPage("pagina1") _c.addOutlineEntry("TCE", "TCE") _c.addOutlineEntry("Pagina 1", "pagina1", 1) _c.showOutline() _c.setAuthor("NUBE - Núcleo Brasileiro de Estágios") _c.scale(1, 0.9)
_c.setFont("Times-Bold", 16) _c.drawCentredString(_width / 2, 735, "TERMO ADITIVO") _c.setFont("Times-Roman", 9) _c.drawCentredString( _width / 2, 735, "(determinações legais estabelecidas no art. 5º e o §1º do art. 6º do Decreto 87.497/82 que regulamentou a Lei 6.494/77)") _c.drawCentredString( _width / 2, 725, "(Atualizada pela Medida Provisória N.º 1952-22 de 30/03/2000)") _c.showPage() _c.save() R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/pdf') R.Header('content-length', str(len(_fname))) R.write(_fname)
J Cameron Cooper wrote:
It's easier to guess when you say what the problem is.
I do see some funny indentation down at the bottom.
OK, it's happen because I paste the code inside a html message, than it's happen. The following code is working. But I still have a problem. The browser ( firefox ) don't reconize the generated document as a PDF. I'm calling this external method through a <dtml-var "gerar_tce_pdf()"> tag, inside a DTML DOCUMENT. def gerarTCE(self): from reportlab.pdfgen import canvas import string import os c = canvas.Canvas("hello.pdf") c.drawString(100,100,"Hello World") c.showPage() c.save() arquivo = open("hello.pdf").read() R = self.REQUEST.RESPONSE R.setHeader('content-type', 'application/pdf') R.setHeader('content-length', str(len(arquivo))) R.write(arquivo) Thanks for helping... :) Fernando Lujan
Fernando Lujan wrote:
I'm calling this external method through a <dtml-var "gerar_tce_pdf()"> tag, inside a DTML DOCUMENT.
Um? What else is that method returning? I'd suggest you call getrarTCE directly by URL, check you're setting the correct content-type and maybe set a content disposition too... cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
Chris Withers wrote:
Um? What else is that method returning? I'd suggest you call getrarTCE directly by URL, check you're setting the correct content-type and maybe set a content disposition too...
Solved. :) I create a External Method that returns the code. I'm calling the external method using: <dtml-var "generatePDF(REQUEST.response)"> And I put the following lines at the end of the code: fname stands for the filename def generatePDF(page): os.rename(_fname, "tces/"+_fname) file = open("tces/"+_fname).read() os.remove("tces/"+_fname) page.setHeader('content-type', 'x-application/pdf') page.setHeader('content-length', str(len(file))) page.write(file) This was the best solution that I could thought, once reportlab didn't create PDF "on the fly", without writing in the filesystem.
--On Freitag, 13. Mai 2005 13:43 Uhr -0300 Fernando Lujan <flujan@gmail.com> wrote:
This was the best solution that I could thought, once reportlab didn't create PDF "on the fly", without writing in the filesystem.
You could use cStringIO for writing the file in memory but in reality it really does not matter from the point of speed. Using a file offers you the possibility to emit it through a FileStreamIterator. This avoids that you need to load the file as a whole into the memory. -aj
--On Mittwoch, 27. April 2005 15:56 Uhr -0300 Fernando Lujan <flujan@uol.com.br> wrote:
Is there some import wich I have been missing?
At least we are missing the *complete* definition of the method. Your code fragment is insufficient. -aj
participants (5)
-
Andreas Jung -
Chris Withers -
Fernando Lujan -
Fernando Lujan -
J Cameron Cooper