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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_print\Plugin\PrintEngineBase implements ContainerFactoryPluginInterface, PrintEngineInterface
- class \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PdfEngineBase
- class \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PhpWkhtmlToPdf implements AlignableHeaderFooterInterface
- class \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PdfEngineBase
- class \Drupal\entity_print\Plugin\PrintEngineBase implements ContainerFactoryPluginInterface, PrintEngineInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of PhpWkhtmlToPdf
File
- src/
Plugin/ EntityPrint/ PrintEngine/ PhpWkhtmlToPdf.php, line 28
Namespace
Drupal\entity_print\Plugin\EntityPrint\PrintEngineView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AlignableHeaderFooterInterface:: |
constant | Align the text in the center. | ||
AlignableHeaderFooterInterface:: |
constant | Align the text to the left. | ||
AlignableHeaderFooterInterface:: |
constant | Align the text to the right. | ||
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PhpWkhtmlToPdf:: |
protected | property | The library instance. | |
PhpWkhtmlToPdf:: |
public static | property | Popular viewport sizes. | |
PhpWkhtmlToPdf:: |
public | function |
Add a string of HTML to a new page. Overrides PrintEngineInterface:: |
|
PhpWkhtmlToPdf:: |
public | function |
Form constructor. Overrides PdfEngineBase:: |
|
PhpWkhtmlToPdf:: |
public | function |
Gets default configuration for this plugin. Overrides PdfEngineBase:: |
|
PhpWkhtmlToPdf:: |
public static | function |
Checks if the Print engine dependencies are available. Overrides PrintEngineInterface:: |
|
PhpWkhtmlToPdf:: |
public | function |
Gets the binary data for the printed document. Overrides PrintEngineInterface:: |
|
PhpWkhtmlToPdf:: |
public static | function |
Gets the installation instructions for this Print engine. Overrides PrintEngineBase:: |
|
PhpWkhtmlToPdf:: |
protected | function |
Gets the paper sizes supported. Overrides PdfEngineBase:: |
|
PhpWkhtmlToPdf:: |
public | function |
Gets the object for this Print engine. Overrides PrintEngineInterface:: |
|
PhpWkhtmlToPdf:: |
public | function |
Send the Print contents to the browser. Overrides PrintEngineInterface:: |
|
PhpWkhtmlToPdf:: |
public | function |
Sets the footer text. Overrides AlignableHeaderFooterInterface:: |
|
PhpWkhtmlToPdf:: |
public | function |
Sets the header text. Overrides AlignableHeaderFooterInterface:: |
|
PhpWkhtmlToPdf:: |
public | function |
Form validation handler. Overrides PrintEngineBase:: |
|
PhpWkhtmlToPdf:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PrintEngineBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PrintEngineBase:: |
protected | property | The export type plugin. | |
PrintEngineBase:: |
public | function | ||
PrintEngineBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
PrintEngineBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PrintEngineBase:: |
public | function |
Gets the export type. Overrides PrintEngineInterface:: |
|
PrintEngineBase:: |
constant | |||
PrintEngineBase:: |
constant | |||
PrintEngineBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
PrintEngineBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
2 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |