function CertificateController::certificateDownload in Certificate 4.x
Stream a PDF to the browser.
Parameters
$entity:
AccountInterface $user:
CertificateTemplate $certificate_template:
boolean $preview Send to browser instead:
Return value
binary
1 call to CertificateController::certificateDownload()
- CertificateController::certificatePage in src/
Controller/ CertificateController.php - Full tab
File
- src/
Controller/ CertificateController.php, line 191
Class
Namespace
Drupal\certificate\ControllerCode
function certificateDownload(EntityInterface $entity = NULL, AccountInterface $user, CertificateTemplate $certificate_template, $preview = FALSE) {
if (!($entity = $this
->getEntityFromRoute())) {
return AccessResult::neutral();
}
$snapshots_enabled = (bool) Drupal::config('certificate.settings')
->get('snapshots');
$snapshot_params = [
'entity_id' => $entity
->id(),
'entity_type' => $entity
->bundle(),
'uid' => $user
->id(),
];
if ($snapshots_enabled) {
$snapshot_search = Drupal::entityTypeManager()
->getStorage('certificate_snapshot')
->loadByProperties($snapshot_params);
$html = !empty($snapshot_search) ? current($snapshot_search)
->get('snapshot')->value : NULL;
}
// If no snapshot HTML found, load the entity
if (empty($html)) {
$renderView = $certificate_template
->renderView($user, $entity);
$html = render($renderView);
}
// Add base HREF so images work.
$base = \Drupal::request()
->getSchemeAndHttpHost();
$html = "<base href=\"{$base}\">" . $html;
// Save a new snapshot if none exists
if ($snapshots_enabled && empty($snapshot_search)) {
$snapshot_params['snapshot'] = $html;
CertificateSnapshot::create($snapshot_params)
->save();
}
if (\Drupal::currentUser()
->hasPermission('administer certificate') && isset($_GET['preview'])) {
print $html;
exit;
}
// Get the PDF engine.
$pdf_gen = $certificate_template
->loadPrintableEngine([
'orientation' => $certificate_template
->get('orientation')
->getString(),
]);
// 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 entity print')) {
$link = Link::createFromRoute('configure a PDF library', 'entity_print.settings');
$msg = t('Please @link to print certificates. Error: @error', [
'@link' => $link
->toString(),
'@error' => $pdf_gen ? $pdf_gen
->getStderr() : '',
]);
}
return [
'#markup' => $msg,
];
}
// Engine is configured, proceed!
// Add the page
$pdf_gen
->addPage($html);
// Create that pdf and send it!
$pdf_gen
->send($entity
->label(), TRUE);
}