The cookbook "Creating preview images for PDF files" is not cooking for me

I want to make PDF brochures available for download and for this I have tried several times to integrate this cookbook:

No preview images are generated, only the list of PDFs. No error message appears. In the source code the img tag is missing, which indicates that no preview image was generated. There is also no JPEG in the media folder.
Since I lack the expertise on how the plugin works, I would like to go another way: Upload PDF files and manually created thumbnails (both have the same file name).
Since the PDFs rarely change, the effort to create the thumbnail manually is acceptable.
What should be the code in the template to list all PDF and link it to the preview image?
Unfortunately, this code does not work. The devil is definitely in the details.

  <?php foreach ($page->documents()->filterBy('extension', 'pdf') as $document): ?>
      <a href="<?= $document->url() ?>">
        <?php if ($preview = $page->images()->findBy('name', $document->filename() . '.jpg')): ?>
          <img src="<?= $preview->resize(200)->url() ?>" alt="">
        <?php endif; ?>
        <?= $document->filename() ?> (<?= $document->niceSize() ?>)
  <?php endforeach; ?>

What is the exact name relation between image and PDF? Is is mypdf.pdf => mypdf.pdf.jpg or mypdf.pdf => mypdf.jpp?

This will not work, because $file->name() refers to the filename without extension, while $file->filename() includes the extension.

So you are trying to find by name a file with for example the filename mypdf.pdf.jpg.

So if you use mypdf.pdf => mypdf.pdf.jpg, you want to find

 <?php if ($preview = $page->images()->findBy('filename', $document->filename() . '.jpg')): ?>

For mypdf.pdf => mypdf.jpp

 <?php if ($preview = $page->images()->findBy('filename', $document->name() . '.jpg')): ?>

I will review the recipe, but note that it requires Imagemagick to be present on the server.

Edit: Done and fixed some incompatibility issues with newer Kirby/PHP versions.

Feel free to report any issues with error description with the docs in the repo on GitHub.

1 Like

Thank you for your solution.

<?php if ($preview = $page->images()->findBy('filename', $document->name() . '.jpg')): ?>

…worked immediately in my case.

… and I saw that you have already revised the cookbook. I will add it to my installation again and then report if it works.