Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
2 posts Page 1 of 1
While working on a small library website, I needed a preview of PDF's book cover page. So I made this small addon which introduces a new tag: pdfthumb

Once installed you can generate a JPG file from specified page from an uploaded PDF file. The syntax of pdfthumb tag is:
Code: Select all
<img src="<cms:pdfthumb pdf_file page='1' width='250' />" alt="<cms:show k_page_title />" > 

assuming that you have defined in your template
Code: Select all
<cms:editable name='pdf_file' label='PDF file' type='file' allowed_ext='pdf' />

The tag uses 2 parameters:

page
which is the page number from uploaded PDF file. If not specified, the default page is 1.

width
which is the width, in pixels, of the resulting JPG file. If not specified, the default width is 500 px.

If you need specific pages and sizes for each PDF file, you can use something like this in template definition:
Code: Select all
<cms:template title="PDF" clonable='1' order='10' >
     <cms:editable name='name' label='Name' type='text' />
     <cms:editable name='pdf_file' label='PDF file' type='file' required='1' allowed_ext='pdf' />
     <cms:editable name='page' label='Page no.' type='text' />
     <cms:editable name='width' label='Width (px)' type='text' />
</cms:template>

and then in page:
Code: Select all
<img src="<cms:pdfthumb pdf_file page="<cms:show page />" width="<cms:show width />" />" alt="<cms:show k_page_title />" >


Installation of the addon is straightforward: extract from the archive the 'pdfthumb' folder and copy it to 'couch/addons' folder in your installed CouchCMS. Add the following line to 'kfunctions.php' from 'couch/addons' folder:
Code: Select all
require_once( K_COUCH_DIR.'addons/pdfthumb/pdfthumb.php' );


Some things to be considered:
1. this tag uses 'imagick' library, which has to be installed on the server. Fortunately, most of the hosting providers have the 'imagick' lib installed. The addon has a rudimentary fallback routine in case 'imagick' is not installed, it will use the included PDF icon.
2. the thumbnail will be generated only once, at first access of the page. Subsequent accesses will use the existing image, unless the page number or image size is changed. Therefore I recommend that after uploading a PDF in admin, to visit once the client-side page in order to start generating the preview. For the first time will be a small delay (a couple of seconds) until 'imagick' works his magic.
3. please note that if you specify a page number greater then total number of pages in the PDF file the addon / imagick lib will crash. I tried to overcome this by counting pages of each and every PDF uploaded, but the site became very sluggish.
4. in order not to clutter the images and files from your defined 'uploads' folder, the thumbnails will be stored in 'pdfthumbs' folder, which will be created automatically.
5. it is not perfect. If you can improve it, feel free to do so.

I hope you will find this addon useful!

Attachments

A very useful addon @pandele.
Thanks :)
2 posts Page 1 of 1
cron