You are here

function CertificateTabController::returnPdf in Course 8.3

Same name and namespace in other branches
  1. 8.2 modules/course_certificate/src/Controller/CertificateTabController.php \Drupal\course_certificate\Controller\CertificateTabController::returnPdf()

Stream a PDF to the browser

Parameters

Course $course:

AccountInterface $account:

CertificateTemplate $template:

Return value

type

1 string reference to 'CertificateTabController::returnPdf'
course_certificate.routing.yml in modules/course_certificate/course_certificate.routing.yml
modules/course_certificate/course_certificate.routing.yml

File

modules/course_certificate/src/Controller/CertificateTabController.php, line 177

Class

CertificateTabController
An example controller.

Namespace

Drupal\course_certificate\Controller

Code

function returnPdf(Course $course, AccountInterface $account, CertificateTemplate $template) {
  $pdf_gen = $template
    ->loadPrintableEngine();

  // Check for a PDF engine
  if ($pdf_gen === FALSE) {
    $current_user = \Drupal::currentUser();
    $msg = t('Current site configuration does not allow PDF file creation. Please contact an administrator.');
    if ($current_user
      ->hasPermission('administer printable')) {
      $url = Url::fromRoute('printable.format_configure_pdf');
      $link = Link::createFromRoute('configure a PDF library', 'printable.format_configure_pdf');
      $msg = t('Please @link to print certificates.', [
        '@link' => $link
          ->toString(),
      ]);
    }
    return [
      '#markup' => $msg,
    ];
  }

  // Everything is configured, build the PDF
  $render = $template
    ->renderView($account, $course);
  $pdf_gen
    ->addPage(render($render));
  return $pdf_gen
    ->stream($pdf_gen
    ->getObject()
    ->getPdfFilename());
}