You are here

abstract class PdfGeneratorBase in PDF generator API 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/PdfGeneratorBase.php \Drupal\pdf_api\Plugin\PdfGeneratorBase

Provides a base class for PDF generator plugins.

Hierarchy

Expanded class hierarchy of PdfGeneratorBase

4 files declare their use of PdfGeneratorBase
DompdfGenerator.php in src/Plugin/PdfGenerator/DompdfGenerator.php
Contains \Drupal\pdf_api\Plugin\DompdfGenerator.
MpdfGenerator.php in src/Plugin/PdfGenerator/MpdfGenerator.php
Contains \Drupal\pdf_api\Plugin\MpdfGenerator.
TcpdfGenerator.php in src/Plugin/PdfGenerator/TcpdfGenerator.php
Contains \Drupal\pdf_api\Plugin\TcpdfGenerator.
WkhtmltopdfGenerator.php in src/Plugin/PdfGenerator/WkhtmltopdfGenerator.php
Contains \Drupal\pdf_api\Plugin\WkhtmltopdfGenerator.

File

src/Plugin/PdfGeneratorBase.php, line 19
Contains \Drupal\printable\Plugin\PrintableFormatBase.

Namespace

Drupal\pdf_api\Plugin
View source
abstract class PdfGeneratorBase extends PluginBase implements PdfGeneratorInterface {

  /**
   * The global options for the PDF generator.
   *
   * @var array
   */
  protected $options = array();

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->pluginDefinition['id'];
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->pluginDefinition['title'];
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->pluginDefinition['description'];
  }

  /**
   * Get the dimensions of a given page size.
   *
   * @param string $page_size
   *   The page size to get the dimensions for (e.g. A4).
   *
   * @return array|FALSE
   *   An array with the keys "width" and "height" that contain the width and
   *   height dimensions respectively. False if the page size is unknown.
   */
  protected function getPageDimensions($page_size) {
    if ($this
      ->isValidPageSize($page_size)) {
      $page_sizes = $this
        ->pageSizes();
      return $page_sizes[$page_size];
    }
  }

  /**
   * Checks if a given page size is valid.
   *
   * @param string $page_size
   *   The page size to check.
   *
   * @return bool
   *   TRUE if the page size is valid, FALSE if not.
   */
  protected function isValidPageSize($page_size) {
    return array_key_exists($page_size, $this
      ->pageSizes());
  }

  /**
   * Get an array of all valid page sizes, keyed by the page size name.
   *
   * @return array
   *   An array of page sizes with the values an array of width and height and
   *   keys the page size name (e.g. A4).
   */
  protected function pageSizes() {
    return array(
      'A0' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A1' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A2' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A3' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A4' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A5' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A6' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A7' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A8' => array(
        'width' => 0,
        'height' => 0,
      ),
      'A9' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B0' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B1' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B10' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B2' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B3' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B4' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B5' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B6' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B7' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B8' => array(
        'width' => 0,
        'height' => 0,
      ),
      'B9' => array(
        'width' => 0,
        'height' => 0,
      ),
      'C5E' => array(
        'width' => 0,
        'height' => 0,
      ),
      'Comm10E' => array(
        'width' => 0,
        'height' => 0,
      ),
      'DLE' => array(
        'width' => 0,
        'height' => 0,
      ),
      'Executive' => array(
        'width' => 0,
        'height' => 0,
      ),
      'Folio' => array(
        'width' => 0,
        'height' => 0,
      ),
      'Ledger' => array(
        'width' => 0,
        'height' => 0,
      ),
      'Legal' => array(
        'width' => 0,
        'height' => 0,
      ),
      'Letter' => array(
        'width' => 0,
        'height' => 0,
      ),
      'Tabloid' => array(
        'width' => 0,
        'height' => 0,
      ),
    );
  }

  /**
   * Get stderr from teh command that was run.
   *
   * @return string
   *   Content of stderr output.
   */
  public function getStderr() {
    return '';
  }

  /**
   * Get stdout from the command that was run.
   *
   * @return string
   *   Content of stdout.
   */
  public function getStdout() {
    return '';
  }

  /**
   * Display error messages.
   *
   * @return boolean
   *   Whether any errors occurred and were not ignored.
   */
  public function displayErrors() {
    $error = $this
      ->getStderr();
    if ($error && !$this->generator->ignoreWarnings) {

      // Add stdOut contents - they might help.
      $output = $this
        ->getStdout();
      if ($output) {
        $output = str_replace("\n", "<br />", $output);
        $markup = new TranslatableMarkup('@error<br />Output was:<br />@output', [
          '@error' => $error,
          '@output' => new FormattableMarkup($output, []),
        ]);
      }
      else {
        $markup = $error;
      }
      $this->messenger
        ->addError($markup);
      return true;
    }
    return false;
  }

  /**
   * Set global options.
   *
   * @param array $options
   *   The array of options to merge into the currently set options.
   */
  public function setOptions(array $options) {
    $this->options += $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PdfGeneratorBase::$options protected property The global options for the PDF generator.
PdfGeneratorBase::displayErrors public function Display error messages. Overrides PdfGeneratorInterface::displayErrors
PdfGeneratorBase::getDescription public function Returns the administrative description for this generator plugin. Overrides PdfGeneratorInterface::getDescription
PdfGeneratorBase::getId public function Returns the administrative id for this generator plugin. Overrides PdfGeneratorInterface::getId
PdfGeneratorBase::getLabel public function Returns the administrative label for this generator plugin. Overrides PdfGeneratorInterface::getLabel
PdfGeneratorBase::getPageDimensions protected function Get the dimensions of a given page size.
PdfGeneratorBase::getStderr public function Get stderr from teh command that was run. Overrides PdfGeneratorInterface::getStderr 1
PdfGeneratorBase::getStdout public function Get stdout from the command that was run. Overrides PdfGeneratorInterface::getStdout 1
PdfGeneratorBase::isValidPageSize protected function Checks if a given page size is valid.
PdfGeneratorBase::pageSizes protected function Get an array of all valid page sizes, keyed by the page size name.
PdfGeneratorBase::setOptions public function Set global options.
PdfGeneratorInterface::addPage public function Add a page to the generated PDF. 4
PdfGeneratorInterface::getObject public function Returns instances of PDF libraries. 4
PdfGeneratorInterface::LANDSCAPE constant Landscape paper orientation.
PdfGeneratorInterface::PORTRAIT constant Portrait paper orientation.
PdfGeneratorInterface::save public function Generate and save the PDF at a specific location. 4
PdfGeneratorInterface::send public function The name of the file to be downloaded. 4
PdfGeneratorInterface::setFooter public function Sets the footer in the PDF. 4
PdfGeneratorInterface::setHeader public function Sets the header in the PDF. 4
PdfGeneratorInterface::setPageOrientation public function Set the paper orientation of the generated PDF pages. 4
PdfGeneratorInterface::setPageSize public function Set the page size of the generated PDF pages. 4
PdfGeneratorInterface::setter public function Set the various options for PDF. 4
PdfGeneratorInterface::stream public function Stream the PDF to the browser. 4
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.