0

My web app is written by python 2.7 & Django 1.11. When a customer pays to subscribe, it will immediately generate an invoice pdf and send to customer via email. But the code is not working when I cut time.sleep. i guess there is a racing condition.

The pdf file is generated with wkhtmltopdf (0.9.9) and pdfkit. What is the time required for sleep? or is it a suitable way?

I am going to send it to customer after pdf is generated. It needs to sleep again to wait for pdf generation?

output_html = current_month_folder + invoice_name + '.html'
output_filename = current_month_folder + invoice_name + '.pdf'
email_filename = invoice_name + '.pdf'
html_content = render_to_string(input_filename, context)

with open(output_html, 'w') as f:
    f.write(html_content)
    f.flush()
time.sleep(5)

pdfkit.from_file(output_html, output_filename)
Benny Chan
  • 730
  • 1
  • 8
  • 24

1 Answers1

0

You don't need to save that file and make it pdf. Use from_string instead

html_content = render_to_string(input_filename, context)

pdfkit.from_string(html_content, output_filename)

You don't need to set sleep! You can place email code below.

Rajan
  • 1,463
  • 12
  • 26
  • I changed to your way but it still couldn't work. After checking, it's because `reload(sys)` `sys.setdefaultencoding('utf8')` I removed it and it can generate the pdf file. But it still couldn't send email to my mailbox yet – Benny Chan Jun 29 '18 at 15:46