class DomPdf in Entity Print 8.2
A Entity Print plugin for the DomPdf library.
Plugin annotation
@PrintEngine(
id = "dompdf",
label = @Translation("Dompdf"),
export_type = "pdf"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_print\Plugin\PrintEngineBase implements ContainerFactoryPluginInterface, PrintEngineInterface
- class \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PdfEngineBase
- class \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\DomPdf implements ContainerFactoryPluginInterface
- class \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PdfEngineBase
- class \Drupal\entity_print\Plugin\PrintEngineBase implements ContainerFactoryPluginInterface, PrintEngineInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DomPdf
File
- src/
Plugin/ EntityPrint/ PrintEngine/ DomPdf.php, line 24
Namespace
Drupal\entity_print\Plugin\EntityPrint\PrintEngineView source
class DomPdf extends PdfEngineBase implements ContainerFactoryPluginInterface {
/**
* Name of DomPdf log file.
*
* @var string
*/
const LOG_FILE_NAME = 'log.html';
/**
* The Dompdf instance.
*
* @var \Dompdf\Dompdf
*/
protected $dompdf;
/**
* The Dompdf instance.
*
* @var \Dompdf\Options
*/
protected $dompdfOptions;
/**
* Keep track of HTML pages as they're added.
*
* @var string
*/
protected $html = '';
/**
* Keep track of whether we've rendered or not.
*
* @var bool
*/
protected $hasRendered;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ExportTypeInterface $export_type, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $export_type);
$this->dompdfOptions = new DompdfLibOptions($this->configuration);
$this->dompdfOptions
->setTempDir(\Drupal::service('file_system')
->getTempDirectory());
$this->dompdfOptions
->setFontCache(\Drupal::service('file_system')
->getTempDirectory());
$this->dompdfOptions
->setFontDir(\Drupal::service('file_system')
->getTempDirectory());
$this->dompdfOptions
->setLogOutputFile(\Drupal::service('file_system')
->getTempDirectory() . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME);
$this->dompdfOptions
->setIsRemoteEnabled($this->configuration['enable_remote']);
$this->dompdf = new DompdfLib($this->dompdfOptions);
if ($this->configuration['disable_log']) {
$this->dompdfOptions
->setLogOutputFile('');
}
$this->dompdf
->setBaseHost($request
->getHttpHost())
->setProtocol($request
->getScheme() . '://');
$this
->setupHttpContext();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.entity_print.export_type')
->createInstance($plugin_definition['export_type']), $container
->get('request_stack')
->getCurrentRequest());
}
/**
* {@inheritdoc}
*/
public static function getInstallationInstructions() {
return t('Please install with: @command', [
'@command' => 'composer require "dompdf/dompdf 0.8.0"',
]);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'enable_html5_parser' => TRUE,
'disable_log' => FALSE,
'enable_remote' => TRUE,
'cafile' => '',
'verify_peer' => TRUE,
'verify_peer_name' => TRUE,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['enable_html5_parser'] = [
'#title' => $this
->t('Enable HTML5 Parser'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['enable_html5_parser'],
'#description' => $this
->t("Note, this library doesn't work without this option enabled."),
];
$form['disable_log'] = [
'#title' => $this
->t('Disable Log'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['disable_log'],
'#description' => $this
->t("Check to disable DomPdf logging to <code>@log_file_name</code> in Drupal's temporary directory.", [
'@log_file_name' => self::LOG_FILE_NAME,
]),
];
$form['enable_remote'] = [
'#title' => $this
->t('Enable Remote URLs'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['enable_remote'],
'#description' => $this
->t('This settings must be enabled for CSS and Images to work unless you manipulate the source manually.'),
];
$form['ssl_configuration'] = [
'#type' => 'details',
'#title' => $this
->t('SSL Configuration'),
'#open' => !empty($this->configuration['cafile']) || empty($this->configuration['verify_peer']) || empty($this->configuration['verify_peer_name']),
];
$form['ssl_configuration']['cafile'] = [
'#title' => $this
->t('CA File'),
'#type' => 'textfield',
'#default_value' => $this->configuration['cafile'],
'#description' => $this
->t('Path to the CA file. This may be needed for development boxes that use SSL. You can leave this empty in production.'),
];
$form['ssl_configuration']['verify_peer'] = [
'#title' => $this
->t('Verify Peer'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['verify_peer'],
'#description' => $this
->t("Verify an SSL Peer's certificate. For development only, do not disable this in production. See https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html"),
];
$form['ssl_configuration']['verify_peer_name'] = [
'#title' => $this
->t('Verify Peer Name'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['verify_peer_name'],
'#description' => $this
->t("Verify an SSL Peer's certificate. For development only, do not disable this in production. See https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html"),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function addPage($content) {
// We must keep adding to previously added HTML as loadHtml() replaces the
// entire document.
$this->html .= (string) $content;
$this->dompdf
->loadHtml($this->html);
}
/**
* {@inheritdoc}
*/
public function send($filename, $force_download = TRUE) {
$this
->doRender();
// Dompdf doesn't have a return value for send so just check the error
// global it provides.
if ($errors = $this
->getError()) {
throw new PrintEngineException(sprintf('Failed to generate PDF: %s', $errors));
}
// The Dompdf library internally adds the .pdf extension so we remove it
// from our filename here.
$filename = preg_replace('/\\.pdf$/i', '', $filename);
// If the filename received here is NULL, force open in the browser
// otherwise attempt to have it downloaded.
$this->dompdf
->stream($filename, [
'Attachment' => $force_download,
]);
}
/**
* {@inheritdoc}
*/
public function getBlob() {
$this
->doRender();
return $this->dompdf
->output();
}
/**
* Tell Dompdf to render the HTML into a PDF.
*/
protected function doRender() {
if (!$this->hasRendered) {
$this->dompdf
->render();
$this->hasRendered = TRUE;
}
}
/**
* {@inheritdoc}
*/
protected function getError() {
global $_dompdf_warnings;
if (is_array($_dompdf_warnings)) {
return implode(', ', $_dompdf_warnings);
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public static function dependenciesAvailable() {
return class_exists('Dompdf\\Dompdf') && !drupal_valid_test_ua();
}
/**
* Setup the HTTP Context used by Dompdf for requesting resources.
*/
protected function setupHttpContext() {
$context_options = [
'ssl' => [
'verify_peer' => $this->configuration['verify_peer'],
'verify_peer_name' => $this->configuration['verify_peer_name'],
],
];
if ($this->configuration['cafile']) {
$context_options['ssl']['cafile'] = $this->configuration['cafile'];
}
// If we have authentication then add it to the request context.
if (!empty($this->configuration['username'])) {
$auth = base64_encode(sprintf('%s:%s', $this->configuration['username'], $this->configuration['password']));
$context_options['http']['header'] = [
'Authorization: Basic ' . $auth,
];
}
$http_context = stream_context_create($context_options);
$this->dompdf
->setHttpContext($http_context);
}
/**
* {@inheritdoc}
*/
protected function getPaperSizes() {
return array_combine(array_keys(CPDF::$PAPER_SIZES), array_map('ucfirst', array_keys(CPDF::$PAPER_SIZES)));
}
/**
* {@inheritdoc}
*/
public function getPrintObject() {
return $this->dompdf;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DomPdf:: |
protected | property | The Dompdf instance. | |
DomPdf:: |
protected | property | The Dompdf instance. | |
DomPdf:: |
protected | property | Keep track of whether we've rendered or not. | |
DomPdf:: |
protected | property | Keep track of HTML pages as they're added. | |
DomPdf:: |
public | function |
Add a string of HTML to a new page. Overrides PrintEngineInterface:: |
|
DomPdf:: |
public | function |
Form constructor. Overrides PdfEngineBase:: |
|
DomPdf:: |
public static | function |
Creates an instance of the plugin. Overrides PrintEngineBase:: |
|
DomPdf:: |
public | function |
Gets default configuration for this plugin. Overrides PdfEngineBase:: |
|
DomPdf:: |
public static | function |
Checks if the Print engine dependencies are available. Overrides PrintEngineInterface:: |
|
DomPdf:: |
protected | function | Tell Dompdf to render the HTML into a PDF. | |
DomPdf:: |
public | function |
Gets the binary data for the printed document. Overrides PrintEngineInterface:: |
|
DomPdf:: |
protected | function | ||
DomPdf:: |
public static | function |
Gets the installation instructions for this Print engine. Overrides PrintEngineBase:: |
|
DomPdf:: |
protected | function |
Gets the paper sizes supported. Overrides PdfEngineBase:: |
|
DomPdf:: |
public | function |
Gets the object for this Print engine. Overrides PrintEngineInterface:: |
|
DomPdf:: |
constant | Name of DomPdf log file. | ||
DomPdf:: |
public | function |
Send the Print contents to the browser. Overrides PrintEngineInterface:: |
|
DomPdf:: |
protected | function | Setup the HTTP Context used by Dompdf for requesting resources. | |
DomPdf:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PrintEngineBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PrintEngineBase:: |
protected | property | The export type plugin. | |
PrintEngineBase:: |
public | function | ||
PrintEngineBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PrintEngineBase:: |
public | function |
Gets the export type. Overrides PrintEngineInterface:: |
|
PrintEngineBase:: |
constant | |||
PrintEngineBase:: |
constant | |||
PrintEngineBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
PrintEngineBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
2 |
PrintEngineBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
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. |