diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..ba06b9111e3fc6578fd0072d9f4675538ad063e3 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +##ABOUT + +mail_template adds a config entity for user defined twig templates, which can be +rendered into pdf using wkhtmltopdf or mail body. + +##DEPENDENCIES + +- ace_editor +- views + +##USAGE + +### Adding custom css to your pdf + +create a library in your theme/module and use hook_mail_template_render_alter(&$render) +to attach your library. + +eg: + +function hook_mail_template_render_alter(&$render) { + $render['#attached']['library'][] = 'commerce_de_wassende_maan/report'; +} + + diff --git a/css/report.css b/css/report.css index b54a3f24104aa79119c1786aca889b5b3709e3d2..ec7014676fb5e517d15f44aa081932eca57e32cd 100644 --- a/css/report.css +++ b/css/report.css @@ -1,8 +1,7 @@ body, pre { - font-size: 10px; + } body.mail-template { -/* background-color:yellow; */ } diff --git a/mail_template.module b/mail_template.module index 87a0ebfe32e3c22a57ee2a311ab118624809f31c..ffea43b26d1eb75627452adba84bc9070bc85c9a 100644 --- a/mail_template.module +++ b/mail_template.module @@ -49,13 +49,6 @@ function mail_template_views_data() { return $data; } -function mail_template_page_attachments(array &$attachments) { - - // Unconditionally attach an asset to the page. - $attachments['#attached']['library'][] = 'mail_template/report'; - -} - /** * Implements hook_mail_alter(). */ diff --git a/mail_template.routing.yml b/mail_template.routing.yml index 4dd1d585a1e8d30258cbd181751d6c819704a8bf..da108a0cc2e353dc43e96073d598b652a89ab1e1 100644 --- a/mail_template.routing.yml +++ b/mail_template.routing.yml @@ -1,7 +1,7 @@ mail_template.view_report: path: '/mail_template/download_view/{view}/{display}' defaults: - _controller: '\Drupal\mail_template\Controller\PdfTemplateController::view_report' + _controller: '\Drupal\mail_template\Controller\PdfTemplateController::viewDownload' _title: 'Download' display: 'default' requirements: @@ -12,7 +12,7 @@ mail_template.view_report: mail_template.renderTemplate: path: '/mail_template/download_template/{template}' defaults: - _controller: '\Drupal\mail_template\Controller\PdfTemplateController::renderTemplate' + _controller: '\Drupal\mail_template\Controller\PdfTemplateController::templateDownload' _title: 'Download' requirements: _permission: 'administer site configuration' diff --git a/src/Controller/PdfTemplateController.php b/src/Controller/PdfTemplateController.php index 53b6769609d84016ae7d8e0889c42270df0be726..899639320173d8c29f3e6a1416153211b19b3edd 100644 --- a/src/Controller/PdfTemplateController.php +++ b/src/Controller/PdfTemplateController.php @@ -3,6 +3,7 @@ namespace Drupal\mail_template\Controller; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Response; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; @@ -44,6 +45,38 @@ class PdfTemplateController extends ControllerBase implements ContainerInjection ); } + /** + * {@inheritdoc} + */ + protected function responseForRenderArray($renderArray) { + + $param = \Drupal::request()->query->all(); + if (isset($param['format']) && $param['format'] === 'html') { + $html = $this->mailTemplateDefault->renderRenderArray( + $renderArray); + + $response = new Response( + $html, + Response::HTTP_OK, + array('content-type' => 'text/html') + ); + return $response; + } + + $pdfOptions = []; + if (isset($param['pdfOptions'])) { + $pdfOptions = $param['pdfOptions']; + } + + $file = $this->mailTemplateDefault->pdfForRenderArrays( + [$renderArray], + $pdfOptions + ); + + return $this->mailTemplateDefault->downloadForFile( + $file, $filename = 'download.pdf'); + } + /** * Downloads a rendered pdf from template. * @@ -53,21 +86,18 @@ class PdfTemplateController extends ControllerBase implements ContainerInjection * @return response * download response */ - public function view_report(ViewEntityInterface $view, $display) { + public function viewDownload(ViewEntityInterface $view, $display) { $param = \Drupal::request()->query->all(); $context = [ 'display' => $display, ]; - if (isset($param['format']) && $param['format'] === 'html') { - return $this->mailTemplateDefault->render_array_from_view( - $view->id(), - $context); - } - $file = $this->mailTemplateDefault->render_arrays_to_pdf([ - $this->mailTemplateDefault->render_array_from_view($view->id(), $context)]); - return $this->mailTemplateDefault->pdf_download_for_file($file, $filename='download.pdf'); + return $this->responseForRenderArray( + $this->mailTemplateDefault->renderArrayForView( + $view->id(), + $context) + ); } /** @@ -81,19 +111,12 @@ class PdfTemplateController extends ControllerBase implements ContainerInjection * @return response * download response */ - public function renderTemplate(TemplateInterface $template, array $context = []) { - $param = \Drupal::request()->query->all(); - - if (isset($param['format']) && $param['format'] === 'html') { - return $this->mailTemplateDefault->render_array_for_template( + public function templateDownload(TemplateInterface $template, array $context = []) { + return $this->responseForRenderArray( + $this->mailTemplateDefault->renderArrayForTemplate( $template->id(), - $context); - } - - $file = $this->mailTemplateDefault->render_arrays_to_pdf([ - $this->mailTemplateDefault->render_array_for_template($template->id(), $context), - ]); - return $this->mailTemplateDefault->pdf_download_for_file($file, $filename = 'download.pdf'); + $context) + ); } } diff --git a/src/MailTemplateService.php b/src/MailTemplateService.php index aa1ec0c023d1017d52083e1b541b506cd84e56b4..5312700ac410e70a4cd3116f1bdcbad29cefd010 100644 --- a/src/MailTemplateService.php +++ b/src/MailTemplateService.php @@ -63,191 +63,73 @@ class MailTemplateService implements MailTemplateServiceInterface { $this->bareHtmlPageRenderer = $bare_html_page_renderer; } - /* - public function generate_pdf($render_arrays) { - //TODO: review if it can be merged with methods below: used for taxshelter - $pdf = new Pdf(array( - 'no-outline', - 'margin-top' => 0, - 'margin-right' => 0, - 'margin-bottom' => 0, - 'margin-left' => 0, - - // Default page options - 'disable-smart-shrinking', - )); - - foreach ($render_arrays as $render_array) { - $html = '<html><body style="margin:0; padding:0;">'; - $html.= drupal_render($render_array); - $html .= '</body></html>'; - - $pdf->addPage($html); - } - - $output = file_unmanaged_save_data($pdf->toString()); - return $output; + /** + * {@inheritdoc} + */ + public function augmentRenderArray(&$render) { + $render['#attached']['library'][] = 'mail_template/report'; + $render['#attached']['html_head'][] = [ + [ + '#type' => 'html_tag', + '#tag' => 'base', + '#attributes' => [ + 'href' => Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), + ], + ], + 'base', + ]; + \Drupal::moduleHandler()->alter('mail_template_render', $render); + return $render; } - */ /** * {@inheritdoc} */ - public function render_array_for_template($template_id, &$context) { + public function renderArrayForTemplate($template_id, &$context) { $template = \Drupal::entityTypeManager() - ->getStorage('template') - ->load($template_id); + ->getStorage('template') + ->load($template_id); if (!$template) { return FALSE; } - - $default_theme = $this->themeHandler->getDefault(); - /* - // set active theme for library overrides - if (trim($template->theme_name) && array_key_exists(trim($template->theme_name), $this->themeHandler->listInfo())) { - $default_theme = trim($template->theme_name); - if (strcmp($this->themeManager->getActiveTheme()->getName(), $default_theme)) { - $this->themeManager->setActiveTheme($this->themeInitialization->initTheme($default_theme)); - } - } - */ - // context for template - $theme_path = $this->themeHandler->getTheme($default_theme)->getPath(); - $context['theme_path'] = drupal_realpath($theme_path); - - /* - # TODO: more standard way to render links to css / js files - $css = \Drupal::service('library.discovery')->getLibraryByName('mail_template', 'report'); - $css_files = ''; - if (isset($css['css'])) { - foreach ($css['css'] as $css_file) { - $file_path = drupal_realpath($css_file['data']); - $css_files .= "<link rel=\"stylesheet\" href=\"{$file_path}\" media=\"all\" />"; - } - } - */ - $context['base_url'] = $GLOBALS['base_url']; - $content_array = [ '#type' => 'inline_template', '#template' => $template->getTemplate()['value'], '#context' => $context, ]; - /* - $content = drupal_render($content_array); - - $report_array = [ - 'report' => [ - '#theme' => 'shareholder_register_report', - '#css' => $css_files, - '#content' => $content, - ], - ]; - $html = drupal_render($report_array); - */ - + $this->augmentRenderArray($content_array); return $content_array; } - /** - * {@inheritdoc} - */ - public function render_template_to_file($template_id, &$context) { - $content = self::render_template($template_id, $context); - if ($content) { - $tmpfile = new File($content, '.html'); - return $tmpfile; - } else { - return FALSE; - } - } - - /** - * {@inheritdoc} - */ - public function pdf_from_template($template_id, &$context) { - if ($header = self::render_template_to_file("{$template_id}_header", $context)) { - $pdf_options['header-html'] = $header->getFileName(); - } elseif ($header = self::render_template_to_file("report_header", $context)) { - $pdf_options['header-html'] = $header->getFileName(); - } - - if ($footer = self::render_template_to_file("{$template_id}_footer", $context)) { - $pdf_options['footer-html'] = $footer->getFileName(); - } elseif ($footer = self::render_template_to_file("report_footer", $context)) { - $pdf_options['footer-html'] = $footer->getFileName(); - } - - $html = self::render_template($template_id, $context); - - $pdf = new Pdf(array( - 'encoding' => 'UTF-8', - 'no-outline', - 'margin-top' => 0, - 'margin-right' => 0, - 'margin-bottom' => 0, - 'margin-left' => 0, - - // Default page options - 'disable-smart-shrinking', - )); - $pdf->addPage($html); - $output = file_unmanaged_save_data($pdf->toString()); - return $output; - } /** * {@inheritdoc} */ - public function render_array_from_view($view, &$context) { + public function renderArrayForView($view, &$context) { if (isset($context['display'])) { $r = views_embed_view($view, $context['display']); } else { $r = views_embed_view($view); } - $r['#attached']['library'][] = 'mail_template/report'; - $r['#attached']['library'][] = 'commerce_de_wassende_maan/report'; - $r['#attached']['html_head'][] = [ - [ - '#type' => 'html_tag', - '#tag' => 'base', - '#attributes' => [ - 'href' => Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), - ], - ], - 'base', - ]; + $this->augmentRenderArray($r); return $r; } /** * {@inheritdoc} */ - public function render_render_array($render_array, $title = '') { - $render_array['#attached']['library'][] = 'mail_template/report'; - $render_array['#attached']['library'][] = 'commerce_de_wassende_maan/report'; - $render_array['#attached']['html_head'][] = [ - [ - '#type' => 'html_tag', - '#tag' => 'base', - '#attributes' => [ - 'href' => Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), - ], - ], - 'base', - ]; - + public function renderRenderArray($render_array, $title = '') { $r = $this->bareHtmlPageRenderer->renderBarePage($render_array, $title, 'mail-template'); $html = $r->getContent(); // FIXME: do NOT manipulate HTML using regular expressions ! // comments etc before and after the html tag must be removed, or // wkhtmltopdf produces a 0 byte pdf. - //$html = preg_replace("#^.*(<html lang=.*/html>).*$#s", '\1', $html); + // $html = preg_replace("#^.*(<html lang=.*/html>).*$#s", '\1', $html); return $html; } @@ -255,45 +137,59 @@ class MailTemplateService implements MailTemplateServiceInterface { /** * {@inheritdoc} */ - public function render_arrays_to_pdf($render_arrays, $title='') { - /* + public function pdfForRenderArrays($render_arrays, array $pdfOptions = []) { + $options = $pdfOptions + [ + 'encoding' => 'UTF-8', + 'margin-top' => 0, + 'margin-right' => 0, + 'margin-bottom' => 0, + 'margin-left' => 0, + + // Default page options. + // 'no-outline', + // 'disable-smart-shrinking', + ]; + + $pdf = new Pdf($options); + + foreach ($render_arrays as $render_array) { + $html = $this->renderRenderArray($render_array); + $pdf->addPage($html); + } + + $output = file_unmanaged_save_data($pdf->toString()); + return $output; + } + + /** + * {@inheritdoc} + */ + public function pdfForTemplate($template_id, &$context) { + $pdf_options = []; + if ($header = self::render_template_to_file("{$template_id}_header", $context)) { $pdf_options['header-html'] = $header->getFileName(); - } elseif ($header = self::render_template_to_file("report_header", $context)) { + } + elseif ($header = self::render_template_to_file("report_header", $context)) { $pdf_options['header-html'] = $header->getFileName(); } if ($footer = self::render_template_to_file("{$template_id}_footer", $context)) { $pdf_options['footer-html'] = $footer->getFileName(); - } elseif ($footer = self::render_template_to_file("report_footer", $context)) { + } + elseif ($footer = self::render_template_to_file("report_footer", $context)) { $pdf_options['footer-html'] = $footer->getFileName(); } - */ - - $pdf = new Pdf(array( - 'encoding' => 'UTF-8', - 'no-outline', - 'margin-top' => 0, - 'margin-right' => 0, - 'margin-bottom' => 0, - 'margin-left' => 0, - // Default page options - 'disable-smart-shrinking', - )); + $render = self::render_array_for_template($template_id, $context); + return self::pdfForRenderArrays([$render], $pdf_options); + } - foreach ($render_arrays as $render_array) { - $html = $this->render_render_array($render_array); - $pdf->addPage($html); - } - $output = file_unmanaged_save_data($pdf->toString()); - return $output; - } /** * {@inheritdoc} */ - public function pdf_download_for_file($file, $filename='download.pdf') { + public function downloadForFile($file, $filename = 'download.pdf') { $response = new BinaryFileResponse(drupal_realpath($file)); $response->SetContentDisposition( ResponseHeaderBag::DISPOSITION_ATTACHMENT, @@ -319,8 +215,8 @@ class MailTemplateService implements MailTemplateServiceInterface { // Attachments. foreach (Template::loadMultiple() as $t) { if ($t->get('mail_key') == $key) { - $file = $this->render_arrays_to_pdf([ - $this->render_array_for_template($t->id(), $context), + $file = $this->pdfForRenderArrays([ + $this->renderArrayForTemplate($t->id(), $context), ]); $attachment = new \StdClass(); diff --git a/src/Plugin/views/area/Pdf.php b/src/Plugin/views/area/Pdf.php index 9536988f264fb6d13a8a8fd520bf3a5b924f34c6..54c4a3a9b4bc7125cc83abbcb5568a0834e44a71 100644 --- a/src/Plugin/views/area/Pdf.php +++ b/src/Plugin/views/area/Pdf.php @@ -3,6 +3,7 @@ namespace Drupal\mail_template\Plugin\views\area; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Yaml\Yaml; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; @@ -43,6 +44,7 @@ class Pdf extends AreaPluginBase { $options = parent::defineOptions(); $options['view_id'] = ['default' => FALSE]; $options['display_id'] = ['default' => FALSE]; + $options['pdf_options'] = ['default' => FALSE]; return $options; } @@ -62,13 +64,12 @@ class Pdf extends AreaPluginBase { '#title' => $this->t('Display ID for PDF.'), '#default_value' => isset($this->options['display_id']) ? $this->options['display_id'] : '', ]; - } - - /** - * {@inheritdoc} - */ - public function preQuery() { - + $form['pdf_options'] = [ + '#type' => 'textarea', + '#title' => $this->t('PDF Options.'), + '#description' => $this->t("Options for wkhtmltopdf. Eg: orientation: Landscape"), + '#default_value' => isset($this->options['pdf_options']) ? $this->options['pdf_options'] : '', + ]; } /** @@ -76,6 +77,11 @@ class Pdf extends AreaPluginBase { */ public function render($empty = FALSE) { $view_id = $this->options['view_id'] ? $this->options['view_id'] : $this->view->id(); + $display_id = $this->options['display_id'] ? $this->options['display_id'] : 'default'; + $pdf_options = []; + if ($this->options['pdf_options']) { + $pdf_options = Yaml::parse($this->options['pdf_options']); + } return [ '#title' => $this->t('Download'), @@ -84,10 +90,10 @@ class Pdf extends AreaPluginBase { 'mail_template.view_report', [ 'view' => $view_id, - 'display' => 'default', + 'display' => $display_id, ], [ - 'query' => $this->view->getExposedInput(), + 'query' => $this->view->getExposedInput() + ['pdfOptions' => $pdf_options], ]), ]; }