Skip to content
Snippets Groups Projects
Commit afdd205e authored by Gaurav Kapoor's avatar Gaurav Kapoor Committed by Gaurav Kapoor
Browse files

Issue #3240579 by gaurav.kapoor: Add PDF render plugin for the module

parent e389c88b
Branches
Tags 1.0.0 2.0.0
No related merge requests found
......@@ -5,7 +5,8 @@
"homepage": "https://drupal.org/project/documentation_generator",
"license": "GPL-2.0+",
"require": {
"phpoffice/phpword": "^0.16.0"
"phpoffice/phpword": "^0.16.0",
"dompdf/dompdf": "^1.0.2"
},
"minimum-stability": "dev"
}
......@@ -15,16 +15,14 @@ function documentation_generator_file_download($uri) {
$scheme = $stream_wrapper_manager->getScheme($uri);
$target = $stream_wrapper_manager->getTarget($uri);
$fileName = 'documentation.docx';
if (($scheme == 'private')
&& ($target == $fileName)
&& ($target == 'documentation.docx' || 'documentation.pdf')
&& ($user = \Drupal::currentUser())
&& ($user->hasPermission('administer documentation generator'))
) {
return [
'Content-Description' => 'File Transfer',
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
'Content-Disposition' => 'attachment; filename="' . $target . '"',
'Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Transfer-Encoding' => 'binary',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
......@@ -32,3 +30,17 @@ function documentation_generator_file_download($uri) {
];
}
}
/**
* Implements hook_theme().
*/
function documentation_generator_theme($existing, $type, $theme, $path) {
return [
'documentation' => [
'variables' => [
'title' => NULL,
'groups' => [],
],
],
];
}
......@@ -9,3 +9,6 @@ services:
documentation_generator.php_word:
class: PhpOffice\PhpWord\PhpWord
documentation_generator.dompdf:
class: Dompdf\Dompdf
<?php
namespace Drupal\documentation_generator\Plugin\DocumentationGeneratorRender;
use Dompdf\Dompdf;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\documentation_generator\Plugin\DocumentationGeneratorRenderBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* PDF Documentation Generator render.
*
* Implements Documentation Generator Render plugin for PDF.
*
* @DocumentationGeneratorRender(
* id = "pdf",
* label = @Translation("PDF")
* )
*/
class PDF extends DocumentationGeneratorRenderBase implements ContainerFactoryPluginInterface {
/**
* The mpdf.
*
* @var \Dompdf\Dompdf
*/
protected $dompdf;
/**
* Constructs a new PDF instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Dompdf\Dompdf $dompdf
* The mpdf.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Dompdf $dompdf) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->dompdf = $dompdf;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('documentation_generator.dompdf')
);
}
/**
* {@inheritdoc}
*/
public function getExtension() {
return 'pdf';
}
/**
* {@inheritdoc}
*/
public function render($fileName, $path, $title, array $groups) {
$build = [
'#theme' => 'documentation',
'#title' => $title,
'#groups' => $groups,
];
$this->dompdf->loadHtml(render($build));
$this->dompdf->render();
$output = $this->dompdf->output();
$success = TRUE;
try {
file_save_data($output, 'private:// ' . $fileName);
}
catch (FileException $e) {
$success = FALSE;
}
return $success;
}
}
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1> {{ title }}</h1>
{% for group in groups %}
{% for element in group %}
{% if element.type == 'title' and element.level == 1 %}
<h2>{{ element.value }}</h2>
{% elseif element.type == 'title' and element.level == 2 %}
<h3>{{ element.value }}</h3>
{% elseif element.type == 'title' and element.level == 3 %}
<h4>{{ element.value }}</h4>
{% elseif element.type == 'paragraph' %}
{% if '@parameter' in element.value %}
{% for parameter in element.parameters %}
{% if parameter.type == 'link' %}
<p>{{ element.value|replace({'@parameter': parameter.src}) }}</p>
{% elseif parameter.type == 'list' %}
<p>{{ element.value|replace({'@parameter': parameter.items|join(', ')}) }}</p>
{% endif %}
{% endfor %}
{% else %}
<p>{{ element.value }}</p>
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment