Can you clarify how to use the options header-html and footer-html? Is this a blade template, actual HTML text, or a path to a html file somewhere.
I tried this, but without success:
return \PDF::loadHTML($view)
->setPaper('a5')
->setOption('header-html',$header)
->setOption('footer-html',$footer)
->stream('report.pdf');
Header and Footer are renderized with success.
now i have made some modifications in my code:
in the controller i have created a function to generate a temporary file in the controller:
/**
* Creates a temporary file.
* The file is not created if the $content argument is null
*
* @param string $content Optional content for the temporary file
* @param string $extension An optional extension for the filename
*
* @return string The filename
*/
protected function createTemporaryFile($content = null, $extension = null){
$filename = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . uniqid('easymed_emr', true);
if (null !== $extension) {
$filename .= '.'.$extension;
}
if (null !== $content) {
file_put_contents($filename, $content);
}
$this->temporaryFiles[] = $filename;
return $filename;
}
the function above was extracted from AbstractGenerator from Knp Snappy
and than i call this function in \PDF::loadHTML:
return \PDF::loadHTML($view)
->setPaper('a5')
->setOption('header-html',$this->createTemporaryFile($header,'html'))
->setOption('footer-center',utf8_decode('P谩gina [page] de [topage] - Impresso em '.date('\ d/m/Y\ \ \脿\s\ H:i')))
->stream('report.pdf');
this temp file was created in system temp file:
C:\Users\username\AppData\Local\Temp
So those options require the full path name? Perhaps I can create a simple helper method for this to load a view or something. Also, isn't that method available already, without having to copy it?
You can create a route, but my route was protected by auth and the wkhtmltopdf cant load the page.
Not sure if I am missing something about your requirements - but simply creating my header as an html file and then doing this, did the trick:
$pdf->loadView('quote.rendered_blockquote', ['quote' => $quote])
->setOption('header-html', url().'/header.html');`
@slickorange There is a clear preference to avoid a creation of static html files.
That's why the modern frameworks have routes that render views, without a need of single files existence like in the past.
@barryvdh It would be very useful to have an ability to use a view (or even better- a partial), together with header-html and footer-html options. Do you think it's possible and not too hard to implement?
@juniorrosul Can you please provide some more information about your temporary file approach? Where did you put the createTemporaryFile() function? Do you store a plain HTML in $content, and how do you generate it?
@MeiRct, sorry for the long time, here the answers:
createTemporaryFile() function in BaseController$content = \View::make('template')->render();->setOption('header-html', $header);
Just remember that your header/footer need to have a DOCTYPE
Add
<!DOCTYPE html>
at the beginning of header/footer and
$pdf = PDF::loadHTML($content)
->setPaper('a4')
->setOption('header-html', $header);
will work perfectly.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Any issues with PDF rendering itself that are not directly related to this package, should be reported on https://github.com/KnpLabs/snappy instead. When having doubts, please try to reproduce the issue with just snappy.
If you believe this is an actual issue with the latest version of laravel-snappy, please reply to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.
Most helpful comment
->setOption('header-html', $header);Just remember that your header/footer need to have a DOCTYPE
Add
<!DOCTYPE html>at the beginning of header/footer and
will work perfectly.