7

Tôi có một phương pháp mà tạo ra một tập tin PDF bằng cách sử ReportLab thư viện:Làm thế nào để tiết kiệm tạo PDF với ReportLab để Datastore trong 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) 

khi tôi gọi phương thức đó, nó sẽ mở ra một hộp thoại lưu, nơi tôi có thể chỉ định nơi lưu tệp.

Tôi nên làm gì để lưu tệp pdf được tạo trong kho dữ liệu?

Cảm ơn trước!

Trả lời

8

1) Bạn có thể chỉ định tên file chỉ mong muốn (không điểm)

2) Hãy thử điều này (không kiểm tra)

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

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

Replace bạn

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

với

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

Cảm ơn russenreaktor! Tôi đã chỉnh sửa hai dòng câu trả lời của bạn =). Nó hoạt động như quyến rũ !. – Lucas

Các vấn đề liên quan