Template for PDF preview image

I followed Sonjas Cookbook to create PDF preview images. I want to add a template to the generated image. To apply the template I added the ‘template’ in the return value of the 'preview in the index.php:

return new File([
	'filename'  => $previewName,
	'parent'     => $this->parent(),
	'template' => 'pdf-preview',
]);
}

How can I assign the template to the preview image?

Have you tried blueprint?

Didn’t work either.
The blueprint\files\image.yml won’t apply.

Du kannst zusätzliche Felder und Bereiche für diese Datei in /site/blueprints/files/default.yml anlegen

Why should it if you apply pdf-preview?

Thats a point…

The PDF gets the template on upload. But the image generated by ImageMagicks convert has no template as it is not uploaded by the panel.

Guess you need to create the file object, then update it before returning, sobthat the meta file gets created.

I tried the following in the file.create:after Hook:

'hooks' => [
	'file.create:after' => function ($file) {
		// only create preview for PDF files
		if ($file->mime() === 'application/pdf') {
			$options	= [
				'quality'	=> 100,
				'page'		=> 0,
				'density'	=> 300,
			];
			$darkroom = new PdfPreview();
			$darkroom->process($file->root(), $options);
	
			$baseName = $file->name();
			$jpgFilename = $baseName . '.jpg';
			$jpgFile = $file->parent()->file($jpgFilename);
			$jpgFile = $jpgFile->changeTemplate('test');
		}
	}
],

But in the dialog I get Call to a member function changeTemplate() on null .
Shouldn’t the JPG file already exist at this point?

What I meant was:

$file = new File([
	'filename'  => $previewName,
	'parent'     => $this->parent(),

]);

$file = $file->changeTemplate('test');

return $file;

That was meant for the preview method, not the hook.

In the file system, yes, as part of the parent files collection, no, you probably need to append it first.

No, I’ve tried this already with no luck.
The txt for the jpg is created when I click the Choose (Auswählen) button. But the txt file contains only the uuid.

You have totally lost me. I think we are in the context of the recipe? Where do you choose anything?

Sorry, I missed the snippets for the template at the end of the cookbook. :dotted_line_face:
I thought preview file method refers to previewing the image in the panel. So of course the assignment of the file template didn’t work. Because the method is only called by the template file for the frontend.

It works now. Thank you for the help & patience!

Since I was looking for this and was about to write the question, I add this:

If you want to add the preview image as the cover for the PDF in the panel you can use:

image:
  query: file.parent.images.findBy("name", file.name)

In the file blueprint ex.: site/blueprints/files/pdf.yml