View source
<?php
namespace Drupal\entity_print\Plugin\EntityPrint\PrintEngine;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_print\Plugin\ExportTypeInterface;
use Drupal\entity_print\Plugin\PrintEngineBase;
class TcPdfv1 extends PrintEngineBase {
protected $tcpdf;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ExportTypeInterface $export_type) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $export_type);
$this->tcpdf = new \TCPDF();
}
public static function getInstallationInstructions() {
return t('Please install with: @command', [
'@command' => 'composer require "tecnickcom/tcpdf ~6"',
]);
}
public function defaultConfiguration() {
return [
'page_format' => 'A4',
'orientation' => static::PORTRAIT,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$page_formats = array_combine(array_keys(\TCPDF_STATIC::$page_formats), array_keys(\TCPDF_STATIC::$page_formats));
$form['page_format'] = [
'#title' => $this
->t('Paper Size'),
'#type' => 'select',
'#options' => $page_formats,
'#default_value' => $this->configuration['page_format'],
'#description' => $this
->t('The page size to print the PDF to.'),
];
$form['orientation'] = [
'#title' => $this
->t('Paper Orientation'),
'#type' => 'select',
'#options' => [
static::PORTRAIT => $this
->t('Portrait'),
static::LANDSCAPE => $this
->t('Landscape'),
],
'#default_value' => $this->configuration['orientation'],
'#description' => $this
->t('The paper orientation one of Landscape or Portrait'),
];
return $form;
}
public function addPage($content) {
$this->tcpdf
->AddPage($this->configuration['orientation'], $this->configuration['page_format']);
$this->tcpdf
->writeHTML($content);
}
public function send($filename, $force_download = TRUE) {
$this->tcpdf
->Output($filename, $force_download ? 'D' : 'I');
}
public function getBlob() {
return $this->tcpdf
->Output('', 'S');
}
public static function dependenciesAvailable() {
return class_exists('\\TCPDF') && !drupal_valid_test_ua();
}
public function getPrintObject() {
return $this->tcpdf;
}
}