Jump to content

PDF Fieldtype/Inputfield

Recommended Posts

PDF Fieldtype/Inputfield
Module for ProcessWire allowing you to easily generate thumbnails of the PDF files embedded to the site.
Current version: 1.1.2 (Changelog)

Module page: http://modules.processwire.com/modules/fieldtype-pdf
Github: https://github.com/uiii/ProcessWire-FieldtypePDF

For detailed instructions see: https://github.com/uiii/ProcessWire-FieldtypePDF/blob/master/README.md

  • Like 22
Link to comment
Share on other sites

Looks great!

A few suggestions if I may :)

I think you should note somewhere is the requirement for imagemagick, ghostscript, and the imagick extension. Also, do a class_exists check on imagick and fail with a nice error if it isn't available.

Also, you may want to add these lines to your code:


Also, if you find that the colors in the image are not matching those in the PDF, this gives instructions on the changes that need to be made to your imagemagick delegates file: http://www.lassosoft.com/CMYK-Colour-Matching-with-ImageMagick

Link to comment
Share on other sites

Great first post! ;)

Just quickly looking and reading. I think instead of creating new class with extending Pagefile you could add a method via hook to Pagefile

$this->addHook("Pagefile::thumbnail", $this, "thumbnail");

public function thumbnail($width, $height) {}

Not sure if that's better at all. Looks all ok, and may better extend with new Class... but just wanted to mention.

  • Like 1
Link to comment
Share on other sites

Hi, thanks for the replies. :-)

I think you should note somewhere is the requirement for imagemagick, ghostscript, and the imagick extension ...

 Yes, that's right. When and how is the proper way to print this error?

Also, if you find that the colors in the image are not matching those in the PDF, this gives instructions on the changes that need to be made to your imagemagick delegates file: http://www.lassosoft.com/CMYK-Colour-Matching-with-ImageMagick

I haven't much experience with ImageMagick. I've just found somewhere the code snippet of "pdf to image" conversion and use it. What consequences will have these two options? 
The "delegate" thing - I think it's not related to the module itself.

$this->addHook("Pagefile::thumbnail", $this, "thumbnail");

public function thumbnail($width, $height) {}

Hey, I didn't know that it's so simple :-). If it's better or not I don't know, maybe the class is more suitable for future module's extensions.

  • Like 1
Link to comment
Share on other sites

You can provide the error like this:

if(!class_exists('Imagick')) {
    $this->error(__("This module requires the php imagick extension, but it is not installed"));

You could use this in the install routine of the module.

I am not sure how to check for ghostscript. A hacky way would be to include a tiny PDF with your module and run the thumbnail generator on it. If the thumbnail is created, then it must be available. If not, return another error.

The cropbox setting is important because PDFs can have a crop applied to them and if you don't set this, the thumbnail you generate will show the entire contents of the page, not just what is visible in the PDF.

The colorspace_rgb is just to convert colors to RGB. The IM delegates settings I linked to are not something you can do with the module, but I think they are worth noting in your readme. Without those changes, some colors can turn out pretty fluorescent. Of course many thumbnails will look just fine depending on the colors used.

Just an FYI - I created a similar module a long time ago that I haven't ever got around to finalizing for release:


The main difference with this one is that it creates large preview images for every page of the PDF and then relies on PW to create the thumbnails as requested from these large images. It's not beautifully coded, but you might find something of interest in there. The reasons I never released were because it can be slow to count the number of pages in a PDF (which I wanted to store) depending on what other libraries are available on the server, and also the generation of images for a document of a couple of hundred pages can take a long time and I never got around to setting up something so that this is done in the background after page save.

  • Like 2
Link to comment
Share on other sites

Fine, thanks. I'll update the module soon.

Just an FYI - I created a similar module a long time ago that I haven't ever got around to finalizing for release:

Yes, I've found it and got a little inspiration. Originally I wanted to use it, but I don't like it requires exact, properly named fields to work. And conversion of the whole PDF isn't my need, just the first page thumbnail. But based on this a got an idea to add optional parameter to the thumbnail method specifing the page number to convert.

Link to comment
Share on other sites

Yes, I've found it and got a little inspiration. Originally I wanted to use it, but I don't like it requires exact, properly named fields to work. 

Yeah, I didn't know about how to make a configurable module back then :)

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

Hi and thanks for this nice module  :)

I'm having a bit of a problem with the thumbnail component of the module.  Sometimes when I create a thumbnail the resize creates extra noise in the image. Sometimes deleting the pdf from the page and re-uploading it removes the noise, so it doesn't seem to be an issue with the file itself. For now I'm working around it by commenting out the line in the module that resizes the image ($imagick->scaleImage($width, $height); from PagePDF.php) and using Image's size function. 


Any help would be appreciated. 


Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...

I'm having huge problems with ImageMagick on my local setup.

I get the following error in the backend:

