Saving PDFs

Now that we created our first RockPdf, let's save it somewhere!

Saving to disk

By default RockPdf will save the PDF to the same folder where it loads the markup file from. You can provide a custom destination like this:

/** @var RockPdf $pdf */
$pdf = $modules->get('RockPdf');
$pdf
  ->load("/site/test.php")
  ->save(
    // custom output path for generated pdf
    to: $config->paths->templates . 'custom-path.pdf',
    preview: true,
  );

// output: /site/templates/custom-path.pdf

If you only provide the filename without the path the pdf will be saved to the same folder as the loaded markup file:

/** @var RockPdf $pdf */
$pdf = $modules->get('RockPdf');
$pdf
  ->load("/site/test.php")
  ->save(
    // custom output name for generated pdf
    to: 'custom-name.pdf',
    preview: true,
  );

// output: /site/custom-name.pdf

Saving to a field

A very common need is to generate a PDF and then save it to a ProcessWire Page. For example when creating invoices for clients or when creating a PDF from content that the client can input.

RockPdf makes that process very easy and straightforward. For this example we assume that we have a field mypdf that is added to the home template.

/** @var RockPdf $pdf */
$pdf = $modules->get('RockPdf');
$pdf
  ->load("/site/test.php")
  ->saveToField(1, 'mypdf');

Reload your page and your PDF should show up in the mypdf field.

RockPdf - Saving PDFs

The saveToField method can take more options to define a custom filename and to append files rather than wiping the field:

/** @var RockPdf $pdf */
$pdf = $modules->get('RockPdf');
$pdf
  ->load("/site/test.php")
  ->saveToField(
    page: 1,
    field: 'mypdf',
    filename: 'demo.pdf',
    append: true,
  );
RockPdf - Saving PDFs

Saving on page save

Most likely you don't want to create a PDF on every page load. Just take your code and move it into a Pages::saved hook in /site/ready.php:

// add hook after page has been saved
$wire->addHookAfter("Pages::saved", function (HookEvent $event) {
  $page = $event->arguments(0);

  // execute this hook only if the homepage is saved
  if($page->template != 'home') return;

  /** @var RockPdf $pdf */
  $pdf = $this->wire->modules->get('RockPdf');
  $pdf
    ->load("/site/test.php")
    ->saveToField(
      page: 1,
      field: 'mypdf',
      filename: 'demo.pdf',
      append: true,
    );
});

Note that we need to use $this->wire->modules instead of just $modules because we are inside a hook, where API variables are not loaded!

Multi-Language

On multi-language setups it is important to request a new instance of RockPdf so that texts appear in the correct language:

  public function createPdf(): void
  {
    $page = $this; // $this is a custom page class
    $userLang = $this->wire->user->language;
    foreach ($this->wire->languages as $i=>$lang) {
      $filename = $lang->name;
      if ($filename == "default") $filename = "en";

      $this->wire->user->language = $lang;
      /** @var RockPdf $rockpdf */
      $rockpdf = wire()->modules->get('RockPdf');
      $rockpdf
        ->addFont([
          'R' => '/site/templates/RockPdf/QUADON.ttf',
        ])
        ->load("/site/templates/RockPdf/_cv.latte", [
          'page' => $page,
        ])
        // ->save(preview: true)
        ->saveToField(
          page: $page,
          field: "my_pdf_field",
          filename: "$filename.pdf",

          // $i holds the loop index
          // on the first iteration this will be 0 so append is false
          // on every other languages it will be positive, so it will append pdfs
          append: $i,
        );
    }
    $this->wire->user->language = $userLang;
  }