1

i am using Barryvdh\DomPDF\ to generate pdf file in my laravel project , this my controller :

<?php
// Our Controller
namespace App\Http\Controllers;

use Illuminate\Http\Request;
// This is important to add here.
use Barryvdh\DomPDF\Facade as PDF;

class CustomerController extends Controller
{
    public function printPDF()
    {
        // This  $data array will be passed to our PDF blade
        $data = [
            'title' => 'First PDF for Medium',
            'heading' => 'Hello from 99Points.info',
            'content' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.'
            ];

        $pdf = PDF::loadView('pdf_view', $data);
        return $pdf->download('medium.pdf');
    }
}

how i can preview my pdf result in browser instead of download ?

thanks

roche
  • 127
  • 1
  • 2
  • 15

2 Answers2

0

Change the return to a response with the file and the file MIME type on the headers

$pdf = PDF::loadView('pdf_view', $data)->download('medium.pdf');

return response($pdf, 200)
            ->header('Content-Type', File::mimeType($pdf));
porloscerros Ψ
  • 4,808
  • 2
  • 11
  • 20
0

This works for me, I created a custom function to receive params based on each use.

use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;
use Barryvdh\DomPDF\Facade\Pdf;   

public function PDFDownload($view, $data = [], $fileName = 'File.pdf',  $paperSize = 'A4', $orientation = 'potrait') {
        $pdf =  PDF::loadView($view, $data)->setPaper($paperSize, $orientation)->setWarnings(false);
        $filePath = storage_path('app/temp/'.$fileName);

        // Save the PDF to a temporary file on the server
        Storage::put('temp/'.$fileName, $pdf->output());

        $headers = [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
        ];

        // Get the PDF content as a string
        $content = file_get_contents($filePath);

        // Delete the temporary file
        Storage::delete('temp/'.$fileName);

        // Return the response with the PDF content and headers
        return response($content, 200, $headers);
    }

If anyone else works for him/her let me know

  • I think the code I gave you will also run on mobile devices. However, there are situations when security settings on mobile devices forbid downloading files from the browser. You could consider generating the PDF and downloading it using a different method to get around this problem. One method is to encode the PDF using the base64_encode function and then return it as a response with the correct headers. Here is some sample code you could use: – Deogratias Alison May 06 '23 at 07:16