7

Ho un metodo che genera un file PDF utilizzando Reportlab libreria:Come risparmiare PDF generato con Reportlab per archivio dati in App Engine Python

def obtenerPDFNuevoPedido(self, handler,rsUsuarioPedido, rsPedido): 
    handler.response.headers['Content-Type'] = 'application/pdf' 
    handler.response.headers['Content-Disposition'] = 'attachment; filename=output.pdf' 
    story = [] 
    story.append(Paragraph('CHIPAS', ParagraphStyle(name="centeredStyle", alignment=TA_CENTER, fontSize=20))) 
    story.append(Paragraph('____________ENLANUBE', ParagraphStyle(name="centeredStyle", alignment=TA_CENTER, fontSize=20))) 
    story.append(Spacer(6, 22)) 
    story.append(Table([[Paragraph(str(strftime("%Y-%m-%d", gmtime())), ParagraphStyle(name="centeredStyle", alignment=TA_LEFT, fontSize=7)), 
    Paragraph(str(strftime("%H:%M:%S", gmtime())), ParagraphStyle(name="centeredStyle", alignment=TA_RIGHT, fontSize=7))]],colWidths=[5.05 * cm, 3.1 * cm])) 
    story.append(Paragraph("DEVELOPED AT ROSHKA-LABS", ParagraphStyle(name="centeredStyle", alignment=TA_CENTER, fontSize=6))) 
    story.append(Paragraph('-'*50, styleCentered)) 
    #... 
    #... 
    doc = SimpleDocTemplate(handler.response.out, pagesize=letter) 
    doc.build(story) 

quando chiamo quel metodo, si apre una finestra di salvataggio, dove ho può specificare dove il file deve essere salvato.

Come devo fare per salvare il file PDF generato nel Datastore?

Grazie in anticipo!

risposta

8

1) È possibile specificare il nome del file solo desiderato (non destinazione)

2) Prova questo (non testato)

#define your database structure 
from google.appengine.ext import db 

class PdfStorage(db.Model): 
    timeAdded = db.DateTimeProperty(auto_now_add=True) 
    pdfContent = db.BlobProperty() 

Sostituisci il tuo

doc = SimpleDocTemplate(handler.response.out, pagesize=letter) 
doc.build(story) 

con

pdf = StringIO() 


doc = SimpleDocTemplate(pdf, pagesize=letter) 
doc.build(story) 

#get content of generated pdf 
content = pdf.getvalue() 

#save to db 
pdfStorage = PdfStorage(pdfContent = content); 
pdfStorage.put() 

#output to browser 
handler.response.write(content) 
+1

Grazie russenreaktor! Ho modificato due righe della tua risposta =). Funziona come un incanto !. – Lucas

Problemi correlati