简体   繁体   English

如何将生成的PDF与Reportlab附加到App Engine Python中的电子邮件中

[英]How to attach generated PDF with Reportlab to Email in App Engine Python

I have a method that generates a PDF file using Reportlab library: 我有一个使用Reportlab库生成PDF文件的方法:

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) 

when I call that method, it opens a save dialog, where I can specify where the file should be saved. 当我调用该方法时,它会打开一个保存对话框,我可以在其中指定文件的保存位置。

How should I do to attach the generated pdf in email? 如何将生成的pdf附加到电子邮件中?

I have seen this example: 我见过这个例子:

from google.appengine.api import urlfetch
from google.appengine.api import mail  

url = "http://www.abc.com/files/file.pdf" 
result = urlfetch.fetch(url)

if result.status_code == 200: 
  document = result.content

mail.send_mail(sender="youremail@yourdomain.com",
               to="receiver@hisdomain.com",
               subject="The file you wanted",
               body="Here is the file you wanted",
               attachments=[("The file name.pdf", document)])

But I don't know how to apply it in this particular case. 但我不知道如何在这种特殊情况下应用它。

Thanks in advance! 提前致谢!

SOLUTION: 解:

Based on the suggestion given by @Jesús, this is how I solved the problem: 根据@Jesús给出的建议,这就是我解决问题的方法:

class PdfTable(db.Model):
    fecha = db.DateTimeProperty(auto_now_add=True)
    archivoBlob = db.BlobProperty()

def obtenerPDFNuevoPedido(self, handler,rsUsuarioPedido, rsPedido):
#1)I generated the PDF this way:
        styleCentered = ParagraphStyle(name="centeredStyle", alignment=TA_CENTER)
        styleCenteredLeft = ParagraphStyle(name="centeredStyle", alignment=TA_LEFT)
        styleCenteredRight = ParagraphStyle(name="centeredStyle", alignment=TA_RIGHT)

        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))
        data = [[Paragraph("Usuario",ParagraphStyle(name="centeredStyle", alignment=TA_LEFT, fontSize=7)), Paragraph("Producto/Precio/Cantidad",ParagraphStyle(name="centeredStyle", alignment=TA_LEFT, fontSize=7)),Paragraph("Total", ParagraphStyle(name="centeredStyle", alignment=TA_RIGHT, fontSize=7))]]
        #
        #
        #
#2)Inside the same method, I saved the PDF file in the Datastore       
        pdf = StringIO.StringIO()

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

        content = pdf.getvalue()

        blob = model.PdfTable()
        blob.archivoBlob = db.Blob(content)
        blob.put()
#3)The file recently stored  in the datastore was attached like this:        
        mail.send_mail(sender="youremail@yourdomain.com",
               to="receiver@hisdomain.com",
               subject="The file you wanted",
               body="Here is the file you wanted",
               attachments=[('resumen_pedido.pdf'), blob.archivoBlob)])

Although I don't know if this is the more efficient way to solve the problem...but it works 虽然我不知道这是否是解决问题的更有效方法......但它确实有效

No need to write to the Blobstore. 无需写入Blobstore。 Just use the file type cStringIO to store the contents of your pdf then output value with .get_value() 只需使用文件类型cStringIO来存储pdf的内容,然后使用.get_value()输出值

self.result = cStringIO.StringIO() 
self.pdf = pisa.pisaDocument(cStringIO.StringIO(self.html.encode("UTF-8")), self.result,encoding='UTF-8')
mail.send_mail,sender_address, to_address, subject, body,attachments=[(file_name,self.result.get_value())]

I think the right way to do this is: 我认为正确的做法是:

  • Generate the PDF file (first fragment of code) 生成PDF文件(代码的第一个片段)
  • Save the PDF file in the Datastore. 将PDF文件保存在数据存储区中。
  • Use the second fragment of code to attach the PDF to the email. 使用第二个代码片段将PDF附加到电子邮件中。

Try it and tell us =) 试试吧告诉我们=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Reportlab将生成的PDF保存到App Engine Python中的数据存储区 - How to save generated PDF with Reportlab to Datastore in App Engine Python 如何将 Python 中的报告实验室生成的 PDF 转换为 HTML - How to convert PDF generated from reportlab in Python to HTML 在 email 中附加 django 生成的 pdf - Attach django generated pdf in email 电子邮件在zip文件中附加多个.CSV:google app engine python - Email attach multiple .CSV in zip file: google app engine python 使用reportLab生成的pdf大小 - Size of pdf generated with reportLab 如何在Google App Engine中使用reportlab - how to use reportlab with google app engine 如何在GAE(Google App Engine)上使用reportlab? - How to use reportlab on GAE(Google App Engine)? 如何将pdf附加到python 2.4.3脚本生成的我的邮件中? - How to attach pdf to my mail generated by python 2.4.3 script? 在Mailgun消息Django / Python中附加生成的PDF - Attach generated PDF in Mailgun message Django/Python 如何使用Platypus将Google App Engine动态图像对象添加到ReportLab pdf文档中:“ ImageReader”对象没有属性“ getKeepWithNext” - How to add Google app engine dynamic image objects to ReportLab pdf document with Platypus: “ImageReader' object has no attribute 'getKeepWithNext”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM