Create a thumbnail from PDF file

Hi,

It’s possible to create a thumbnail from a PDF file? I can do it for images but when I run the same code for PDF files I get an error.

PS: I know I’ve been asking a lot of questions that maybe are easy, but I’m new to Ruby and I still don’t quite understand how things work here. I’m sorry!

Yes, you just have to specify which page you’re converting in loader options:

ImageProcessing::MiniMagick
  .source(pdf_file)
  .loader(page: 0) # specify page number
  .resize_to_limit!(...)

If you have a multi-page PDF, see this wiki page.

I know I’ve been asking a lot of questions that maybe are easy, but I’m new to Ruby and I still don’t quite understand how things work here.

That’s ok :slightly_smiling_face:

Doesn’t work for me, this is my code:

Attacher.derivatives_processor do |original|
    pdf_thumbnail = ImageProcessing::MiniMagick
      .source(original.path)
      .loader(page: 0) # specify page number
      .resize_to_limit!(220, 220)

    { thumb: pdf_thumbnail, }
end

This is the error:

`convert /tmp/shrine20191115-17045-1glgy43.pdf[0] -auto-orient -resize 220x220> -sharpen 0x1 
/tmp/image_processing20191115-17045-1u8g8t7.pdf` failed with error: convert-im6.q16: not 
authorized `/tmp/shrine20191115-17045-1glgy43.pdf' @ error/constitute.c/ReadImage/412. convert- 
im6.q16: no images defined `/tmp/image_processing20191115-17045-1u8g8t7.pdf' @ 
error/convert.c/ConvertImageCommand/3258.

In this case it’s useful to read the error message. The “not authorized” part indicates to me that the ImageMagick process doesn’t have permissions to your /tmp directory. But I’m not sure, I’d recommend googling what it means.

Actually, I see it’s also complaining about the output path. I think you need to convert the result to an actual image format, e.g. try adding .convert(“jpeg”) to the pipeline.

Hi Jean, Hi Janko,

The below works perfectly fine on my end :

Attacher.derivatives_processor do |original|
magick = ImageProcessing::MiniMagick.source(original).loader(page: 0)
{
thumbnail: magick.convert(“jpg”).flatten.resize_to_fit!(127, 180)
}
end

Jean, don’t forget to flatten (as shown above in the thumbnail derivative) in order to not get the transparent area of the page (then probably most of the PDF pages) appear as black.

(It is pretty strange as a conversion should get rid of the transparent part anyway as jpg does not handle transparency. Though flatten kinda tells Imagemagick to use filling color white, instead of black…)

1 Like

Hi Jean,

Oddly I kinda got an error today when creating a thumbnail for a PDF file.
It happened as I installed Vips on Ubuntu.

When installing Vips, I got asked if I wanted to overwrite my ImageMagick policy file. Which I agreed …

Then got error

MiniMagick::Error (convert /tmp/shrine20191118-7069-e3p9my.pdf[0] -auto-orient -flatten -resize 127x180 -sharpen 0x1 /tmp/image_processing20191118-7069-16vgi2a.jpg failed with error:

convert-im6.q16: not authorized `/tmp/shrine20191118-7069-e3p9my.pdf’ @ error/constitute.c/ReadImage/412.

convert-im6.q16: no images defined `/tmp/image_processing20191118-7069-16vgi2a.jpg’ @ error/convert.c/ConvertImageCommand/3258.

):

I realised that indeed had to change rights for Imagemagick in order to be allowed to alter PDF files .

You may follow this stackoverflow thread in order to enable it back :

(for Ubuntu)

(also this is a development only error…)

Hi Maxence,

I solved my problem, which was caused because I needed to install a library on my operating system (Linux Mint). At this moment I don’t remember the name of the library (sorry).

Thanks anyway for your contribution, it’s very useful!

1 Like