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