You are here

class PhpWkhtmlToPdf in Entity Print 8.2

PHP wkhtmltopdf plugin.

@PrintEngine( id = "phpwkhtmltopdf", label = @Translation("Php Wkhtmltopdf"), export_type = "pdf" )

To use this implementation you will need the Php Wkhtmltopdf library, simply run:


    composer require "mikehaertl/phpwkhtmltopdf ~2.1"

Hierarchy

Expanded class hierarchy of PhpWkhtmlToPdf

File

src/Plugin/EntityPrint/PrintEngine/PhpWkhtmlToPdf.php, line 28

Namespace

Drupal\entity_print\Plugin\EntityPrint\PrintEngine
View source
class PhpWkhtmlToPdf extends PdfEngineBase implements AlignableHeaderFooterInterface {

  /**
   * The library instance.
   *
   * @var \mikehaertl\wkhtmlto\Pdf
   */
  protected $pdf;

  /**
   * Popular viewport sizes.
   *
   * @var array
   * @constant
   */
  public static $viewportSizeOptions = [
    '_none' => 'Default',
    '1920x1080' => '1920x1080',
    '1366x768' => '1366x768',
    '1280x1024' => '1280x1024',
    '1280x800' => '1280x800',
    '1024x768' => '1024x768',
    '768x1024' => '768x1024',
    '720x1280' => '720x1280',
    '375x667' => '375x667',
    '360x640' => '360x640',
  ];

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ExportTypeInterface $export_type) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $export_type);
    $this->pdf = new Pdf([
      'binary' => $this->configuration['binary_location'],
      'orientation' => $this->configuration['orientation'],
      'username' => $this->configuration['username'],
      'password' => $this->configuration['password'],
      'page-size' => $this->configuration['default_paper_size'],
      'zoom' => $this->configuration['zoom'],
      'viewport-size' => $this->configuration['viewport_size'],
    ]);
    if ($this->configuration['remove_pdf_margins']) {
      $this->pdf
        ->setOptions([
        'margin-top' => 0,
        'margin-bottom' => 0,
        'margin-left' => 0,
        'margin-right' => 0,
      ]);
    }

    // Table of contents handling.
    if ($this->configuration['toc_generate']) {
      if ($this->configuration['toc_enable_back_links']) {

        // This option is actually a page option.
        $this
          ->getPrintObject()
          ->setOptions([
          'enable-toc-back-links',
        ]);
      }
      $options = [];
      if ($this->configuration['toc_disable_dotted_lines']) {
        $options[] = 'disable-dotted-lines';
      }
      if ($this->configuration['toc_disable_links']) {
        $options[] = 'disable-toc-links';
      }
      $this
        ->getPrintObject()
        ->addToc($options);
    }

    // Proxy configuration.
    $config = Settings::get('http_client_config');
    if (!empty($config['proxy']['https'])) {
      $this->pdf
        ->setOptions([
        'proxy' => $config['proxy']['https'],
      ]);
    }
    elseif (!empty($config['proxy']['http'])) {
      $this->pdf
        ->setOptions([
        'proxy' => $config['proxy']['http'],
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getInstallationInstructions() {
    return t('Please install with: @command', [
      '@command' => 'composer require "mikehaertl/phpwkhtmltopdf ~2.1"',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'binary_location' => '/usr/local/bin/wkhtmltopdf',
      'zoom' => 1,
      'toc_generate' => FALSE,
      'toc_enable_back_links' => FALSE,
      'toc_disable_dotted_lines' => FALSE,
      'toc_disable_links' => FALSE,
      'viewport_size' => '_none',
      'remove_pdf_margins' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['zoom'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Zoom'),
      '#description' => $this
        ->t('Set this to zoom the pages - needed to produce hairlines.'),
      '#default_value' => $this->configuration['zoom'],
      '#weight' => -8,
      '#step' => 0.01,
    ];
    $form['binary_location'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Binary Location'),
      '#description' => $this
        ->t('Set this to the system path where the PDF engine binary is located.'),
      '#default_value' => $this->configuration['binary_location'],
      '#weight' => -7,
    ];
    $form['toc'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Table of contents'),
      '#tree' => TRUE,
      '#open' => $this->configuration['toc_generate'],
    ];
    $form['toc']['toc_generate'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Generate table of contents?'),
      '#default_value' => $this->configuration['toc_generate'],
    ];
    $form['toc']['toc_enable_back_links'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Link from section header to table of contents?'),
      '#default_value' => $this->configuration['toc_enable_back_links'],
    ];
    $form['toc']['toc_disable_dotted_lines'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Do not use dotted lines in the table of contents?'),
      '#default_value' => $this->configuration['toc_disable_dotted_lines'],
    ];
    $form['toc']['toc_disable_links'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Do not link from table of contents to sections'),
      '#default_value' => $this->configuration['toc_disable_links'],
    ];
    $form['viewport_size'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Viewport Size'),
      '#options' => self::$viewportSizeOptions,
      '#description' => $this
        ->t('Set viewport size if you have custom scrollbars or css attribute overflow to emulate window size.'),
      '#default_value' => $this->configuration['viewport_size'],
      '#weight' => -6,
    ];
    $form['remove_pdf_margins'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Remove PDF margins'),
      '#description' => $this
        ->t('Remove the page margins on the PDF'),
      '#default_value' => $this->configuration['remove_pdf_margins'],
      '#weight' => -5,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::validateConfigurationForm($form, $form_state);
    $values = OptGroup::flattenOptions($form_state
      ->getValues());
    $binary_location = isset($values['binary_location']) ? $values['binary_location'] : NULL;
    if ($binary_location && !file_exists($binary_location)) {
      $form_state
        ->setErrorByName('binary_location', sprintf('The wkhtmltopdf binary does not exist at %s', $binary_location));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function send($filename, $force_download = TRUE) {

    // If the filename received here is NULL, force open in the browser
    // otherwise attempt to have it downloaded.
    if (!$this->pdf
      ->send($filename, !$force_download)) {
      throw new PrintEngineException(sprintf('Failed to generate PDF: %s', $this->pdf
        ->getError()));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getBlob() {
    return $this->pdf
      ->toString();
  }

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

  /**
   * {@inheritdoc}
   */
  public static function dependenciesAvailable() {
    return class_exists('mikehaertl\\wkhtmlto\\Pdf') && !drupal_valid_test_ua();
  }

  /**
   * {@inheritdoc}
   */
  protected function getPaperSizes() {
    return [
      'a0' => 'A0',
      'a1' => 'A1',
      'a2' => 'A2',
      'a3' => 'A3',
      'a4' => 'A4',
      'a5' => 'A5',
      'a6' => 'A6',
      'a7' => 'A7',
      'a8' => 'A8',
      'a9' => 'A9',
      'b0' => 'B0',
      'b1' => 'B1',
      'b10' => 'B10',
      'b2' => 'B2',
      'b3' => 'B3',
      'b4' => 'B4',
      'b5' => 'B5',
      'b6' => 'B6',
      'b7' => 'B7',
      'b8' => 'B8',
      'b9' => 'B9',
      'ce5' => 'CE5',
      'comm10e' => 'Comm10E',
      'dle' => 'DLE',
      'executive' => 'Executive',
      'folio' => 'Folio',
      'ledger' => 'Ledger',
      'legal' => 'Legal',
      'letter' => 'Letter',
      'tabloid' => 'Tabloid',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function setHeaderText($text, $alignment) {
    $this->pdf
      ->setOptions([
      'header-' . $alignment => $text,
    ]);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setFooterText($text, $alignment) {
    $this->pdf
      ->setOptions([
      'footer-' . $alignment => $text,
    ]);
    return $this;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
AlignableHeaderFooterInterface::ALIGN_CENTER constant Align the text in the center.
AlignableHeaderFooterInterface::ALIGN_LEFT constant Align the text to the left.
AlignableHeaderFooterInterface::ALIGN_RIGHT constant Align the text to the right.
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.
PhpWkhtmlToPdf::$pdf protected property The library instance.
PhpWkhtmlToPdf::$viewportSizeOptions public static property Popular viewport sizes.
PhpWkhtmlToPdf::addPage public function Add a string of HTML to a new page. Overrides PrintEngineInterface::addPage
PhpWkhtmlToPdf::buildConfigurationForm public function Form constructor. Overrides PdfEngineBase::buildConfigurationForm
PhpWkhtmlToPdf::defaultConfiguration public function Gets default configuration for this plugin. Overrides PdfEngineBase::defaultConfiguration
PhpWkhtmlToPdf::dependenciesAvailable public static function Checks if the Print engine dependencies are available. Overrides PrintEngineInterface::dependenciesAvailable
PhpWkhtmlToPdf::getBlob public function Gets the binary data for the printed document. Overrides PrintEngineInterface::getBlob
PhpWkhtmlToPdf::getInstallationInstructions public static function Gets the installation instructions for this Print engine. Overrides PrintEngineBase::getInstallationInstructions
PhpWkhtmlToPdf::getPaperSizes protected function Gets the paper sizes supported. Overrides PdfEngineBase::getPaperSizes
PhpWkhtmlToPdf::getPrintObject public function Gets the object for this Print engine. Overrides PrintEngineInterface::getPrintObject
PhpWkhtmlToPdf::send public function Send the Print contents to the browser. Overrides PrintEngineInterface::send
PhpWkhtmlToPdf::setFooterText public function Sets the footer text. Overrides AlignableHeaderFooterInterface::setFooterText
PhpWkhtmlToPdf::setHeaderText public function Sets the header text. Overrides AlignableHeaderFooterInterface::setHeaderText
PhpWkhtmlToPdf::validateConfigurationForm public function Form validation handler. Overrides PrintEngineBase::validateConfigurationForm
PhpWkhtmlToPdf::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PrintEngineBase::__construct
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.
PrintEngineBase::$exportType protected property The export type plugin.
PrintEngineBase::calculateDependencies public function
PrintEngineBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
PrintEngineBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PrintEngineBase::getExportType public function Gets the export type. Overrides PrintEngineInterface::getExportType
PrintEngineBase::LANDSCAPE constant
PrintEngineBase::PORTRAIT constant
PrintEngineBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
PrintEngineBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 2
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.