You are here

protected function TcpPdfExampleController::generateSimplePdf in TCPDF 8

Generates a pdf file using TCPDF module.

Return value

string Binary string of the generated pdf.

1 call to TcpPdfExampleController::generateSimplePdf()
TcpPdfExampleController::downloadPdf in tcpdf_example/src/Controller/TcpPdfExampleController.php

File

tcpdf_example/src/Controller/TcpPdfExampleController.php, line 51
Contains \Drupal\tcpdf_example\Controller\TcpPdfExampleController

Class

TcpPdfExampleController

Namespace

Drupal\tcpdf_example\Controller

Code

protected function generateSimplePdf() {

  // Get the content we want to convert into pdf.
  $html_template = [
    '#theme' => 'tcpdf_example_basic_html',
  ];
  $html = \Drupal::service('renderer')
    ->render($html_template);

  // Never make an instance of TCPDF or TCPDFDrupal classes manually.
  // Use tcpdf_get_instance() instead.
  $tcpdf = tcpdf_get_instance();

  /* DrupalInitialize() is an extra method added to TCPDFDrupal that initializes
   *  some TCPDF variables (like font types), and makes possible to change the
   *  default header or footer without creating a new class.
   */
  $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',
        // You can pass extra data to your callback.
        'context' => array(
          'welcome_message' => 'Hello, tcpdf example!',
        ),
      ),
    ),
  ));

  // Insert the content. Note that DrupalInitialize automatically adds the first
  // page to the pdf document.
  $tcpdf
    ->writeHTML($html);
  return $tcpdf
    ->Output('', 'S');
}