You are here

public function PrintBuilder::savePrintable in Entity Print 8.2

Render any content entity as a printed document and save to disk.

Be careful when not specifying a uri as the default behaviour will use the default file scheme which is likely to be public and therefore putting a rendered version of this entity in a web accessible location. If you want to keep the files private, you must specify the uri yourself when calling this method.

Parameters

\Drupal\Core\Entity\EntityInterface[] $entities: The content entity to render.

\Drupal\entity_print\Plugin\PrintEngineInterface $print_engine: The plugin id of the Print engine to use.

string $scheme: The Drupal scheme.

string $filename: (optional) The filename or empty to have one generated.

bool $use_default_css: (optional) TRUE if you want the default CSS included, otherwise FALSE.

Return value

string FALSE or the URI to the file. E.g. public://my-file.pdf.

Overrides PrintBuilderInterface::savePrintable

File

src/PrintBuilder.php, line 86

Class

PrintBuilder
The print builder service.

Namespace

Drupal\entity_print

Code

public function savePrintable(array $entities, PrintEngineInterface $print_engine, $scheme = 'public', $filename = FALSE, $use_default_css = TRUE) {
  $renderer = $this
    ->prepareRenderer($entities, $print_engine, $use_default_css);

  // Allow other modules to alter the generated Print object.
  $this->dispatcher
    ->dispatch(PrintEvents::PRE_SEND, new PreSendPrintEvent($print_engine, $entities));

  // If we didn't have a URI passed in the generate one.
  if (!$filename) {
    $filename = $renderer
      ->getFilename($entities) . '.' . $print_engine
      ->getExportType()
      ->getFileExtension();
  }
  $uri = "{$scheme}://{$filename}";

  // Save the file.
  return \Drupal::service('file_system')
    ->saveData($print_engine
    ->getBlob(), $uri, FileSystemInterface::EXISTS_REPLACE);
}