创建 PDF 并使用烧瓶邮件作为附件发送

问题描述

我正在尝试使用 pdf-kit 从 HTML 页面创建 PDF,然后将该 pdf 作为附件发送到电子邮件中。

这是我创建 pdf 的代码,这似乎有效:

                html = render_template("office_report.html",role=role,name=name,owner=off,reps=reps,all_weekly=all_weekly,we=we,totals=totals,cog=cog,all_weekly_rpt=True,off_rpt=off_rpt,v=v,adonors=adonors,ae=ae,ov=ov,pay_total=pay_total)
                options = {"enable-local-file-access": None}
                try:

                    pdf=pdfkit.from_string(html,False,options=options)
                    files =  pdf
                except Exception as e:
                    print(e)
                    pdf='ERROR'

然后我尝试发送电子邮件,但不明白如何将 PDF 附加到电子邮件中。

try:

                    msg = Message('Owner Report ',sender = ("Brand Drivers",BD),recipients = ['jesswholt@gmail.com',BD,comp])
                    msg.body = "Weekly Report" + str(we)
                    
                    with app.open_resource(files) as fp:
                        msg.attach("Owner Report","invoice/pdf",fp.read())
                    mail.mail.send(msg)
                    print('sent')
                except Exception as e:
                    print(e)

这里是转换成PDF的HTML

<!doctype html>
<html lang="en">
<head>
<Meta charset="utf-8" />

<Meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

<title>Childhelp</title>

<Meta content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0' name='viewport' />
<Meta name="viewport" content="width=device-width" />


</head>
<body>

            <div class="header">
                <h4 class="title">Office Report <br>{{owner.owner_name}}<br>{{owner.office_ID}}</h4>
                <p>WeekEnding: {{we.strftime('%m-%d-%Y')}}</p>
            </div>


                  <table  id="office_weekly" class="table table-hover table-striped">

                      <thead>
                        <th>Ambassador</th>
                        <th>Transactions</th>
                        <th>Total Payments</th>
                        <th>Items Sold</th>
                        <th>Hotlines</th>
                        <th>Bonuses</th>
                      <!--  <th>Total</th>-->
                      </thead>
                      <tbody>
                        {% for r in reps %}
                          <tr>
                            <td>
                              <strong>{{r.rep_name}}</strong><br>
                              {{r.rep_ID}}
                            </td>
                            {% for w in all_weekly if w.rep == r.rep_ID %}
                            <td>{{w.transactions}}</td>

                            <td>
                              Cash: ${{w.cash/100}}<br>
                              Check: ${{w.check/100}}<br>
                              Card: ${{w.card/100}}<br>
                              <strong>Total: ${{w.total/100}}</strong>
                            </td>
                            <td>
                              Sold: {{w.tot_itm}}<br>
                              refunded: {{w.refunds}}<br>
                              Amount refunded: {{"$%.2f" % (w.amount_refunded/100)}}<br>
                              refund Dates {{w.dates_refunded}}

                            </td>
                            <td>
                              Active: {{w.subs}}<br>
                              Cancels: {{w.can}}
                            </td>
                            <td>{{"$%.2f" % (w.bonus | float)}}</td>

                            {% else %}
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td>Report Incomplete</td>
                          </tr>
                          {% endfor %}
                        {% endfor %}
                        <tr>
                          <td><hr></td>
                          <td><hr></td>
                          <td><hr></td>
                          <td><hr></td>
                          <td><hr></td>
                          <td><hr></td>
                        </tr>
                      </tbody>

                      <tbody>
                      <tr>
                        <th></th>
                      </tr>
                      <td>Office Sub Total:</td>
                      <td>{{totals.transactions}}

                      </td>
                      <td>
                        Cash: {{totals.cash}}<br>
                        Check: {{totals.check}}<br>
                        Card: {{totals.card}}<br>
                        Total: {{totals.sales}}<br>
                        Overrides: {{"$%.2f" % (ov | float)}}

                      </td>
                      <td>
                        Items: {{totals.t_items}}<br>
                        COG: {{"$%.2f" % (cog)}} per piece<br>
                        Total COG: {{"$%.2f" % (totals.total_cog)}}

                      </td>
                      <td>
                        Total Hotlines for Office:<br>
                        Active: {{totals.hotlines}}<br>
                        Canceled: {{totals.cancels}}<br>

                        Total Pay Hotlines: {{"$%.2f" % (totals.hotlines | float * 15)}}

                      </td>
                      <td>
                        Total Bonuses Paid: {{totals.bonus}}<br>

                        Items refunded: {{totals.items_refunded}}<br>

                        Amount refunded: {{totals.amount_refunded / 100}}

                      </td>
                      </tbody>
                      <tbody>
                        <tr>
                          <th> Line Items</th>
                        </tr>
                        <td></td>
                        <td></td>
                      <td><strong>Balance Carry: {{off_rpt.balance_carry}}</strong></td>

                      </tbody>
                      <tbody>
                        <td></td>
                        <td></td>
                      <td><strong>Deposit: {{off_rpt.deposit}}</strong></td>

                      </tbody>
                      <tbody>
                        <td></td>
                        <td></td>
                      <td><strong>Events: {{totals.events}}</strong></td>

                      </tbody>
                      <tbody>
                        <td></td>
                        <td></td>
                      <td><strong>Line Items:</strong></td>

                      <td>
                        Name: {{v.li1_n}}<br>
                        Amount: ${{v.li1_p}}<br>
                        Name: {{v.li2_n}}<br>
                        Amount: ${{v.li2_p}}<br>
                        Name: {{v.li3_n}}<br>
                        Amount: ${{v.li3_p}}<br>
                        Name: {{v.li4_n}}<br>
                        Amount: ${{v.li4_p}}
                      </td>
                      </tbody>

                      <tbody>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                      {% if pay_total %}
                        <td><strong>Owner Payout:</strong></td>
                        <td><strong>{{"$%.2f" % (pay_total | float)}}</strong></td>
                      {% else %}
                        <td><strong>Card Sales:</strong></td>
                        <td><strong>{{totals.card}}</strong></td>
                      {% endif %}
                      </tbody>

                      <tbody>
                        <td>
                          <h3>Hotline Signups</h3>
                        </td>
                      </tbody>
                      <thead>
                        <th>Donor Name</th>
                        <th>Ambassador</th>

                        <th>Signup Date</th>
                        <th>Canceled</th>

                      <!--  <th>Total</th>-->
                      </thead>
                      <tbody>
                        {% for d in adonors %}
                        <tr>
                          <td>{{d.donor_name}}</td>
                          {% for x in reps if x.rep_ID == d.rep_ID %}
                          <td>{{x.rep_name}}<br>{{x.rep_ID}}</td>
                          {% endfor %}

                          <td>{{d.monthly_start_date}}</td>
                          {% if d.donor_status == False %}
                            <td>{{d.monthly_stop_date}}</td>
                          {% else %}
                            <td style="color:green">Active</td>
                          {% endif %}
                        </tr>

                        {% endfor %}
                      </tbody>

                  </table>
                  <div class="col-md-18">

                  </div>




    </body>

    </html>

解决方法

取而代之的是:

with app.open_resource(files) as fp:
    msg.attach("Owner Report","invoice/pdf",fp.read())

就这样做:

msg.attach("Owner Report",pdf)

pdf(或 files)包含您的文件作为字节类型对象。因此您无需使用 open_resource 并读取文件,因为您已经拥有所需的数据。