Commit 7d575653 authored by Attila Santo-Rieder's avatar Attila Santo-Rieder
Browse files

Issue #3279185 by attisan: Fix printing in php CLI context (like drush)

parent 43ffefd2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
default_css: true
force_download: true
base_url: ''
print_engines:
  pdf_engine: dompdf
+3 −0
Original line number Diff line number Diff line
@@ -13,6 +13,9 @@ entity_print.settings:
      label: 'All in-use print engines.'
      sequence:
        type: string
    base_url:
      type: string
      label: 'Base URL to drupal page'

entint_print_engine_pdf:
  type: mapping
+18 −0
Original line number Diff line number Diff line
@@ -23,6 +23,17 @@ class PrintHtmlAlterEvent extends Event {
   */
  protected $entities;

  /**
   * The server application programming interface string.
   *
   * Will be some like 'fpm-fcgi' or 'cgi-fcgi' for PHP
   * running through a browser call. Will be 'cli' for calls
   * initiated through the console (e.g. drush).
   *
   * @var string
   */
  protected $phpSapi = PHP_SAPI;

  /**
   * PrintHtmlAlterEvent constructor.
   *
@@ -56,4 +67,11 @@ class PrintHtmlAlterEvent extends Event {
    return $this->entities;
  }

  /**
   * Gets the initialized PHP SAPI.
   */
  public function getPhpSapi() {
    return $this->phpSapi;
  }

}
+28 −5
Original line number Diff line number Diff line
@@ -52,21 +52,44 @@ class PostRenderSubscriber implements EventSubscriberInterface {
   * @see https://drupal.org/node/1494670
   */
  public function postRender(PrintHtmlAlterEvent $event) {
    // We only apply the fix to PHP Wkhtmltopdf because the other
    // implementations allow us to specify a base url.
    // We apply the fix to PHP Wkhtmltopdf and any engine when run in CLI.
    $config = $this->configFactory->get('entity_print.settings');
    if ($config->get('print_engines.pdf_engine') !== 'phpwkhtmltopdf') {
    if (
      $config->get('print_engines.pdf_engine') !== 'phpwkhtmltopdf' &&
      $event->getPhpSapi() !== 'cli'
    ) {
      return;
    }

    $html_string = &$event->getHtml();
    $html5 = new HTML5();
    $document = $html5->loadHTML($html_string);
    $request_base_url = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost();
    $base_url = $config->get('base_url') ?: $request_base_url;

    // Only add a base element if there is none set in the html.
    if ($document->getElementsByTagName('base')->count() === 0) {
      $base = $document->createElement('base');
      $base->setAttribute('href', $base_url);

      // Add new base element to the head element or ...
      if ($document->getElementsByTagName('head')->count() !== 0) {
        /** @var \DOMNode $head */
        foreach ($document->getElementsByTagName('head') as $head) {
          $head->appendChild($base);
        }
      }
      // (edge-case) create a head element to add the base element to.
      else {
        $head = $document->createElement('head');
        $document->appendChild($head);
        $head->appendChild($base);
      }
    }

    // Define a function that will convert root relative uris into absolute
    // urls.
    $transform = function ($tag, $attribute) use ($document) {
      $base_url = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost();
    $transform = function ($tag, $attribute) use ($document, $base_url) {
      foreach ($document->getElementsByTagName($tag) as $node) {
        $attribute_value = $node->getAttribute($attribute);

+9 −0
Original line number Diff line number Diff line
@@ -132,6 +132,14 @@ class SettingsForm extends ConfigFormBase {
      '#default_value' => $config->get('force_download'),
    ];

    $form['entity_print']['base_url'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Base URL'),
      '#description' => $this->t('Useful if you plan to print in <em>CLI</em> context like <em>drush</em> using queue workers or other background processing means. Leave blank in all other cases.'),
      '#attributes' => ['placeholder' => $this->getRequest()->getSchemeAndHttpHost()],
      '#default_value' => $config->get('base_url'),
    ];

    foreach ($this->exportTypeManager->getDefinitions() as $export_type => $definition) {
      // If we have a print_engine in the form_state then use that otherwise,
      // fall back to what was saved as this is a fresh form. Check explicitly
@@ -233,6 +241,7 @@ class SettingsForm extends ConfigFormBase {
    $config
      ->set('default_css', $values['default_css'])
      ->set('force_download', $values['force_download'])
      ->set('base_url', $values['base_url'])
      ->save();

    $this->messenger()->addStatus($this->t('Configuration saved.'));
Loading