Commit 82b4608d authored by Maximiliano Mendez Villalobos's avatar Maximiliano Mendez Villalobos Committed by Juan Natera
Browse files

Issue #3267859 by MaxMendez: Force download parameter

parent 46e3b683
Loading
Loading
Loading
Loading
+6 −10
Changes for src/DomPdfGenerator.php: 6 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Render\Markup;
use Drupal\Core\Extension\ModuleExtensionList;

/**
 * Defines the DomPdfGenerator service to be used by another services.
@@ -55,12 +54,6 @@ class DomPdfGenerator {
   */
  protected $requestStack;

  /**
   * The module extension list.
   *
   * @var \Drupal\Core\Extension\ModuleExtensionList
   */
  protected $moduleExtensionList;

  /**
   * Constructs a new object.
@@ -82,7 +75,6 @@ class DomPdfGenerator {
    $this->renderer = $renderer;
    $this->requestStack = $request_stack;
    $this->moduleHandler = $module_handler;
    $this->moduleExtensionList = $extension_list_module;
    $this->options = new Options();
    $this->options->set('enable_css_float', TRUE);
    $this->options->set('enable_html5_parser', TRUE);
@@ -110,8 +102,10 @@ class DomPdfGenerator {
   *   Text to load additional css.
   * @param string $cssPath
   *   Path to load additional css.
   * @param bool $forceDownload
   *   If force download by Content-Disposition header is required.
   */
  public function getResponse($title, array $content, $preview = FALSE, array $options = [], $pageSize = 'A4', $showPagination = 0, $paginationX = 0, $paginationY = 0, $disposition = 'portrait', $cssText = NULL, $cssPath = NULL) {
  public function getResponse($title, array $content, $preview = FALSE, array $options = [], $pageSize = 'A4', $showPagination = 0, $paginationX = 0, $paginationY = 0, $disposition = 'portrait', $cssText = NULL, $cssPath = NULL, $forceDownload = TRUE) {
    $request = $this->requestStack->getCurrentRequest();
    $base_url = $request->getSchemeAndHttpHost();

@@ -123,7 +117,7 @@ class DomPdfGenerator {

    // Dompdf needs to be initialized with custom options if they are supplied.
    $this->dompdf = new Dompdf($this->options);
    $path = $this->moduleExtensionList->getPath('pdf_generator');
    $path = $this->moduleHandler->getModule('pdf_generator')->getPath();
    $css = file_get_contents($path . '/css/pdf.css');
    // Add inline css from text.
    if (!empty($cssText)) {
@@ -186,7 +180,9 @@ class DomPdfGenerator {
      $title = $this->renderer->render($title);
    }
    $filename = strtolower(trim(preg_replace('#\W+#', '_', $title), '_'));
    if ($forceDownload) {
      $response->headers->set('Content-Disposition', "attachment; filename={$filename}.pdf");
    }
    return $response;
  }

+28 −1
Changes for src/Plugin/views/display/Pdf.php: 28 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -146,6 +146,10 @@ class Pdf extends PathPluginBase implements ResponseDisplayPluginInterface, Cont
    $fileCss = !empty($view->display_handler->getOption('file_css')) ?
      $view->display_handler->getOption('file_css') :
      NULL;
    $forceDownload = !empty($view->display_handler->getOption('force_download')) ?
      $view->display_handler->getOption('force_download') :
      FALSE;

    $response = \Drupal::service('pdf_generator.dompdf_generator')->getResponse(
      $title,
      $output,
@@ -157,7 +161,8 @@ class Pdf extends PathPluginBase implements ResponseDisplayPluginInterface, Cont
      $paginationY,
      $disposition,
      $textCss,
      $fileCss
      $fileCss,
      $forceDownload
    );
    return $response;
  }
@@ -278,6 +283,12 @@ class Pdf extends PathPluginBase implements ResponseDisplayPluginInterface, Cont

    ];

    $options['force_download'] = [
      'category' => 'page',
      'title' => $this->t('Force Download'),
      'value' => !empty($this->getOption('force_download')) ?  $this->t('Yes') : $this->t('No'),
    ];

  }

  /**
@@ -426,6 +437,18 @@ class Pdf extends PathPluginBase implements ResponseDisplayPluginInterface, Cont

        break;

      case 'force_download':
        $form['force_download'] = [
          '#title' => $this->t('Force Download'),
          '#type' => 'checkbox',
          '#required' => FALSE,
          '#description' => $this->t('If enabled the PDF file is forced to download and do not open on browser.'),
          '#default_value' => !empty($this->getOption('force_download')) ? $this->getOption('force_download') : FALSE,
        ];

        break;


      case 'path':
        $form['path']['#description'] = $this->t('This view will be displayed by visiting this path on your site. It is recommended that the path be something like "path/%/%/feed" or "path/%/%/rss.xml", putting one % in the path for each contextual filter you have defined in the view.');
    }
@@ -478,6 +501,10 @@ class Pdf extends PathPluginBase implements ResponseDisplayPluginInterface, Cont
        $this->setOption('pagination_y', $form_state->getValue('pagination_y'));
        break;

      case 'force_download':
        $this->setOption('force_download', $form_state->getValue('force_download'));
        break;

    }
  }