TcpPdfExampleController.php in TCPDF 8
File
tcpdf_example/src/Controller/TcpPdfExampleController.php
View source
<?php
namespace Drupal\tcpdf_example\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
class TcpPdfExampleController extends ControllerBase {
public function downloadPdf($example_name) {
switch ($example_name) {
case 'simple':
$pdf = $this
->generateSimplePdf();
break;
default:
return $this
->t('No such example.');
}
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($pdf));
header('Content-Disposition: attachment; filename="mydocument.pdf"');
print $pdf;
return [];
}
public function exampleContents() {
$page = [];
$page['example_pdf_link'] = [
'#title' => $this
->t('Basic pdf'),
'#type' => 'link',
'#url' => Url::fromRoute('tcpdf_example.download_pdf', array(
'example_name' => 'simple',
)),
];
return $page;
}
protected function generateSimplePdf() {
$html_template = [
'#theme' => 'tcpdf_example_basic_html',
];
$html = \Drupal::service('renderer')
->render($html_template);
$tcpdf = tcpdf_get_instance();
$tcpdf
->DrupalInitialize(array(
'footer' => array(
'html' => 'This is a test!! <em>Bottom of the page</em>',
),
'header' => array(
'callback' => array(
'function' => 'tcpdf_example_default_header',
'context' => array(
'welcome_message' => 'Hello, tcpdf example!',
),
),
),
));
$tcpdf
->writeHTML($html);
return $tcpdf
->Output('', 'S');
}
}