Skip to content
OpenCms documentation
OpenCms documentation

Creating thumbnails for PDFs

OpenCms provides a special tag for PDF thumbnail generation: You just provide the PDF, the thumbnail should be generated for, the size and the format (png or jpg) of the resulting thumbnail and the thumbnail will be generated if requested, and reachable via the URL rendered by special tag.

Here we provide detailed information on how to use the tag and point to some technical details.

The main use case of PDF thumbnails will be download options for PDF files. When providing such a download option, it's a good idea to show a thumbnail of the PDF you can download. Maybe, make the thumbnail clickable and start the download on click.

Thumbnails for PDFs are generated in a JSP via the tag <cms:pdfthumbnail>. The tag takes the following attributes, specifying the PDF file used for the thumbnail generation and also some attributes of the thumbnail:

VFS URI of the PDF for which the thumbnail should be created.

Height of the generated thumbnail. Given as integer value, specifying the number of pixels. By default the height is set to -1, which means that it is chosen automatically dependent on the PDF.

Width of the generated thumbnail. Given as integer value, specifying the number of pixels. By default the width is set to -1, which means that it is chosen automatically dependent on the PDF.

Format of the generated thumbnail. Either jpg or png. Default:  png.

The tag expands to a link to the thumbnail. Thus, it is typically used as for the value of the src attribute in an <img> tag. Here's a simple example:

<img src='<cms:pdfthumbnail file="/vfs/uri/to/file.pdf" />' alt="..." title="..." />

In the example, a png image will be generated as thumbnail. It's size is determined by the size of the PDF given as input for the generation.

PDF thumbnails are not stored in the VFS. When a <cms:pdfthumbnail> tag is rendered, a special link to the, not yet present, thumbnail is generated. If the link is requested the first time, the thumbnail is generated. If it is requested again, a cached version of the thumbnail is served. The cached versions are stored on the server's RFS in the folder {webapp home}/WEB-INF/pdfcache. The cache is cleared, if the PDF document used for the thumbnail is altered.

The interesting code for the PDF generation is found in the module org.opencms.pdftools. The class for exposing the tag is found here.