در حال بارگذاری ...

Choosing PDF tool1:08

And that's what I'm doing here. All right, so there's a number of steps here. We have a lot to figure out. Let's get going. Now my first thought is, I need to figure out how to convert this HTML into a PDF that I can save to storage. Now I was researching this yesterday at the time of this recording. And I was reminded that Spotsy's BrowserShop package can handle this pretty easily. And even better, I already use this package at LayerCast to generate social media images and screencast cards and things like that.

And even better, I already use this package at LayerCast to generate social media images and screencast cards and things like that. So I think this will end up being a great choice. Notice I can do things like this. We provide it a URL and save it as a PDF. I mean, come on, that's pretty cool. All right, let's figure out how to use it. So like I said, I already have it installed on my machine. But if you want to work along in your own projects, you would need to install Puppeteer through npm.

Installing dependencies1:54

But if you want to work along in your own projects, you would need to install Puppeteer through npm. And then also, we'll have to pull this in as a composer package right here. So we'll paste this in. There's no harm in running it again. And we should be all set to go, I think. Okay, so let's do this. Why don't we play around by creating a controller. And I'll call this, how about InvoiceDownloadsController? All right, so let's go over there, InvoiceDownloadsController.

Creating download controller2:19

And I'll call this, how about InvoiceDownloadsController. All right, so let's go over there, InvoiceDownloadsController. And yeah, for now, maybe this can be an invocable controller. Because basically, this is my download action. All right, so think about it. The first thing I need to do is render that HTML that you saw right here. And I can do that like so. Make a view, it's called invoices.show. And the only thing this view requires is the invoice that we're displaying, of course. And if the user is signed in, and we'll need to add a middleware to the route to force

And the only thing this view requires is the invoice that we're displaying, of course. And if the User is signed in, and we'll need to add a middleware to the route to force that. But if so, I can say, give me the user's subscription, and I want to grab the invoice, and we need to feed that. And that can probably be part of the URI. Something like that. Okay, so you surely know that for any controller, you can return a view, and it will be rendered as the response. But if you ever want to, instead, compile and save that to a variable, you can say view,

Adding download route3:19

as the response. But if you ever want to, instead, compile and save that to a variable, you can say view, render, and then you can save it like so. Okay, so let's dd this HTML, and have a look. But of course, before we can even run this, and let's get rid of this unused import. Before I can even run this, I need to have a route set up. So let's do that now. So let's say Route::get, and we'll do something like settings/subscription/invoices/{id}, and then at least a start. I may not even keep this endpoint, because I imagine this will be triggered as a result.

invoice ID, and then at least a start. I may not even keep this endpoint, because I imagine this will be triggered as a result of a webhook, or maybe a artisan command, or something like that. So right now, we're just sort of spiking things out. Anyways, that'll hit an InvoiceDownloadsController. And actually, because it's invocable, I should be able to provide the controller path as the second argument, instead of an array. So if I switch back to my controller, I think we're ready to give this a try. Okay, so here's our endpoint. There's the ID.

Generating PDF from HTML4:21

Okay, so here's our endpoint. There's the ID. Let's now add download. And that should hit that endpoint. And there we go. It works. We've rendered the view. Okay, so now I'm going to use that browser-shot library to convert that HTML into a PDF. And we can do that like this. I import browser-shot.

And we can do that like this. I import browser-sharp. There's an html method, and I'll feed that. And I'm sure I'll need to configure the parameters, the margin, the layout, things like that. But for now, let's just begin by saving it as a test PDF. And then I'll say return done. All right. So if we did everything correctly, the HTML from that invoice should be saved to my public directory. So if we come back and refresh, I see done.

directory. So if we come back and refresh, I see done. And now I should have a test.pdf in my public directory. And I do. Okay, cool. So actually, this is working, and it only required one line. But yeah, notice, I might want to tweak the layout, and I'd love to see a little more margin. And also, I don't see any of the backgrounds. Okay, so I'm not incredibly familiar with the browser-shot library.

Saving PDF to storage6:05

I think this is good. Let's have a look at the layout here. Ah, okay, so it defaults to letter, I guess. I was wondering if I'd have to force the page size, but that's good for me where I live. So believe it or not, this is all we had to do to convert a block of HTML into a PDF, and then save it to my file system. Now, one issue is that we're saving this invoice to the public directory of our app, which means it's publicly accessible. Why don't we instead save it to my storage path, into the app directory, and we need to give it something other than test.

Why don't we instead save it to my storage path, into the app directory, and we need to give it something other than test. Maybe I can stick with invoiceid.pdf, and then let's turn that into string interpolation. Okay, so now, if I give it another refresh, all right, so pay attention to the ID here, 692891. Let's see, 692891, and yeah, now notice it is in my storage/app directory, which means it's no longer publicly accessible, and that's what we want. Okay, very cool. We're still spiking things out, but I think we're making pretty good progress. So in the next lesson, let's figure out how to grab this PDF, attach it to an email, and

BrowsershotSave HTML as a PDF

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