PDF thumbnail generation for Craft CMS 3
The craft.PDFThumb.thumbnail() template tag returns a URL to the generated thumbnail.
{% for myAsset in item.assetRef %}
<img src="{{ craft.PDFThumb.thumbnail(myAsset) }}">
<img src="{{ craft.PDFThumb.thumbnail(myAsset, 100, 100) }}">
<img src="{{ craft.PDFThumb.thumbnail(myAsset, 100, 150, true, 'jpg') }}">
{% endfor %}
Heavily based on https://github.com/carnesmedia/pthumb
PThumb is a Craft plugin that generates thumbnail preview images for PDF documents. It's easy to drop into your templates, requires only the commmand-line version of ImageMagick (not the PHP bindings), and caches the generated thumbnails.
It's still very much a work in progress, so use it in production environments at your own risk.
- Download the ZIP file
- Unzip it and move the
pthumbfolder tocraft/plugins - Install the plugin from the Plugins page of your control panel
- Click on the PThumb link on the plugins page to configure the plugin
| Setting | Description |
|---|---|
| Storage Path | Absolute or relative path to the storage directory for PDFs on the server, ex ~/craft/public/uploads/pdfs |
| Base URL | URL to the storage path, ex /uploads/pdfs |
The craft.PThumb.thumbnail() template tag returns a URL to the generated thumbnail.
<img src="{{ craft.PThumb.thumbnail(myAsset) }}">
<img src="{{ craft.PThumb.thumbnail(myAsset, 100, 100) }}">
<img src="{{ craft.PThumb.thumbnail(myAsset, 100, 150, true, 'jpg') }}">
The arguments, in order, are:
| Argument | Description |
|---|---|
| Asset | Required |
| Width | In pixels, default: 250 |
| Height | In pixels, default: 250 |
| Exact size | Force the canvas to exactly match the height/width specified by extending transparent (PNG) or white (JPG) space to the dimensions, useful for producing square images of rectangular documents, default: false |
| File format | Options are 'png' or 'jpg', default: png |
- Files must be limited to a single directory
- Files must be stored locally