Skip to content

Instantly share code, notes, and snippets.

@LarryBarker
Created October 23, 2019 01:10
Show Gist options
  • Save LarryBarker/49869934f92a2878f0986f50dbe97616 to your computer and use it in GitHub Desktop.
Save LarryBarker/49869934f92a2878f0986f50dbe97616 to your computer and use it in GitHub Desktop.
AJAX PDF Download
/**
* AJAX handler to generate and return a PDF of the desired report
*
* @param string $shareCode The share code for the report
* @return Response PDF download response
*/
public function view_onDownloadPdf($shareCode)
{
return Backend::redirect("author/plugin/download-pdf/$shareCode");
}
/**
* Download the generated report as a PDF
*
* @param string $shareCode The share code for the report
* @return Response
*/
public function downloadPdf($shareCode)
{
$report = Report::getFromShareCode($shareCode);
if (!$report) {
abort(404);
}
$this->view($shareCode);
$html = $this->makeView('view');
$pdf = SnappyPDF::loadHTML($html)
->setOption('margin-top', 10)
->setOption('margin-bottom', 10)
->setOption('margin-left', 8)
->setOption('margin-right', 8)
->setPaper('letter')
->output();
$response = Response::make();
$response->header('Content-Type', 'application/pdf');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Content-Disposition', sprintf('%s; filename="%s"', 'attachment', str_slug($report->name) . '.pdf'));
$response->setContent((string) $pdf);
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment