Laravel Dompdf: Save to Storage
To save a PDF generated using Dompdf library in Laravel to storage, you can follow these steps:
- First, you need to install the Dompdf library via Composer. Open your terminal and run the following command:
- Create a method in your Laravel controller that generates the PDF using Dompdf. For example:
- In your routes file, define a route to access the method in the controller:
- Now, you can visit the `/generate-pdf` route in your browser to trigger the PDF generation and save it to the storage folder.
composer require dompdf/dompdf
public function generatePdf() { // Add any required data or logic to generate the PDF $html = 'Example PDF
This is an example PDF generated using Dompdf in Laravel.
'; $dompdf = new \Dompdf\Dompdf(); $dompdf->loadHtml($html); $dompdf->render(); $output = $dompdf->output(); // Save the PDF to storage $path = storage_path('app/public/example.pdf'); file_put_contents($path, $output); return 'PDF saved to storage!'; }
Route::get('/generate-pdf', 'YourController@generatePdf');
After executing these steps, the generated PDF will be saved as “example.pdf” in the `storage/app/public` directory of your Laravel project.