60

I have the following:

ghostscript-fonts-5.50-24
ImageMagick-6.7.2-1
ghostscript-9.02-1

Which I use to create a series of JPGs for each page using:

convert -density 175 -colorspace sRGB test.pdf -resize 50% -quality 95 test.jpg

When I run this on my windows machine all appears to work ok, but on our linux server we get the black background problem.

The resulting JPGs have a black background rendering the image un-readable, what am I missing or is there something I should be doing to correct this?

I've been all over google for days but each suggestion doesnt seem to work for me.

Any help is much appreciated, thanks in advance :)

EDIT

Just noticed this output when converting one of the PDFs that produces the black background:

**** Warning: Fonts with Subtype = /TrueType should be embedded.
             The following fonts were not embedded:
                    Arial
                    Arial,Bold
                    Arial,BoldItalic
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Microsoft« Word 2010 <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.

This seems related but as we don't have control over how the PDFs are produced we need some way of fixing this server side.

Thanks again

jahilldev
  • 3,520
  • 4
  • 35
  • 52
  • 4
    _Please notify the author of the software_ - heh, good luck with that `;-)`. – halfer Jun 07 '12 at 16:08
  • I wonder, would setting the `-background` colour help here? Maybe you need to set it to white? – halfer Jun 07 '12 at 16:09
  • 2
    I've managed to get it working by converting to PNG instead of JPG as they support transparency. Thanks for the help – jahilldev Jun 08 '12 at 15:18
  • 2
    Great. Pop your solution in the answer box below, if you would - you can even award yourself the tick! `:)` – halfer Jun 08 '12 at 16:43
  • http://stackoverflow.com/questions/1903841/imagemagick-pdf-to-jpeg-conversion-results-in-green-square-where-image-should-be may be related... – rogerdpack Feb 20 '13 at 09:15
  • Yes, from pdf to jpg was a problem. pdf to png worked. pdf to gif worked and made a much smaller file. – muz the axe Jan 31 '17 at 11:01

3 Answers3

110

Ran into this one today, found this:

https://www.imagemagick.org/discourse-server/viewtopic.php?t=20234

Based on that, these should all work:

  • -flatten
  • -alpha flatten
  • -alpha remove

I'm currently using the below for my specific case which works great:

convert -thumbnail "1280x800>" -density 300 -background white -alpha remove in.pdf out.jpg
Bob Stein
  • 16,271
  • 10
  • 88
  • 101
Tapio Saarinen
  • 2,499
  • 3
  • 20
  • 18
  • 21
    If you want to split a multi-page PDF into several JPEGs per page, `-flatten` will not work, as it will mash all pages into a single image. `-alpha flatten` or `-alpha remove` is the way to go. – florian Jan 28 '15 at 13:06
  • 2
    For C# you can use: image.Alpha(AlphaOption.Remove); – Flappy Jun 11 '15 at 09:35
  • 1
    I am converting PDF to TIFF (for faxing) and was using "-alpha off" by default, but would see all black pages if the page had PDF form fields or signatures pasted in as transparent PNG's. Changing to "-alpha remove" worked as florian suggests. I played with other alpha values but remove is the way to go. – drew010 May 22 '19 at 00:03
  • 1
    If anyone using RMagick and want to map above solution into RMagick then use this Doc: Magick Command Options and Their Equivalent Methods in RMagick - https://rmagick.github.io/optequiv.html – viks Jul 12 '19 at 07:46
  • For rmagick what worked for me was changing the image_type. `image.image_type=Magick::ImageType.new('TrueColorType', 1)` – mrben522 Nov 19 '20 at 16:19
11

Simple fix to this issue is to use an image format that supports transparency, such as png.

So:

convert -density 175 -colorspace sRGB test.pdf -resize 50% -quality 95 test.png

Problem solved :)

jahilldev
  • 3,520
  • 4
  • 35
  • 52
  • @rogerdpack, try http://tinypng.org/ or [Improved pngquant](https://github.com/pornel/improved-pngquant). You know how you can save a PNG as an indexed color image and it looks just like a GIF? Well, it turns out you can do the indexed color palette *and* preserve the alpha transparency. The result is a vastly compressed PNG that looks almost identical to the original. [JPEG 2000](http://en.wikipedia.org/wiki/JPEG_2000) was supposed to have alpha transparency support for JPEGs, but support the file format never really gained traction in web browsers. – thirdender May 07 '13 at 22:15
  • If anyone using RMagick and want to map above solution into RMagick then use this Doc: Magick Command Options and Their Equivalent Methods in RMagick - https://rmagick.github.io/optequiv.html – viks Jul 12 '19 at 07:46
4

If you want a high quality result, use this command:

convert -density 700 input.pdf -resize 25% -append  -quality 98 -alpha remove output.jpg

For windows users, use magick instead of convert

sancho21
  • 3,511
  • 1
  • 39
  • 45