NoDecodeDelegateForThisImageFormat `C:/usr/local/domain.local/public_html/site/assets/files/1037/testfile.pdf' @ error/constitute.c/ReadImage/555

As I understand it, this has something to do with image formats. Doing a var_dump(Imagick::queryFormats()) yields an empty array.

I have the php_imagick.dll extension installed, along with the necessary binaries, all in my System PATH.

The only version that works with my PHP 5.5.11 (installed with XAMPP 1.8.3) is ImageMagick 6.8.8-4 Q16 x86. The Windows installer is the same version. Every other version causes PHP to skip loading the extension.

No errors in php_error_log.

Any ideas?

Update - If I run convert -list configure, I see that there is no pdf delegate listed. How do I install that? And why isn't it included with IM?

Update - seems this has more to do with GhostScript... I've always had it installed, so I don't know where the problem is.

Note - Have also asked on SO here: http://stackoverflow.com/questions/27382843

Link to comment
Share on other sites

  • 2 months later...

Has anyone a hint how to get this awesome module to work if there is no imagick extension on the server but ImageMagick is available as executable? Could it be possible to write a wrapper for the excution of ImageMagick or am I missing something which is only available by the PHP Modul?

By the way ... I want to use it on a managed server from DomainFactory.

Link to comment
Share on other sites

You could definitely do this so long as you have exec available for PHP and also have imagemagick compiled with ghostscript. 

If these requirements are met, then something like this should work:

exec('convert -density 600 document.pdf[0] -colorspace RGB -resample 300 output.png');

inside the thumbnails function: https://github.com/uiii/ProcessWire-FieldtypePDF/blob/master/PagePDF.php#L7

Link to comment
Share on other sites

  • 1 month later...

Hi Mike,

I'm not sure if there is some minimal version of ghostscript needed. I have installed ghostscript 9.15 with imagemagick 6.9.0 and according to phpinfo the php's imagick module is of version 3.1.2. In this configuration everything works fine.

No binaries are executed directly from php, the php's imagick module is used (imagick.so in php.ini), but you must have installed ghostscript and imagemagick as a system package.

Hope this helps you.

BTW: If you are going to use this module on PW 2.5, you will probably get some errors. I'm working on that and hope to finish it this week. PW 2.4 should be ok. 

Link to comment
Share on other sites

Really, so you are lucky :-). There are some issues https://github.com/uiii/ProcessWire-FieldtypePDF/issues. Not all issues are fatal, but needs to fix. I have already fixed all of them but not published yet. I have also changed the API (still backward compatible). The new version isn't ready yet, I need to update the documentation and want to add some tests.

Link to comment
Share on other sites

Hehe, guess so. I originally used it in a development-site on 2.5.13, and have kept up to date since, now on 2.5.23 (current). The only problems I had (only on production server) were specifically about Imagick and GhostScript.

Will give the update a test when you release it. :-)

Link to comment
Share on other sites

Hi Richard

So GhostScript was installed on my shared server, but I'm getting this now:

 - PDFDelegateFailed `[ghostscript library 8.70] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300" -dUseCropBox -dFirstPage=1 -dLastPage=1 "-sOutputFile=/tmp/magick-11484_KMMlcXf5Ras%d" "-f/tmp/magick-11484_ZdfVOglomq7" "-f/tmp/magick-11484jAuPBFSKDl5M"':    **** Warning:  An error occurred while reading an XREF table.
   **** The file has been damaged.  This may have been caused
   **** by a problem while converting or transfering the file.
   **** Ghostscript will attempt to recover the data.
   **** Page has no /MediaBox attribute. Using the current page size.
Error: /stackunderflow in --run--
Operand stack:
   --dict:4/4(L)--   612.0   792.0   3
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1862   1   3   %oparray_pop   1861   1   3   %oparray_pop   1845   1   3   %oparray_pop   --nostringval--   --nostringval--   2   1   1   --nostringval--   %for_pos_int_continue   --nostringval--   --nostringval--
Dictionary stack:
   --dict:1157/1684(ro)(G)--   --dict:1/20(G)--   --dict:75/200(L)--   --dict:75/200(L)--   --dict:106/127(ro)(G)--   --dict:286/300(ro)(G)--   --dict:23/25(L)--   --dict:2/6(L)--
Current allocation mode is local
GPL Ghostscript 8.70: Unrecoverable error, exit code 1
 @ error/pdf.c/InvokePDFDelegate/271

Have you seen this before?

Update: Seems that the error is only appearing for one PDF file. I'll need to convert it or something to get it to work. Anyways, I also got an error saying "Field "certificate_pdf" is not yet ready to use and needs to be configured." I don't quite understand what needs to be configured...

Update: Just saw this - need to upgrade GS.

Link to comment
Share on other sites

Hi Mike,

I haven't seen this GS error before. I'm not an expert in Ghostscript nor ImageMagick, I have just go through many internet discussions and found working php code for pdf to image conversion. But as you say, it looks like a GS bug.

I also got an error saying "Field "certificate_pdf" is not yet ready to use and needs to be configured." I don't quite understand what needs to be configured...

