You are here

protected function MpdfGenerator::preGenerate in PDF generator API 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/PdfGenerator/MpdfGenerator.php \Drupal\pdf_api\Plugin\PdfGenerator\MpdfGenerator::preGenerate()

Set the global options from the plugin into the mPDF generator class.

1 call to MpdfGenerator::preGenerate()
MpdfGenerator::save in src/Plugin/PdfGenerator/MpdfGenerator.php
Generate and save the PDF at a specific location.

File

src/Plugin/PdfGenerator/MpdfGenerator.php, line 187
Contains \Drupal\pdf_api\Plugin\MpdfGenerator.

Class

MpdfGenerator
A PDF generator plugin for the mPDF library.

Namespace

Drupal\pdf_api\Plugin\PdfGenerator

Code

protected function preGenerate() {

  /*
   * We have to pass the initial page size and orientation that we want to
   * the constructor, so we delay making the generator until we have those
   * details.
   *
   * mPDF is also strange in its handling of parameters. We can't just set
   * the page size and orientation separately (as you'd expect) but need to
   * combine them in the format argument for them to be effective from the
   * get-go.
   */
  $options = $this->options;
  $orientation = $options['orientation'] ? $options['orientation'] : 'P';
  $filesystem = \Drupal::service('file_system');
  $random = new Random();
  $this->tmpDir = $filesystem
    ->getTempDirectory() . '/' . $random
    ->name(16, TRUE);
  $filesystem
    ->mkdir($this->tmpDir);
  $config = [
    'format' => $this
      ->isValidPageSize($options['sheet-size']) ? $options['sheet-size'] : 'A4',
    'tempDir' => $this->tmpDir,
  ];
  if ($orientation == 'L') {
    $config['format'] .= '-' . $orientation;
  }
  $this->generator = new mPDF($config);

  // Apply any other options.
  unset($options['orientation']);
  unset($options['sheet-size']);
  $this->generator
    ->AddPageByArray($options);
  $this
    ->setHeader($this->headerContent);
  $this
    ->setFooter($this->footerContent);
  $stylesheet = '.node_view  { display: none; }';
  $this->generator
    ->WriteHTML($stylesheet, 1);
  $this->generator
    ->WriteHTML(utf8_encode($this->pdfContent), 0);
}