View source
<?php
namespace Drupal\entity_print\Plugin\EntityPrint\PrintEngine;
use Dompdf\Dompdf as DompdfLib;
use Dompdf\Options as DompdfLibOptions;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\entity_print\Plugin\ExportTypeInterface;
use Drupal\entity_print\PrintEngineException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Dompdf\Adapter\CPDF;
class DomPdf extends PdfEngineBase implements ContainerFactoryPluginInterface {
const LOG_FILE_NAME = 'log.html';
protected $dompdf;
protected $dompdfOptions;
protected $html = '';
protected $hasRendered;
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();
}
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());
}
public static function getInstallationInstructions() {
return t('Please install with: @command', [
'@command' => 'composer require "dompdf/dompdf 0.8.0"',
]);
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'enable_html5_parser' => TRUE,
'disable_log' => FALSE,
'enable_remote' => TRUE,
'cafile' => '',
'verify_peer' => TRUE,
'verify_peer_name' => TRUE,
];
}
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;
}
public function addPage($content) {
$this->html .= (string) $content;
$this->dompdf
->loadHtml($this->html);
}
public function send($filename, $force_download = TRUE) {
$this
->doRender();
if ($errors = $this
->getError()) {
throw new PrintEngineException(sprintf('Failed to generate PDF: %s', $errors));
}
$filename = preg_replace('/\\.pdf$/i', '', $filename);
$this->dompdf
->stream($filename, [
'Attachment' => $force_download,
]);
}
public function getBlob() {
$this
->doRender();
return $this->dompdf
->output();
}
protected function doRender() {
if (!$this->hasRendered) {
$this->dompdf
->render();
$this->hasRendered = TRUE;
}
}
protected function getError() {
global $_dompdf_warnings;
if (is_array($_dompdf_warnings)) {
return implode(', ', $_dompdf_warnings);
}
return FALSE;
}
public static function dependenciesAvailable() {
return class_exists('Dompdf\\Dompdf') && !drupal_valid_test_ua();
}
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 (!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);
}
protected function getPaperSizes() {
return array_combine(array_keys(CPDF::$PAPER_SIZES), array_map('ucfirst', array_keys(CPDF::$PAPER_SIZES)));
}
public function getPrintObject() {
return $this->dompdf;
}
}