Reputation: 1990
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"
/>
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
Reputation: 647
Let me address the two points separately:
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
)
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