phphtmllaravelbrowsershot

How to output PDF string to a smaller portion of the page (or iframe)?


I'm using Spatie Browsershot to create PDF files. Sometimes I don't need to actually save the PDF but just display it in the browser.

I can use the following method to display the PDF in the entire page:

public function index(Request $request)
{
    $pdf = Browsershot::html('<h1>Test</h1>')
        ->pdf();
    return response($pdf, 200)
        ->header('Content-Type', 'application/pdf');
}

However I want to display the PDF in a smaller window inside the page, or a modal (or an iframe).

How can I do that?

The $pdfcontains a string that looks like that:

b"
%PDF-1.4
%ËÙÚß
1 0 obj
<</Creator (Chromium)
/Producer (Skia/PDF m126)
/CreationDate (D:20240716080820+00'00')
/ModDate (D:20240716080820+00'00')>>
endobj
3 0 obj
<</ca 1
/BM /Normal>>

.. some long string..

/Root 15 0 R
/Info 1 0 R>>
startxref
12499
%%EOF

Is there a way to output this PDF inside smaller element?

The following didn't work:

// controller
public function index(Request $request)
{
    $pdf = Browsershot::html('<h1>Test</h1>')
        ->pdf();
    return view('index', ['pdf' => $pdf]);
}

And then in the view:

<iframe src="{{$pdf}}"></iframe>

Solution

  • <iframe src="{{ route('your.pdf.route.name') }}"></iframe>
    

    and in your controller serving that route:

    public function index(Request $request)
    {
        $pdf = Browsershot::html('<h1>Test</h1>')
            ->pdf();
        return response($pdf, 200)
            ->header('Content-Type', 'application/pdf');
    }