public function DomPdfGenerator::getResponse in PDF Generator 8
Same name and namespace in other branches
- 2.0.x src/DomPdfGenerator.php \Drupal\pdf_generator\DomPdfGenerator::getResponse()
Render usin dompdf with given options.
Parameters
string $title: The title of the pdf.
array $content: The build array to be rendered.
bool $preview: If enabled the html will be printed without render the pdf.
array $options: The options array to perform pdf.
string $pageSize: The size of the page.
string $disposition: The disposition of the page, portrait or landscape.
string $cssText: Text to load additional css.
string $cssPath: Path to load additional css.
File
- src/
DomPdfGenerator.php, line 103
Class
- DomPdfGenerator
- Defines helpers methods to help in managing config which used in SCity.
Namespace
Drupal\pdf_generatorCode
public function getResponse($title, array $content, $preview = FALSE, array $options = [], $pageSize = 'A4', $disposition = 'portrait', $cssText = NULL, $cssPath = NULL) {
$request = $this->requestStack
->getCurrentRequest();
$base_url = $request
->getSchemeAndHttpHost();
// By default we load some options.
// The user can override these options.
foreach ($options as $key => $option) {
$this->options
->set($key, $option);
}
// Dompdf needs to be initialized with custom options if they are supplied.
$this->dompdf = new Dompdf($this->options);
$css = file_get_contents(drupal_get_path('module', 'pdf_generator') . '/css/pdf.css');
// Add inline css from text.
if (!empty($cssText)) {
$css .= "\r\n";
$css .= $cssText;
}
// Add inline css from file.
if (!empty($cssPath) && file_exists($cssPath)) {
$css .= "\r\n";
$css .= file_get_contents($cssPath);
}
$build = [
'#theme' => 'pdf_generator_print',
'#css' => [
'#markup' => $css,
],
'#content' => $content,
'#title' => $title,
];
if ($preview) {
return $build;
}
$html = $this->renderer
->render($build);
$html = urldecode($html);
$html = str_replace('src="' . $base_url . '/', 'src="', $html);
$html = str_replace('href="/', 'href="' . $base_url . '/', $html);
$html = str_replace('src="/', 'src="' . DRUPAL_ROOT . '/', $html);
$this->dompdf
->setOptions($this->options);
$this->dompdf
->loadHtml($html);
$this->dompdf
->setPaper($pageSize, $disposition);
$this->moduleHandler
->alter('pdf_generator_pre_render', $this->dompdf);
$this->dompdf
->render();
$response = new Response();
$response
->setContent($this->dompdf
->output());
$response->headers
->set('Content-Type', 'application/pdf');
if (is_array($title)) {
$title = $this->renderer
->render($title);
}
$filename = strtolower(trim(preg_replace('#\\W+#', '_', $title), '_'));
$response->headers
->set('Content-Disposition', "attachment; filename={$filename}.pdf");
return $response;
}