You are here

protected function MpdfGenerator::preGenerate in PDF generator API 8

Same name and namespace in other branches
  1. 2.x 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 177
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;
  $config = [];
  $orientation = '';
  $orientation = $options['orientation'] ? $options['orientation'] : 'P';
  $config['format'] = $this
    ->isValidPageSize($options['sheet-size']) ? $options['sheet-size'] : 'A4';
  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);
}