python - Any way to create PDF file with wkhtmltopdf without returning HttpResponse or using URL? I just want to attach PDF file to email -



python - Any way to create PDF file with wkhtmltopdf without returning HttpResponse or using URL? I just want to attach PDF file to email -

i'm using wkhtmltopdf wrapper generate template pdf in django 1.6. works fine when want display pdf afterwards or send pdf file httpresponse download want create file in tmp folder , attach email.

i'm not sure how accomplish this.

# views.py context = { 'products_dict': products_dict, 'main_categories': main_categories, 'user_category': user_category } response = pdftemplateresponse(request=request, context=context, template="my_template.html", filename="filename.pdf", show_content_in_browser=true, cmd_options={'encoding': 'utf8', 'quiet': true, 'orientation': 'landscape', } ) homecoming response

the code above generate pdf how want it. thing don't want display pdf in browser or start download (i don't want homecoming response). want create , attach file email this:

email = emailmessage() email.subject = "subject" email.body = "your pdf" email.from_email = "sender@gmail.com" email.to = [ "receiver@gmail.com", ] # attach pdf file email email.attach_file(my_pdf_file_here) # send email email.send()

i tried utilize subprocess doesn't seem can send context template render before generating pdf.

edit (solution): daniel roseman help go towards wanted. did utilize tests file of wkhtmltopdf here: http://pydoc.net/python/django-wkhtmltopdf/1.1/wkhtmltopdf.tests.tests/

this did in view:

order = order.objects.get(id=order_id) return_file = "tmp/" + 'order' + str(order_id) + '.pdf' context = { 'order' : order, 'items' : order.ordered_products.all() } response = pdftemplateresponse(request=request, context=context, template="'products/order_pdf.html'", cmd_options={ 'encoding': 'utf8', 'quiet': true } ) temp_file = response.render_to_temporary_file("products/order_pdf.html") wkhtmltopdf(pages=[temp_file.name], output=return_file)

you can utilize return_file in email.attach() method this:

email.attach_file(return_file)

you can omit output parameter in wkhtmltopdf method. method homecoming output , utilize output in attach_file() method.

your issue not wkhtmltopdf, django-wkhtmltopdf provides class-based views renders wkhtmltopdf. if don't want view, don't need utilize them: render template , pass result string command-line wkhtmltopdf tool.

it looks django-wkhtmltopdf library provide utility functions (in utils directory) might create lastly stage bit easier.

python django pdf wkhtmltopdf

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -