This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Convert html to pdf and send email with pdf attached on Django

mattisbmx wrote on 2014-07-16:
Hi team, How I can attached the pdf file to mail?

def generate_pdf_view(request):
try:
client = pdfcrowd.Client("user", "key")
client.setPageWidth("298mm")
client.setPageHeight("210mm")
ruta = "http://google.cl"
pdf = client.convertURI(ruta)
nombrePDF = "name_pdf.pdf"

response = HttpResponse(mimetype="application/pdf")
response["Cache-Control"] = "max-age=0"
response["Accept-Ranges"] = "none"
response["Content-Disposition"] = "attachment; filename="+nombrePDF+""
response.write(pdf)

subject = 'subject'
from_email = 'from_email@gmail.com'
to = 'to_email@gmail.com'
ctx = {}
message = render_to_string('email.html', ctx)
msg = EmailMultiAlternatives(subject, message, from_email, [to])
msg.content_subtype = "html"
msg.attach_file(pdf) # Here I need to attached the pdf file
msg.send()
except pdfcrowd.Error, why:
response = HttpResponse(mimetype="text/plain")
response.write(why)
return response
support wrote on 2014-07-18:
Hello,

Try this:
email = EmailMessage(subject, message, from, [to])
email.attach("mydoc.pdf", pdf, "application/pdf")
email.send()
SquareEye wrote on 2015-02-24:
Hi - should the following work? Grateful for any advice, as it doesn't seem to...

<?php

require 'pdfcrowd.php';

try

{

$client = new Pdfcrowd("USERNAME", "API_KEY");
$pdf = $client->convertURI('http://somepage.somedomain.com');


email = EmailMessage("Some subject header", "Message body goes here.", "from@email.com", "to@email.comt");
email.attach("mydoc.pdf", $pdf, "application/pdf");
email.send();

}

catch(PdfcrowdException $e)
{
echo "Pdfcrowd Error: " . $e->getMessage();
}

?>
support wrote on 2015-02-25: