B L Praveen
B L Praveen

Reputation: 1990

Laravel Maatwebsite Export PDF

Converting HTML to PDF using Maatwebsite I could not achieve below pdf page setting options. Instead, I switched to laravel-dompdf.

With Laravel DomPDF html convert to pdf with few styling issues which I could fix later like background-url replaced with img tag and some list replaced with table tag

 $options = [
                'defaultFont' => 'sans-serif',
                'isRemoteEnabled' => TRUE,
                'isJavascriptEnabled' => FALSE,
                'debugKeepTemp' => TRUE,
                'isHtml5ParserEnabled' => TRUE,
                'enable_html5_parser' => TRUE,
            ];
            return Pdf::setOptions($options)
            ->loadHTML($html)
            ->setWarnings(false)
            ->setPaper('a3', 'landscape')
            ->download('export_tickets.pdf');

Laravel dompdf also gives error when using google fonts.

    <link
      href="https://fonts.googleapis.com/css2?family=Varela+Round&display=swap"
      rel="stylesheet"
    />

Two Issues

1)Maatwebsite and dompdf gives error using Google fonts

2)In Maatwebsite How to set following options

                'defaultFont' => 'sans-serif',
                'isRemoteEnabled' => TRUE,
                'isJavascriptEnabled' => FALSE,
                'debugKeepTemp' => TRUE,
                'isHtml5ParserEnabled' => TRUE,
                'enable_html5_parser' => TRUE,

            ->setWarnings(false)
            ->setPaper('a3', 'landscape')

Upvotes: 0

Views: 1096

Answers (1)

M69k65y
M69k65y

Reputation: 647

Let me address the two points separately:

Issue using Google Fonts

From the first option in this answer, possibly the easiest way to get external font files is to use the @font-face rule. In your case, this would look like this:

<style>
    @font-face {
        font-family: 'Varela';
        font-style: normal;
        font-weight: normal;
        src: url("https://fonts.googleapis.com/css2?family=Varela+Round") format('truetype');
    }
    
    /* Specify the elements to style */
    html {
        font-family: 'Varela', sans-serif;
    }
</style>

Since laravel-dompdf and Maatwebsite (Dompdf) work similarly, this should work across either of them. (Tested with laravel-dompdf)

Set Maatwebsite PDF Options

Since Maatwebsite uses PhpSpreadsheet, you can use a custom writer. In your case, this could look something like:

// \App\Helpers\CustomDompdf.php
// This is based on the working of PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf.

namespace App\Helpers;

use PhpOffice\PhpSpreadsheet\Writer\Pdf;

class CustomDompdf extends Pdf
{
    protected function createExternalWriterInstance()
    {
        $instance = new \Dompdf\Dompdf();
        $options = $instance->getOptions();
        $options->setDefaultFont("Courier");
        $options->setDefaultPaperSize("a3");
        $options->setDefaultPaperOrientation("landscape");
        $options->setIsRemoteEnabled(true);
        $options->setIsJavascriptEnabled(true);
        $options->setDebugKeepTemp(true);
        // Note that this is deprecated.
        $options->setIsHtml5ParserEnabled(true);
        $instance->setOptions($options);

        return $instance;
    }

    /**
     * Save Spreadsheet to file.
     *
     * @param string $filename Name of the file to save as
     */
    public function save($filename, int $flags = 0): void
    {
        $fileHandle = parent::prepareForSave($filename);

        //  Create PDF
        $pdf = $this->createExternalWriterInstance();

        $pdf->loadHtml($this->generateHTMLAll());
        $pdf->render();

        //  Write to file
        fwrite($fileHandle, $pdf->output() ?? '');

        parent::restoreStateAfterSave();
    }
    
}

To use this writer:

\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('CustomPdf', \App\Helpers\CustomDompdf::class);

// Return the response as a PDF.
return Excel::download(new YourExcelExport, "File Name.pdf", "CustomPdf");

Upvotes: 1

Related Questions