This is one of the issues in PW 2.5 as I mentioned earlier. See https://github.com/uiii/ProcessWire-FieldtypePDF/issues/6. If you want, you can temporarily use  somatonic's fork or wait for my upgrade. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By MarkE
      This fieldtype and inputfield bundle was built for storing measurement values within a field, rendering them in a variety of formats and converting them to other units or otherwise modifying them via the API.
      The API consists of a number of predefined functions, some of which include...
      render() for rendering the measurement object, valueAs() for converting the value to another unit value, convertTo() for converting the whole measurement object to different units, and add() and subtract() for for modifying the stored value by the value (converted as required) in another measurement. In the admin the inputfield includes a checkbox (which can be optionally disabled) for converting values on page save. For an example if a value was typed in as centimeters, the unit was changed to metres, and the page saved with this checkbox selected, said value would be automatically converted so that e.g. 170 cm becomes 1.7 m.

      A simple length field using Fieldtype Measurement and Inputfield Measurement.
      Combination units (e.g. feet and inches) are also supported.
      Please note that this module is 'proof of concept' at the moment - there are limited units available and quite a lot of code tidying to do. More units will be added shortly.
      See the GitHub at https://github.com/MetaTunes/FieldtypeMeasurement for full details and updates.
    • By tcnet
      File Manager for ProcessWire is a module to manager files and folders from the CMS backend. It supports creating, deleting, renaming, packing, unpacking, uploading, downloading and editing of files and folders. The integrated code editor ACE supports highlighting of all common programming languages.

      This module is probably the most powerful module. You might destroy your processwire installation if you don't exactly know what you doing. Be careful and use it at your own risk!
      ACE code editor
      This module uses ACE code editor available from: https://github.com/ajaxorg/ace

      This module uses the JavaScript dragscroll available from: http://github.com/asvd/dragscroll. Dragscroll adds the ability to drag the table horizontally with the mouse pointer.
      PHP File Manager
      This module uses a modified version of PHP File Manager available from: https://github.com/alexantr/filemanager
    • By tcnet
      This module implements the website live chat service from tawk.to. Actually the module doesn't have to do much. It just need to inserted a few lines of JavaScript just before the closing body tag </body> on each side. However, the module offers additional options to display the widget only on certain pages.
      Create an account
      Visit https://www.tawk.to and create an account. It's free! At some point you will reach a page where you can copy the required JavaScript-code.

      Open the module settings and paste the JavaScript-code into the field as shown below. Click "Submit" and that's all.

      Open the module settings
      The settings for this module are located int the menu Modules=>Configure=>LiveChatTawkTo.

    • By tcnet
      Session Viewer is a module for ProcessWire to list session files and display session data. This module is helpful to display the session data of a specific session or to kick out a logged in user by simply delete his session file. After installation the module is available in the Setup menu.

      The following conditions must be met for the module to work properly:
      Session files
      Session data must be stored in session files, which is the default way in ProcessWire. Sessions stored in the database are not supported by this module. The path to the directory where the session files are stored must be declared in the ProcessWire configuration which is by default: site/assets/sessions.
      Serialize handler
      In order to transform session data easier back to a PHP array, the session data is stored serialized. PHP offers a way to declare a custom serialize handler. This module supports only the default serialize handlers: php, php_binary and php_serialize. WDDX was dropped in PHP 7.4.0 and is therefore not supported by this module as well as any other custom serialize handler. Which serialize handler is actually used you can find out in the module configuration which is available under Modules=>Configure=>SessionViewer.

      Session data
      The session data can be displayed in two different ways. PHP's default output for arrays print_r() or by default for this module nice_r() offered on github: https://github.com/uuf6429/nice_r. There is a setting in the module configuration if someone prefers print_r(). Apart from the better handling and overview of the folded session data the output of nice_r() looks indeed nicer.

      ProcessWire module directory
    • By Robin S
      Repeater Easy Sort
      Adds a compact "easy-sort" mode to Repeater and Repeater Matrix, making those fields easier to sort when there are a large number of items.
      The module also enhances Repeater Matrix by allowing a colour to be set for each matrix type. This colour is used in the item headers and in the "add new" links, to help visually distinguish different matrix types in the inputfield.
      A Repeater field

      A Repeater Matrix field with custom header colours

      Easy-sort mode
      Each Repeater/Matrix item gets an double-arrow icon in the item header. Click this icon to enter easy-sort mode.
      While in easy-sort mode:
      The items will reduce in width so that more items can be shown on the screen at once. The minimum width is configurable in the field settings. Any items that were in an open state are collapsed, but when you exit easy-sort mode the previously open items will be reopened. You can drag an item left/right/up/down to sort it within the items. The item that you clicked the icon for is shown with a black background. This makes it easier to find the item you want to move in easy-sort mode. You can click an item header to open the item. An "Exit easy-sort mode" button appears at the bottom of the inputfield. Configuration
      In the field settings for Repeater and Repeater Matrix fields you can define a minimum width in pixels for items in easy-sort mode. While in easy-sort mode the items will be sized to neatly fill the available width on any screen size but will never be narrower than the width you set here.
      In the field settings for Repeater Matrix you can define a custom header colour for each matrix type using an HTML "color" type input. The default colour for this type of input is black, so when black is selected in the input it means that no custom colour will be applied to the header.
      The easy-sort mode is only possible on Repeater/Matrix fields that do not use the "item depth" option.
  • Create New...