Preview Features

Preview Overlay

The preview overlay feature has been used throughout this docs and is great for local development when placed in /site/ready.php as it instantly shows the resulting PDF on every page load:

/** @var RockPdf $pdf */
$pdf = $modules->get('RockPdf');
$pdf
  ->load("/site/test.php")
  ->save(preview: true);
RockPdf - Preview Features

File Field Preview

But often you also want a preview of a PDF that is stored in a file field of the ProcessWire backend. Let's say we wanted to write the page title to a PDF and save that PDF to the mypdf field of that page whenever the page is saved. We use the home template for our example.

Create PDF on page save:

// in /site/ready.php
$wire->addHookAfter("Pages::saved(template=home)", function ($event) {
  $page = $event->arguments(0);

  /** @var RockPdf $pdf */
  $pdf = $event->wire->modules->get('RockPdf');
  $pdf
    ->load("/site/test.php", [
      // make saved page available as $page
      // in the loaded document file
      'page' => $page,
    ])
    ->saveToField(
      page: $page,
      field: 'mypdf',
      filename: 'demo.pdf',
    );
});

We output the page title in the loaded file test.php:

<h1><?= $page->title ?></h1>

Show PDF in an iframe:

Finally add this code to /site/ready.php:

/** @var RockPdf $pdf */
$pdf = $modules->get('RockPdf');
// show pdf in iframe for field "mypdf"
$pdf->iframe('mypdf');

Save the page and you'll see the result:

RockPdf - Preview Features