class PdfFormat in Printer and PDF versions for Drupal 8+ 8
Same name and namespace in other branches
- 2.x modules/printable_pdf/src/Plugin/PrintableFormat/PdfFormat.php \Drupal\printable_pdf\Plugin\PrintableFormat\PdfFormat
Provides a plugin to display a PDF version of a page.
Plugin annotation
@PrintableFormat(
id = "pdf",
module = "printable_pdf",
title = @Translation("PDF"),
description = @Translation("PDF description.")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\printable\Plugin\PrintableFormatBase implements ContainerFactoryPluginInterface, PrintableFormatInterface
- class \Drupal\printable_pdf\Plugin\PrintableFormat\PdfFormat
- class \Drupal\printable\Plugin\PrintableFormatBase implements ContainerFactoryPluginInterface, PrintableFormatInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of PdfFormat
File
- modules/
printable_pdf/ src/ Plugin/ PrintableFormat/ PdfFormat.php, line 28
Namespace
Drupal\printable_pdf\Plugin\PrintableFormatView source
class PdfFormat extends PrintableFormatBase {
/**
* The PDF generator plugin manager service.
*
* @var \Drupal\pdf_api\PdfGeneratorPluginManager
*/
protected $pdfGeneratorManager;
/**
* The PDF generator plugin instance.
*
* @var \Drupal\pdf_api\Plugin\PdfGeneratorInterface
*/
protected $pdfGenerator;
/**
* The filename to use.
*
* @var string
*/
protected $filename;
/**
* The path cerrent service.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $pathCurrent;
/**
* The request stack service.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* {@inheritdoc}
*
* @param array $configuration
* The configuration array.
* @param string $plugin_id
* The plugin id.
* @param array $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory service.
* @param \Drupal\pdf_api\PdfGeneratorPluginManager $pdf_generator_manager
* The PDF generator plugin manager service.
* @param \Drupal\printable\PrintableCssIncludeInterface $printable_css_include
* The printable CSS include interface.
* @param \Drupal\printable\LinkExtractor\LinkExtractorInterface $link_extractor
* The Link extractor service.
* @param \Drupal\Core\Path\CurrentPathStack $pathCurrent
* Represents the current path for the current request.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* Request stack that controls the lifecycle of requests.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigFactory $config_factory, PdfGeneratorPluginManager $pdf_generator_manager, PrintableCssIncludeInterface $printable_css_include, LinkExtractorInterface $link_extractor, CurrentPathStack $pathCurrent, RequestStack $requestStack) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $config_factory, $printable_css_include, $link_extractor);
$this->pdfGeneratorManager = $pdf_generator_manager;
$this->pathCurrent = $pathCurrent;
$this->requestStack = $requestStack;
$pdf_library = (string) $this->configFactory
->get('printable.settings')
->get('pdf_tool');
if (!$pdf_library) {
return;
}
$pdf_library = strtolower($pdf_library);
$this->pdfGenerator = $this->pdfGeneratorManager
->createInstance($pdf_library);
if ($pdf_library != 'wkhtmltopdf') {
return;
}
$options = [];
$use_xvfb_run = (string) $this->configFactory
->get('printable.settings')
->get('print_pdf_use_xvfb_run');
$path_to_xfb_run = (string) $this->configFactory
->get('printable.settings')
->get('path_to_xfb_run');
$ignore_warnings = (bool) $this->configFactory
->get('printable.settings')
->get('ignore_warnings');
if ($use_xvfb_run) {
$options = [
'use-xserver' => NULL,
'commandOptions' => [
'enableXvfb' => TRUE,
'xvfbRunBinary' => $path_to_xfb_run,
],
'ignoreWarnings' => $ignore_warnings,
];
}
$this->pdfGenerator
->getObject()
->setOptions($options);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('plugin.manager.pdf_generator'), $container
->get('printable.css_include'), $container
->get('printable.link_extractor'), $container
->get('path.current'), $container
->get('request_stack'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'pdf_generator' => 'wkhtmltopdf',
];
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this
->getConfiguration();
$options = [];
foreach ($this->pdfGeneratorManager
->getDefinitions() as $definition) {
$options[$definition['id']] = $definition['title'];
}
$form['pdf_generator'] = [
'#type' => 'radios',
'#title' => 'PDF Generator',
'#default_value' => $config['pdf_generator'],
'#options' => $options,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->setConfiguration([
'pdf_generator' => $form_state['values']['pdf_generator'],
]);
}
/**
* Get the header content.
*
* @return string
* Content of header.
*/
public function getHeaderContent() {
$pdf_header = [
'#theme' => 'printable_pdf_header',
];
return render($pdf_header);
}
/**
* Get the footer content.
*
* @return string
* Content of footer.
*/
public function getFooterContent() {
$pdf_footer = [
'#theme' => 'printable_pdf_footer',
];
return render($pdf_footer);
}
/**
* Get the HTML content for PDF generation.
*
* @return string
* HTML content for PDF.
*/
public function buildPdfContent() {
$content = parent::buildContent();
$rendered_page = parent::extractLinks(render($content));
return $rendered_page;
}
/**
* Set formatted header and footer.
*/
public function formattedHeaderFooter() {
// And this can be used by users who do not want default one, this example
// is for wkhtmltopdf generator.
$this->pdfGenerator
->getObject()
->SetFooter('This is a footer on left side||' . 'This is a footer on right side');
}
/**
* Return default headers (may be overridden by the generator).
*
* @param string $filename
* The filename to suggest to the browser.
* @param bool $download
* Whether to download the PDF or display it in the browser.
*
* @return array
* Default headers for the response object.
*/
private function getHeaders($filename, $download) {
$disposition = $download ? 'attachment' : 'inline';
return [
'Content-Type' => Unicode::mimeHeaderEncode('application/pdf'),
'Content-Disposition' => $disposition . '; filename="' . $filename . '"',
'Content-Length' => filesize($filename),
'Content-Transfer-Encoding' => 'binary',
'Pragma' => 'no-cache',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Expires' => '0',
'Accept-Ranges' => 'bytes',
];
}
/**
* File send callback for the Streamed response.
*/
public function streamResponseContent() {
$this->pdfGenerator
->send();
}
/**
* Remove tokens from images so we just get the path.
*/
public function removeImageTokens($subject) {
// We only need to do this for absolute paths, not external images, so
// search for mentions of DRUPAL_ROOT.
$next_pos = strpos($subject, DRUPAL_ROOT);
while ($next_pos !== FALSE) {
$have_matches = preg_match('/[\\s\'"]/', substr($subject, $next_pos), $matches, PREG_OFFSET_CAPTURE);
$path_end = $have_matches ? $matches[0][1] : strlen($subject) - $next_pos + 1;
$query_start = strpos(substr($subject, $next_pos, $path_end), '?');
if ($query_start !== false) {
$subject = substr($subject, 0, $next_pos + $query_start) . substr($subject, $next_pos + $path_end);
}
$next_pos = strpos($subject, DRUPAL_ROOT, $next_pos + $path_end - $query_start + 1);
}
return $subject;
}
/**
* {@inheritdoc}
*/
public function getResponse() {
$paper_size = (string) $this->configFactory
->get('printable.settings')
->get('paper_size');
$paper_orientation = $this->configFactory
->get('printable.settings')
->get('page_orientation');
$path_to_binary = $this->configFactory
->get('printable.settings')
->get('path_to_binary');
$save_pdf = $this->configFactory
->get('printable.settings')
->get('save_pdf');
$pdf_location = $this->configFactory
->get('printable.settings')
->get('pdf_location');
$raw_content = $this
->buildPdfContent();
$pdf_content = $this
->removeImageTokens($raw_content);
$footer_content = $this
->getFooterContent();
$header_content = $this
->getHeaderContent();
// $this->formattedHeaderFooter();
$this->pdfGenerator
->setter($pdf_content, $pdf_location, $save_pdf, $paper_orientation, $paper_size, $footer_content, $header_content, $path_to_binary);
if (empty($pdf_location)) {
$pdf_location = str_replace("/", "_", $this->pathCurrent
->getPath()) . '.pdf';
$pdf_location = substr($pdf_location, 1);
}
$this->filename = DRUPAL_ROOT . '/' . $pdf_location;
$this->pdfGenerator
->save($this->filename);
if ($this->pdfGenerator
->displayErrors()) {
$source_url = $this->requestStack
->getCurrentRequest()
->getRequestUri();
$pos = strpos($source_url, "printable");
$source_url = substr($source_url, 0, $pos - 1);
return new RedirectResponse($source_url);
}
return (new BinaryFileResponse($this->filename, 200, $this
->getHeaders($pdf_location, $save_pdf)))
->deleteFileAfterSend(true);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
PdfFormat:: |
protected | property | The filename to use. | |
PdfFormat:: |
protected | property | The path cerrent service. | |
PdfFormat:: |
protected | property | The PDF generator plugin instance. | |
PdfFormat:: |
protected | property | The PDF generator plugin manager service. | |
PdfFormat:: |
protected | property | The request stack service. | |
PdfFormat:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
PdfFormat:: |
public | function | Get the HTML content for PDF generation. | |
PdfFormat:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
PdfFormat:: |
public static | function |
Creates an instance of the plugin. Overrides PrintableFormatBase:: |
|
PdfFormat:: |
public | function |
Gets default configuration for this plugin. Overrides PrintableFormatBase:: |
|
PdfFormat:: |
public | function | Set formatted header and footer. | |
PdfFormat:: |
public | function | Get the footer content. | |
PdfFormat:: |
public | function | Get the header content. | |
PdfFormat:: |
private | function | Return default headers (may be overridden by the generator). | |
PdfFormat:: |
public | function |
Returns the response object for this format plugin. Overrides PrintableFormatBase:: |
|
PdfFormat:: |
public | function | Remove tokens from images so we just get the path. | |
PdfFormat:: |
public | function | File send callback for the Streamed response. | |
PdfFormat:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
PdfFormat:: |
public | function |
Overrides PrintableFormatBase:: |
|
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. | |
PrintableFormatBase:: |
protected | property | The config factory service. | |
PrintableFormatBase:: |
protected | property | A render array of the content to be output by the printable format. | |
PrintableFormatBase:: |
protected | property | A string containing the list of links present in the page. | |
PrintableFormatBase:: |
protected | property | Printable link extractor. | |
PrintableFormatBase:: |
protected | property | Printable CSS include manager. | |
PrintableFormatBase:: |
protected | function | Build a render array of the content, wrapped in the printable theme. | |
PrintableFormatBase:: |
protected | function | Extracts the links present in HTML string. | |
PrintableFormatBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurablePluginInterface:: |
|
PrintableFormatBase:: |
public | function |
Returns the administrative description for this format plugin. Overrides PrintableFormatInterface:: |
|
PrintableFormatBase:: |
public | function |
Returns the administrative label for this format plugin. Overrides PrintableFormatInterface:: |
|
PrintableFormatBase:: |
protected | function | Get the HTML output of the whole page and pass to the response object. | |
PrintableFormatBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface:: |
|
PrintableFormatBase:: |
public | function |
Set the content for the printable response. Overrides PrintableFormatInterface:: |
|
PrintableFormatBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
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. |