To generate a PDF from HTML in Laravel 10, you can use the "dompdf" package, which is a popular PHP library for converting HTML to PDF. Here are the steps:
You can install the Dompdf package using Composer. Open a terminal window, navigate to your Laravel project directory, and run the following command:
javascript
composer
require
dompdf/dompdf
Create a view that contains the HTML content that you want to convert to PDF. You can use Laravel's Blade templating engine to create the view. For example, create a file called "pdf.blade.php" in the "resources/views" directory, and add your HTML content:
html
<!DOCTYPE
html
>
<
html
>
<head>
<title>PDF</title>
</head>
<body>
<h1>PDF from HTML</h1>
<p>This is some sample content.</p>
</body>
</
html
>
Step 3: Generate PDF from HTML
Create a new route in your Laravel application that will generate the PDF from the HTML content. In the route's closure, create a new Dompdf object, load the HTML content from the view, and render it to PDF. Finally, send the PDF back to the user as a download:
php
use DompdfDompdf;
Route::get('/pdf', function () {
// create new Dompdf object "INIDEV"
$dompdf = new Dompdf();
// load HTML content from view "INIDEV"
$html = view('pdf')->render();
// render HTML to PDF "INIDEV"
$dompdf->loadHtml($html);
$dompdf->render();
// send PDF back to user as download return "INIDEV"
$dompdf->stream('pdf-from-html.pdf'); });
You can now test the PDF generation by visiting the URL of the route you created in your browser. For example, if you created a route with the URL "/pdf", you can visit "http://your-app-url/pdf" to download the PDF.
That's it! You've now successfully generated a PDF from HTML in Laravel 10 using the Dompdf package.
ININDIA is an IT services and custom development company that provides innovative solutions to businesses of all sizes. Our team of experienced developers and designers specialize in web and mobile app development, UI/UX design, and software consulting. We are committed to delivering high-quality solutions that help our clients achieve their goals and drive their business forward.