You are here

class MpdfGenerator 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

A PDF generator plugin for the mPDF library.

Plugin annotation


@PdfGenerator(
  id = "mpdf",
  module = "pdf_api",
  title = @Translation("mPDF"),
  description = @Translation("PDF generator using the mPDF generator.")
)

Hierarchy

Expanded class hierarchy of MpdfGenerator

File

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

Namespace

Drupal\pdf_api\Plugin\PdfGenerator
View source
class MpdfGenerator extends PdfGeneratorBase implements ContainerFactoryPluginInterface {

  /**
   * Instance of the mPdf class library.
   *
   * @var \mPdf
   */
  protected $generator;

  /**
   * The saved header content.
   *
   * @var string
   */
  protected $headerContent;

  /**
   * The saved PDF content.
   *
   * @var string
   */
  protected $pdfContent;

  /**
   * The saved footer content.
   *
   * @var string
   */
  protected $footerContent;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function setter($pdf_content, $pdf_location, $save_pdf, $paper_orientation, $paper_size, $footer_content, $header_content, $path_to_binary = '') {
    $this
      ->setPageSize($paper_size);
    $this
      ->setPageOrientation($paper_orientation);

    // Save until the generator is constructed in the preGenerate method.
    $this->headerContent = $header_content;
    $this->pdfContent = $pdf_content;
    $this->footerContent = $footer_content;
  }

  /**
   * {@inheritdoc}
   */
  public function getObject() {
    return $this->generator;
  }

  /**
   * {@inheritdoc}
   */
  public function setHeader($text) {
    $this->generator
      ->SetHeader($text);
  }

  /**
   * {@inheritdoc}
   */
  public function addPage($html) {
    $this->generator
      ->addPage($html);
  }

  /**
   * {@inheritdoc}
   */
  public function setPageOrientation($orientation = PdfGeneratorInterface::PORTRAIT) {
    if ($orientation == PdfGeneratorInterface::PORTRAIT) {
      $orientation = 'P';
    }
    else {
      $orientation = 'L';
    }
    $this
      ->setOptions(array(
      'orientation' => $orientation,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function setPageSize($page_size) {
    if ($this
      ->isValidPageSize($page_size)) {
      $this
        ->setOptions(array(
        'sheet-size' => $page_size,
      ));
    }
  }

  /**
   * Sets the password in PDF.
   *
   * @param string $password
   *   The password which will be used in PDF.
   */
  public function setPassword($password) {
    if (isset($password) && $password != NULL) {

      // Print and Copy is allowed.
      $this->generator
        ->SetProtection(array(
        'print',
        'copy',
      ), $password, $password);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setFooter($text) {

    // $this->generator->SetFooter($text);
  }

  /**
   * {@inheritdoc}
   */
  public function save($location) {
    $this
      ->preGenerate();
    $this->generator
      ->Output($location, 'F');
  }

  /**
   * {@inheritdoc}
   */
  public function send() {
    $this->generator
      ->Output("", "I");
  }

  /**
   * {@inheritdoc}
   */
  public function stream($filelocation) {
    $this->generator
      ->Output($filelocation, 'F');
  }

  /**
   * Set the global options from the plugin into the mPDF generator class.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
MpdfGenerator::$footerContent protected property The saved footer content.
MpdfGenerator::$generator protected property Instance of the mPdf class library.
MpdfGenerator::$headerContent protected property The saved header content.
MpdfGenerator::$pdfContent protected property The saved PDF content.
MpdfGenerator::addPage public function Add a page to the generated PDF. Overrides PdfGeneratorInterface::addPage
MpdfGenerator::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MpdfGenerator::getObject public function Returns instances of PDF libraries. Overrides PdfGeneratorInterface::getObject
MpdfGenerator::preGenerate protected function Set the global options from the plugin into the mPDF generator class.
MpdfGenerator::save public function Generate and save the PDF at a specific location. Overrides PdfGeneratorInterface::save
MpdfGenerator::send public function The name of the file to be downloaded. Overrides PdfGeneratorInterface::send
MpdfGenerator::setFooter public function Sets the footer in the PDF. Overrides PdfGeneratorInterface::setFooter
MpdfGenerator::setHeader public function Sets the header in the PDF. Overrides PdfGeneratorInterface::setHeader
MpdfGenerator::setPageOrientation public function Set the paper orientation of the generated PDF pages. Overrides PdfGeneratorInterface::setPageOrientation
MpdfGenerator::setPageSize public function Set the page size of the generated PDF pages. Overrides PdfGeneratorInterface::setPageSize
MpdfGenerator::setPassword public function Sets the password in PDF.
MpdfGenerator::setter public function Set the various options for PDF. Overrides PdfGeneratorInterface::setter
MpdfGenerator::stream public function Stream the PDF to the browser. Overrides PdfGeneratorInterface::stream
MpdfGenerator::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
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::LANDSCAPE constant Landscape paper orientation.
PdfGeneratorInterface::PORTRAIT constant Portrait paper orientation.
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 3
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.