View source
<?php
namespace Drupal\pdf_using_mpdf\Conversion;
use Drupal\node\NodeInterface;
use Drupal\pdf_using_mpdf\ConvertToPdfInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Utility\Token;
use Drupal\file\Entity\File;
use Mpdf\Mpdf;
use Mpdf\Output\Destination;
class ConvertToPdf implements ConvertToPdfInterface {
protected $renderer;
protected $mpdf;
protected $token;
protected $configFactory;
protected $settings;
protected $node;
protected $filename;
public function __construct(RendererInterface $renderer, ConfigFactoryInterface $config_factory, Token $token) {
$this->renderer = $renderer;
$this->configFactory = $config_factory;
$this->token = $token;
$node = \Drupal::routeMatch()
->getParameter('node');
if ($node instanceof NodeInterface) {
$this->node = $node;
}
}
protected function getConfigValues() {
return $this->configFactory
->getEditable('pdf_using_mpdf.settings')
->get('pdf_using_mpdf');
}
protected function init() {
$mpdf_config = $this
->getDefaultConfig();
$this->mpdf = new Mpdf($mpdf_config);
}
public function convert($html) {
$this->settings = $this
->getConfigValues();
if (empty($html)) {
drupal_get_messages('error');
drupal_set_message(t('No content. PDF cannot be generated for this path.'), 'error');
return;
}
$this
->generator($html);
$this
->showPdf();
}
public function convertToString($html) {
$this->settings = $this
->getConfigValues();
if (empty($html)) {
drupal_get_messages('error');
drupal_set_message(t('No content. PDF cannot be generated for this path.'), 'error');
return;
}
$this
->generator($html);
return $this->mpdf
->Output('', Destination::STRING_RETURN);
}
public function getFilename() {
return $this->filename;
}
protected function generator($html) {
$styles = $this
->importStyles();
$this
->init();
$this
->setHeader();
if (!empty($styles)) {
$this->mpdf
->WriteHTML($styles, 1);
}
$this->mpdf
->WriteHTML($html, 2);
$this
->applyProperties();
$this
->setFooter();
$this
->setFilename();
}
protected function setHeader() {
$header = $this->settings['pdf_header'];
if (isset($header) && $header != NULL) {
$header = $this
->replaceTokens($header);
$this->mpdf
->SetHTMLHeader($header);
}
}
protected function applyProperties() {
$watermark_option = $this->settings['watermark_option'];
$watermark_opacity = $this->settings['watermark_opacity'];
if ($watermark_option == 0) {
$text = $this->settings['pdf_watermark_text'];
if (!empty($text)) {
$this->mpdf
->SetWatermarkText($text, $watermark_opacity);
$this->mpdf->showWatermarkText = TRUE;
}
}
else {
$image_id = $this->settings['watermark_image'];
if (isset($image_id[0])) {
$file = File::load($image_id[0]);
$image_path = $file
->getFileUri();
$image_path = file_create_url($image_path);
$this->mpdf
->SetWatermarkImage($image_path, $watermark_opacity);
$this->mpdf->showWatermarkImage = TRUE;
}
}
$title = $this->settings['pdf_set_title'];
if (!empty($title)) {
$this->mpdf
->SetTitle($title);
}
$author = $this->settings['pdf_set_author'];
if (!empty($author)) {
$this->mpdf
->SetAuthor($author);
}
$subject = $this->settings['pdf_set_subject'];
if (isset($subject) && $subject != NULL) {
$this->mpdf
->SetSubject($subject);
}
$creator = $this->settings['pdf_set_creator'];
if (!empty($creator)) {
$this->mpdf
->SetCreator($creator);
}
$password = $this->settings['pdf_password'];
if (!empty($password)) {
$this->mpdf
->SetProtection([
'print',
'copy',
], $password, $password);
}
}
protected function setFooter() {
$footer = $this->settings['pdf_footer'];
if (isset($footer) && $footer != NULL) {
$footer = $this
->replaceTokens($footer);
$this->mpdf
->SetHTMLFooter($footer);
}
}
protected function setFilename() {
$this->filename = $this
->replaceTokens($this->settings['pdf_filename']);
}
public function showPdf() {
switch ($this->settings['pdf_save_option']) {
case 0:
$this->mpdf
->Output($this->filename . '.pdf', Destination::INLINE);
break;
case 1:
$this->mpdf
->Output($this->filename . '.pdf', Destination::DOWNLOAD);
break;
case 2:
$folder = \Drupal::service('file_system')
->realpath(file_default_scheme() . "://");
$folder .= '/pdf_using_mpdf';
$this->mpdf
->Output($folder . '/' . $this->filename . '.pdf', Destination::FILE);
drupal_set_message(t('PDF File <em>@filename</em> has been saved to the server.', [
'@filename' => $this->filename . '.pdf',
]));
break;
}
}
public function getDefaultConfig() {
$orientation = $this->settings['orientation'] == 'L' ? '-L' : '';
$config = [
'tempDir' => file_directory_temp(),
'useActiveForms' => TRUE,
'format' => $this->settings['pdf_page_size'] . $orientation,
'default_font_size' => $this->settings['pdf_font_size'] . 'pt',
'default_font' => $this->settings['pdf_default_font'],
'margin_left' => $this->settings['margin_left'],
'margin_right' => $this->settings['margin_right'],
'margin_top' => $this->settings['margin_top'],
'margin_bottom' => $this->settings['margin_bottom'],
'margin_header' => $this->settings['margin_header'],
'margin_footer' => $this->settings['margin_footer'],
'dpi' => $this->settings['dpi'],
'img_dpi' => $this->settings['img_dpi'],
'enabled_content_types' => $this->settings['enabled_content_types'],
];
return $config;
}
protected function importStyles() {
$file = '';
$path = DRUPAL_ROOT . '/' . $this->settings['pdf_css_file'];
if (file_exists($path)) {
$file = file_get_contents($path);
}
return $file;
}
protected function replaceTokens($text) {
$data = [];
if ($this->node) {
$data['node'] = $this->node;
}
return $this->token
->replace($text, $data);
}
}