class DomPdfGenerator in PDF Generator 8
Same name and namespace in other branches
- 2.0.x src/DomPdfGenerator.php \Drupal\pdf_generator\DomPdfGenerator
Defines helpers methods to help in managing config which used in SCity.
@TODO This will be modified to get pdf generators from plugins.
Hierarchy
- class \Drupal\pdf_generator\DomPdfGenerator uses StringTranslationTrait
Expanded class hierarchy of DomPdfGenerator
1 string reference to 'DomPdfGenerator'
1 service uses DomPdfGenerator
File
- src/
DomPdfGenerator.php, line 19
Namespace
Drupal\pdf_generatorView source
class DomPdfGenerator {
use StringTranslationTrait;
/**
* Options for dompdf.
*
* @var \Dompdf\Dompdf
*/
protected $options;
/**
* Dompdf library.
*
* @var \Dompdf\Dompdf
*/
protected $dompdf;
/**
* Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The object renderer.
*
* @var \Drupal\Core\Render\Renderer
*/
protected $renderer;
/**
* Protected requestStack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a new object.
*
* @param \Drupal\Core\Render\Renderer $renderer
* The object renderer.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The current request stack.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(Renderer $renderer, RequestStack $request_stack, ModuleHandlerInterface $module_handler) {
$this->renderer = $renderer;
$this->requestStack = $request_stack;
$this->moduleHandler = $module_handler;
$this->options = new Options();
$this->options
->set('enable_css_float', TRUE);
$this->options
->set('enable_html5_parser', TRUE);
$this->options
->set('enable_remote', TRUE);
$this->options
->set('defaultFont', 'Times');
}
/**
* Render usin dompdf with given options.
*
* @param string $title
* The title of the pdf.
* @param array $content
* The build array to be rendered.
* @param bool $preview
* If enabled the html will be printed without render the pdf.
* @param array $options
* The options array to perform pdf.
* @param string $pageSize
* The size of the page.
* @param string $disposition
* The disposition of the page, portrait or landscape.
* @param string $cssText
* Text to load additional css.
* @param string $cssPath
* Path to load additional css.
*/
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;
}
/**
* Return a list of page sizes.
*/
public function pageSizes() {
$sizes = array_keys(CPDF::$PAPER_SIZES);
return array_combine($sizes, array_map('ucfirst', $sizes));
}
/**
* Return a list disposition.
*/
public function availableDisposition() {
return [
'portrait' => $this
->t('Portrait'),
'landscape' => $this
->t('Landscape'),
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DomPdfGenerator:: |
protected | property | Dompdf library. | |
DomPdfGenerator:: |
protected | property | Module handler. | |
DomPdfGenerator:: |
protected | property | Options for dompdf. | |
DomPdfGenerator:: |
protected | property | The object renderer. | |
DomPdfGenerator:: |
protected | property | Protected requestStack. | |
DomPdfGenerator:: |
public | function | Return a list disposition. | |
DomPdfGenerator:: |
public | function | Render usin dompdf with given options. | |
DomPdfGenerator:: |
public | function | Return a list of page sizes. | |
DomPdfGenerator:: |
public | function | Constructs a new object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |