Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick PDF to JPGs sometimes results in black background

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

like image 607
jahilldev Avatar asked Jun 07 '12 15:06

jahilldev


People also ask

Why does an image on a PDF have a black background?

Hi, This behavior is by design. Transparency is disabled on purpose for PDF/A because we support PDF/A-1, which does not allow transparency. Thanks!

Can ImageMagick convert PDF?

So if you have a vector PDF and you convert it to PDF using ImageMagick, it will rasterize the PDF to pixels (not vectors) and imbed the raster image in a vector PDF shell. That will make the output much larger than the input.


2 Answers

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 
like image 68
Tapio Saarinen Avatar answered Oct 20 '22 01:10

Tapio Saarinen


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 :)

like image 44
jahilldev Avatar answered Oct 20 '22 03:10

jahilldev