View source
<?php
namespace Drupal\pdf_api\Plugin\PdfGenerator;
use Drupal\pdf_api\Plugin\PdfGeneratorBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\pdf_api\Annotation\PdfGenerator;
use Drupal\Core\Annotation\Translation;
use Dompdf\Dompdf;
use Symfony\Component\DependencyInjection\ContainerInterface;
define('DOMPDF_ENABLE_AUTOLOAD', FALSE);
class DompdfGenerator extends PdfGeneratorBase implements ContainerFactoryPluginInterface {
protected $generator;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, DOMPDF $generator) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->generator = $generator;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('dompdf'));
}
public function setter($pdf_content, $pdf_location, $save_pdf, $paper_orientation, $paper_size, $footer_content, $header_content, $path_to_binary = '') {
$this
->setPageOrientation($paper_orientation);
$this
->addPage($pdf_content);
$this
->setHeader($header_content);
}
public function getObject() {
return $this->generator;
}
public function setHeader($text) {
$canvas = $this->generator
->get_canvas();
$canvas
->page_text(72, 18, "Header: {PAGE_COUNT}", "", 11, array(
0,
0,
0,
));
}
public function addPage($html) {
$this->generator
->load_html($html);
$this->generator
->render();
}
public function setPageOrientation($orientation = PdfGeneratorInterface::PORTRAIT) {
$this->generator
->set_paper("", $orientation);
}
public function setPageSize($page_size) {
if ($this
->isValidPageSize($page_size)) {
$this->generator
->set_paper($page_size);
}
}
public function setFooter($text) {
}
public function save($location) {
$content = $this->generator
->output([]);
file_put_contents($location, $content);
}
public function send() {
$this->generator
->stream("pdf", array(
'Attachment' => 0,
));
}
public function stream($filelocation) {
$this->generator
->Output($filelocation, "F");
}
}