# Pdf thumb driver

**URL:** <https://forum.getkirby.com/t/pdf-thumb-driver/5017>\
**Category:** Questions\
**Tags:** v2\
**Created:** [August 18, 2016, 7:40pm UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017 "2016-08-18T19:40:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [August 18, 2016, 7:40pm UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/1 "2016-08-18T19:40:17Z")

</div>

i want to extend the imagemagick thumbs driver to generate jpgs from pdfs. i have my custom thumbs driver as plugin calling ghostscript using exec(). my thumbdriver it get loaded fine, but code is not called for two reasons:

1. pdf-files because thumb() calls image-\>thumb() which calls image-\>isWebsave() which rejects the pdf as source.  
ghostscript code works fine – tested outside of driver.

> **[Build software better, together](https://github.com/search?q=repo%3Agetkirby%2Fkirby%2BisWebsafe)**
>
> GitHub is where people build software. More than 150 million people use GitHub to discover, fork, and contribute to over 420 million projects.

[https://github.com/getkirby/kirby/blob/f452ae5d2f8cdf2e3f58ac6691781a072ddd282f/kirby/traits/image.php#L190](https://github.com/getkirby/kirby/blob/f452ae5d2f8cdf2e3f58ac6691781a072ddd282f/kirby/traits/image.php#L190)

1. creating thumb rejects non images as well.  
[toolkit/lib/thumb.php at master · getkirby-v2/toolkit · GitHub](https://github.com/getkirby/toolkit/blob/master/lib/thumb.php#L67)

i would like to keep thumbs driver and not create files on my own, recreating thumbs output logic.  
other code in thumbs class should work with pdfs too.  
dimension will return 0w 0h.

> <https://github.com/getkirby-v2/toolkit/blob/94d1d6e14698e73f5c5deb7cc3400d4426e7137b/lib/media.php#L427>

any ideas how to allow pdfs as valid images as far as thumbs are concerned?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [August 19, 2016, 8:58am UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/2 "2016-08-19T08:58:04Z")

</div>

Hm, I guess the intention of thumbs was to make thumbnails with the same image format (JPG becomes a JPG thumb, PNG becomes a PNG thumb), which is why creating thumbs of non-images does not make sense.

What you could do though is to [override the thumb component](https://getkirby.com/docs/developer-guide/plugins/components). You can then apply special treatment to PDFs:

```php
class PdfThumb extends Kirby\Component\Thumb {
  public function create($file, $params) {
    if($file->extension() == 'pdf') {
      // special treatment
    }
    
    return parent::create($file, $params);
  }
}

```

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [August 19, 2016, 9:03am UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/3 "2016-08-19T09:03:08Z")

</div>

i see. so instead of twisting the thumbs-function you are right maybe i really should create a ‘converter’ to create a raw jpg first (like with 300dpi) and then create a thumb of that one (with my resizing, cropping etc).

but i will take a look at the components anyway. thx.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [August 19, 2016, 9:43am UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/4 "2016-08-19T09:43:50Z")

</div>

Not necessarily. You could also do your Ghostscript magic there without a step in between. 🙂

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [August 19, 2016, 9:57am UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/5 "2016-08-19T09:57:55Z")

</div>

oh its quiet tiny magic actually.

```
/* NOTES
   should check if file does not exist already using !f::exists($outPath))
   and prepare path if needed using dir::make(dirname($outPath));
*/
$command = array();
$command[] = 'gs';
$command[] = '-sDEVICE=jpeg';
$command[] = '-r'.$dpi.'x'.$dpi; // like 72, 96 or 300
$command[] = '-dNOPAUSE';
$command[] = '-sOutputFile="'.$outPath.'"'; // like $site->roots()->thumbs().DS.$file->uri()
$command[] = '"'.$file->root().'"';
exec(implode(' ', $command));
```

---

<div class="post-metadata">

**Author:** ![tobystokes](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobystokes/32/745_2.png) [@tobystokes](https://forum.getkirby.com/u/tobystokes)\
**Post date:** [August 22, 2016, 6:36pm UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/6 "2016-08-22T18:36:35Z")

</div>

I’ve recently done this as discussed here: [Create Preview from PDF-File](https://forum.getkirby.com/t/create-preview-from-pdf-file/1080) . So I defined new file method in /site/plugins/methods.php:

```auto
file::$methods['preview'] = function($file, $size='800', $page='0') {

		$key = 'preview_'. md5($file->root());
		$thumb = kirby()->roots()->thumbs() .DS. $key;

		if (f::exists($thumb))
		{

			if ( (f::modified($thumb) > $file->modified())) {
				return new Asset(kirby()->urls()->thumbs() .'/'. $key);
			}
			f::remove($thumb);

	  }

	  $image = new Imagick($file->root().'['.$page.']'); // which page? First by default
		$image->setImageBackgroundColor('white');
		$image->setImageAlphaChannel(imagick::ALPHACHANNEL_REMOVE);
		$image->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);
		$image->setImageFormat('jpg');
		$image->setImageCompression(Imagick::COMPRESSION_JPEG);
	  $image->setImageCompressionQuality(80);
		$image->thumbnailImage($size, 0);
		$image->writeImage($thumb);

		return new Asset(kirby()->urls()->thumbs() .'/'. $key);

    };

```

That does require imageMagick instead of GD, but I don’t think it even needs GhostScript.  
So in your template you can call `$file->preview()` to create a jpg, and then in theory use the thumb plugin on it after.

@lukasbestle if you override the thumb component, does it completely replace the standard thumb methods, or can we just extend them?

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [August 22, 2016, 7:47pm UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/7 "2016-08-22T19:47:34Z")

</div>

thank you @tobystokes. i decided to go for GS since php imagick is not available and can not be enabled on the server i am using. but exec(‘convert…’) works.

```
 echo extension_loaded('imagick') || class_exists("Imagick") ? 'has':'not'; // = not

```

and Ghostscript yields better results when dealing with high resolution vector pdfs – as far as i can tell. 1MB+ vector pdfs.

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [August 22, 2016, 8:08pm UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/8 "2016-08-22T20:08:29Z")

</div>

about the component. see code from @lukasbestle above. he suggest to me just to extend the create function (which is calling it parent class as return value. so everything is the same, just create function does a little more.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [August 24, 2016, 10:36am UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/9 "2016-08-24T10:36:18Z")

</div>

Kirby does not actually require the `Imagick` PHP extension, it uses `exec('convert')` internally, so Imagick could actually work for you here.

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [August 24, 2016, 10:51am UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/10 "2016-08-24T10:51:48Z")

</div>

using convert would work if i can find out how to ‘convert’ a pdf to jpg on my server. i could not find a example that worked on my server. maybe imagick is configured without pdf support – i do not know (yet).

```
convert -density 300 image.pdf -resize 25% image.png
```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [August 24, 2016, 10:53am UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/11 "2016-08-24T10:53:02Z")

</div>

Well, if Ghostscript works for you, you don’t _need_ to use Imagick. 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:55pm UTC](https://forum.getkirby.com/t/pdf-thumb-driver/5017/12 "2025-02-06T16:55:23Z")

</div>

Archived on February 6, 2025, 4:55pm UTC


