Laravel Dompdf Save To Storage

Laravel Dompdf: Save to Storage

To save a PDF generated using Dompdf library in Laravel to storage, you can follow these steps:

  1. First, you need to install the Dompdf library via Composer. Open your terminal and run the following command:
  2. composer require dompdf/dompdf
  3. Create a method in your Laravel controller that generates the PDF using Dompdf. For example:
  4.     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!'; }
  5. In your routes file, define a route to access the method in the controller:
  6. Route::get('/generate-pdf', 'YourController@generatePdf');
  7. Now, you can visit the `/generate-pdf` route in your browser to trigger the PDF generation and save it to the storage folder.

After executing these steps, the generated PDF will be saved as “example.pdf” in the `storage/app/public` directory of your Laravel project.

Similar post

Leave a comment