You are here

public function EntityPrintController::viewPdf in Entity Print 8

Output an entity as a PDF.

Parameters

string $entity_type: The entity type.

int $entity_id: The entity id.

Return value

\Symfony\Component\HttpFoundation\Response The response object on error otherwise the PDF is sent.

1 string reference to 'EntityPrintController::viewPdf'
entity_print.routing.yml in ./entity_print.routing.yml
entity_print.routing.yml

File

src/Controller/EntityPrintController.php, line 73

Class

EntityPrintController

Namespace

Drupal\entity_print\Controller

Code

public function viewPdf($entity_type, $entity_id) {

  // Create the PDF engine plugin.
  $config = $this
    ->config('entity_print.settings');
  $entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($entity_id);
  try {
    $pdf_engine = $this->pluginManager
      ->createInstance($config
      ->get('pdf_engine'));
    return (new StreamedResponse(function () use ($entity, $pdf_engine, $config) {

      // The PDF is sent straight to the browser.
      $this->pdfBuilder
        ->getEntityRenderedAsPdf($entity, $pdf_engine, $config
        ->get('force_download'), $config
        ->get('default_css'));
    }))
      ->send();
  } catch (PdfEngineException $e) {

    // Build a safe markup string using Xss::filter() so that the instructions
    // for installing dependencies can contain quotes.
    drupal_set_message(new FormattableMarkup('Error generating PDF: ' . Xss::filter($e
      ->getMessage()), []), 'error');
    return new RedirectResponse($entity
      ->toUrl()
      ->toString());
  }
}