Created
August 22, 2021 09:47
-
-
Save skolo-online/4c7134a1604d48ae2296f2d2663b6511 to your computer and use it in GitHub Desktop.
Create a PDF and Email to Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Dont forget the view imports | |
def emailDocumentInvoice(request, slug): | |
#fetch that invoice | |
try: | |
invoice = Invoice.objects.get(slug=slug) | |
pass | |
except: | |
messages.error(request, 'Something went wrong') | |
return redirect('invoices') | |
#fetch all the products - related to this invoice | |
products = Product.objects.filter(invoice=invoice) | |
#Get Client Settings | |
p_settings = Settings.objects.get(clientName='Skolo Online Learning') | |
#Calculate the Invoice Total | |
invoiceTotal = 0.0 | |
if len(products) > 0: | |
for x in products: | |
y = float(x.quantity) * float(x.price) | |
invoiceTotal += y | |
context = {} | |
context['invoice'] = invoice | |
context['products'] = products | |
context['p_settings'] = p_settings | |
context['invoiceTotal'] = "{:.2f}".format(invoiceTotal) | |
#The name of your PDF file | |
filename = '{}.pdf'.format(invoice.uniqueId) | |
#HTML FIle to be converted to PDF - inside your Django directory | |
template = get_template('invoice/pdf-template.html') | |
#Render the HTML | |
html = template.render(context) | |
#Options - Very Important [Don't forget this] | |
options = { | |
'encoding': 'UTF-8', | |
'javascript-delay':'1000', #Optional | |
'enable-local-file-access': None, #To be able to access CSS | |
'page-size': 'A4', | |
'custom-header' : [ | |
('Accept-Encoding', 'gzip') | |
], | |
} | |
#Javascript delay is optional | |
#Remember that location to wkhtmltopdf | |
config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf') | |
#Saving the File | |
filepath = os.path.join(settings.MEDIA_ROOT, 'client_invoices') | |
os.makedirs(filepath, exist_ok=True) | |
pdf_save_path = filepath+filename | |
#Save the PDF | |
pdfkit.from_string(html, pdf_save_path, configuration=config, options=options) | |
#send the emails to client | |
to_email = invoice.client.emailAddress | |
from_client = p_settings.clientName | |
emailInvoiceClient(to_email, from_client, pdf_save_path) | |
invoice.status = 'EMAIL_SENT' | |
invoice.save() | |
#Email was send, redirect back to view - invoice | |
messages.success(request, "Email sent to the client succesfully") | |
return redirect('create-build-invoice', slug=slug) | |
from django.core.mail import EmailMessage | |
from django.conf import settings | |
def emailInvoiceClient(to_email, from_client, filepath): | |
from_email = settings.EMAIL_HOST_USER | |
subject = '[Skolo] Invoice Notification' | |
body = """ | |
Good day, | |
Please find attached invoice from {} for your immediate attention. | |
regards, | |
Skolo Online Learning | |
""".format(from_client) | |
message = EmailMessage(subject, body, from_email, [to_email]) | |
message.attach_file(filepath) | |
message.send() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment