Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Beautiful confirmation emails
Lecture: HTML to PDF file

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We've got our HTML. How do we turn that into a PDF? Let me introduce you to pdfkit. So pdfkit is a cool library we can use in Python
0:10 that will convert HTML to pdf using Web kit. Super, super cool. So all we gotta do is pip install pdfkit.
0:17 However, there is this runtime dependency on wkHTMLtopdf thing so you can install it on ubuntu and other other Linux systems like that.
0:29 there, you can get it from their site. You can run the script If you're on Windows,
0:33 you can check out their system for their homepage for binary installers and things like that So in addition to pip install,
0:40 in order to make this step work, you have to make sure you install this wkHTMLpdf thing. Now I believe I've already set that up correctly.
0:49 We're gonna find out in a minute, but let's go ahead and over and say we're using pdf kit.
0:54 I'm gonna go ahead and pip install it here, so I can see what happens. Alright. Installed with near zero delay, not much effort, so that was good.
1:05 And then like we did for this. It's about equally complicated, but it's also not worth going too much details.
1:12 Let me just drop this in here and I'll give it to you. So we're gonna go over here and use pdf kit, and we're going to generate a temporary file.
1:21 I'm just gonna put it in the current working directory. You probably want to put it in a better temporary location,
1:25 but for this little demo is just going here when you create this random file there And here's the magic pdfkit. from_string,
1:34 given the HTML, just store it in that file, and then what we're actually going to do is read the bytes back and then delete it the way you delete it.
1:42 Using path lib is unlinked kind of funky, but that's how you do it, and we're going to base64.
1:48 Encode this because what we need to do is attach the content attached the attachment to
1:53 the email as base64. I'm going to set a little break point here before we delete the file, so we can go look at it.
1:59 So let's call our build_pdf() over here instead of returned then the invoice_HTML and I'll just print out the pdf.
2:09 Remember, this is the base64 encoded text, but at least we'll see something and let's hit it.
2:30 All right, let's give it a shot. Running the debug and see what happens, takes just a minute to generate that look at
2:38 our pdf bytes. They look kind of crazy, but what about our file? There it is. So it's over in chapter seven with this great long u-u id identified.
2:48 Let's go see if we can find it. Well, look at that right there. Little previews looking pretty good if we open it. There you have it cloud city cake.
2:57 But there's our address build to so and so perfect. All the information, right? The invoice number. Just the order id basically the 17 gold coins.
3:08 And here's what we bought a small red velvet cake with lemon frosting and chocolate topping
3:12 Awesome. And we've got it as base64 encoded text right here that will be able to attach to our outbound E-Mail.


Talk Python's Mastodon Michael Kennedy's Mastodon