0

I am trying to use chrome-php/chrome to load a Blade view and then output it to the browser instead of saving it.

However using the docs in their GitHub I could not construct a working code that does that.

I tried:

public function createPdf(Request $request)
{
    $page = $browser->createPage();
    $page->navigate('http://example.com/path-to-blade-file')->waitForNavigation();
    $html = $page->getHtml(10000);
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename=filename.pdf');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');

    // How to then use the HTML from the Blade to display the PDF?
}

but then I could not use their method to output directly to the browser with echo base64_decode($pdf->getBase64()) because I have an HTML string and not $pdf object, and I could not find a way to create PDF from HTML

Also, is there a better way to load the Blade view instead of using the full URL?

pileup
  • 1
  • 2
  • 18
  • 45
  • If you have DomPDF Package installed in laravel then export the data to PDF – Ken Lee May 13 '23 at 15:39
  • I can't because dompdf doesn't support RTL well so I needed this package because from what I tested it works really well (But so far I've only used it by saving files, not to output directly to browser yet). Or, it should work now because `chrome-php` did the job of rendering it and dompdf just needs to output it and it should work? – pileup May 13 '23 at 15:41

2 Answers2

0

I managed to get it working, I first needed to convert the blade view to raw HTML string, then set the page contents of the headless chrome instance using setHtml(), then call the pdf() method on the page and output that after decoding its base64 value:

use HeadlessChromium\BrowserFactory;

    public function createPdf(Request $request)
    {
        $browserFactory = new BrowserFactory();

        // starts headless chrome
        $browser = $browserFactory->createBrowser();

        try {
            // convert blade view to raw HTML string
            $bladeViewRawHtmlString = view('blade-view')->render();

            // creates a new page and set the HTML
            $page = $browser->createPage();            
            $page->setHtml($bladeViewRawHtmlString);

            // pdf
            header('Content-Description: File Transfer');
            header('Content-Type: application/pdf');
            header('Content-Disposition: inline; filename=filename.pdf');
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');

            $pdf = $page->pdf(['printBackground' => false]);
            echo base64_decode($pdf->getBase64());
        } finally {
            // bye
            $browser->close();
        }
    }
pileup
  • 1
  • 2
  • 18
  • 45
0

I used to save PDF files in Storage path, downloading it with Storage::download('path.pdf').

Michu
  • 122
  • 12