Most of the PDF I used for test results in a black image or mostly black with some parts of the PDF in it.

What I did to solve it was the following: (pdfpreview.module line 211)

function _pdfpreview_convert_first_page($source, $dest, $args = array()) {
  $source = drupal_realpath($source) . '[0]';
  $dest = drupal_realpath($dest);

//  $args['quality'] = '-quality ' . escapeshellarg(variable_get('imagemagick_quality', PDFPREVIEW_DEFAULT_QUALITY));
//  $args['previewsize'] = '-resize ' . escapeshellarg(variable_get('pdfpreview_previewsize', PDFPREVIEW_DEFAULT_SIZE));
  $args['alpha'] = '-alpha ' . escapeshellarg('off');
//  $args['background'] = '-background ' . escapeshellarg('white');
//  $args['flatten'] = '-flatten';

And on line 269

function _pdfpreview_output_filename($file) {

Changed JPG extension to PNG.

By doing this, the preview is generated as PNG with transparency. Then Im using imagecache actions on the used preset to select the "change file format" action to jpg

I dont know how to submit a patch, sorry about this, I hope my modifications are understandable.

Comments

jtwalters’s picture

Or you can keep JPG format which is more suited for high resolution previews (perhaps larger than 200x200 pixels)

See my convert args at: http://drupal.org/node/1715484#comment-6318728

leda.ch’s picture

The solution #1 did not work for me, but the code above (issue summary) works a charm!

Thanks for this!
leda

lpc’s picture

Hi,

Solution works great, thanks!

Lukas

kelvinleehk’s picture

Version: 7.x-2.1 » 7.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new540 bytes

I had the same problem too. Instead of converting the PDF to PNG first, turns out there is a -trim flag that can trim the white / black space when converting.

This is the first time I ever roll a patch. Please let me know if I did anything wrong.

ayalon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

#4 is the correct and working solution! Please commit it!

joel_guesclin’s picture

I have a similar problem in Drupal 6 which is not fixed by using the trim flag, since the black areas are inside the image not just round the outside. For the moment I have fixed it by having output go to a PNG file but this is a hack which is not very satisfying. It would perhaps be preferable to make this a setting?

MetalG’s picture

I'm using win7 I manually edited the file, but its not working.. :(

can Anyone give me a correct working pdfpreview.module file?

thx

symvan’s picture

StatusFileSize
new10.55 KB
new12.08 KB

Hi,

The original solution in the issue summary does work because it removes the -resize option.
Also the '-trim' solution does not seem to work as needed (it trims the border).

According to the answer to a false bug report for imagemagic (convert) here the problem is not ImageMagick version (hence not a real bug) but the PDF file fed to it (it has transparency) and one needs to flatten the pdf before converting to jpeg.

Therefore the solution would be to add: the following lines

  $args['background'] = '-background white';
  $args['alpha'] = '-alpha remove';
  $args['colorspace'] = '-colorspace RGB';

under function _pdfpreview_convert_first_page (file pdfpreview.module).

The above options:

  1. ensure the background is white
  2. remove transparency
  3. ensure the colorspace is RGB (standard for WWW)

respectively.

I attach a sample pdf file and the corresponding JPG file BEFORE the above addition. After the addition the file renders fine with white bg.

I have tested this on 6.x-1.1 but checked 7.x-2.1 code and it is identical so this addition should also work for the 7.x branch. Since this is a common issue with certain pdf files (mine come from compiling XeLaTeX documents) it might worth adding the code to the development branches.

axe312’s picture

Arguments from #8 are working correctly.

here is the patch for the current dev, should also work on the stable release!

xaqrox’s picture

Status: Reviewed & tested by the community » Needs work

I'm getting this error:

convert: UnrecognizedAlphaChannelType `remove' @ error/convert.c/ConvertImageCommand/655

If I remove the line from the patch in #9 that sets the alpha option, there's no error, but the preview is still black. Here are the parameters of the offending convert command, courtesy of dpm():

'/var/www/rad/j4j/sites/default/files/collection/news_articles/2012-OWF_Lessig-CC.pdf[0]' -quality '100' -resize '1024x1024' -background white -alpha remove -colorspace RGB '/var/www/rad/j4j/sites/default/files/pdfpreview/4-2012-OWF_Lessig-CC.png'

I'm using pdfpreview-7.x-2.1, imagemagick-7.x-1.0, and convert -version says 6.6.9-7... any suggestions?

xaqrox’s picture

I have solved my problem but not in general. I found two problems:

  1. For whatever reason, my version of convert does not have a "Remove" option. I saw "Off" in the output of convert -list alpha, so I tried that and the error went away. I don't understand alpha channels well enough to know what I'm actually asking for with that, but it seems to work.
  2. But the image was still black. So I started removing options, and found that eliminating the -resize option stopped the black from happening. So, referring to the online IM docs, I set the "Preview Size" field in the PDFPreview settings to "100%" (even though it asks for pixels, it doesn't validate for that) and everything was fine.

Here's the successful commmand:

'/var/www/rad/j4j/sites/default/files/collection/news_articles/poignant-guide.pdf[0]' -quality '100' -resize '100%' -background white -alpha Off -colorspace RGB '/var/www/rad/j4j/sites/default/files/pdfpreview/7-poignant-guide.png'

My application can live without resizing, but even so all this is definitely a hack, and this module needs more work. I can't be the one to do it right now, but I'll put up a couple issues pointing in the right direction.

sakthipriyadevakumar’s picture

the code above works for me...thanks...

johannesdr’s picture

The patches above did not work for me.
I have found some other options in this stackoverflow thread http://stackoverflow.com/questions/10934456/imagemagick-pdf-to-jpgs-some... and adding -flatten solves the problem for me.

johannesdr’s picture

Status: Needs work » Needs review
mfb’s picture

StatusFileSize
new625 bytes

Ok here's my take on a fix: set colorspace to rgb and turn alpha off.

guillaumev’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Can confirm that the last patch provided in this issue (comment #15) is working.

bzitzow’s picture

UPDATE: I put the wrong comment number! :( How can I edit that?

I applied patch #15 which resolved the black overlay, but introduced another problem. Results:

  • Forcing the RGB color profile did not look good based on the source pdf's that we need converted
  • The black overlay was removed

I re-rolled the patch, removing the forced color profile setting. This gave the results of no black overlay, and no unnecessary color modification.

I assert that forcing the color profile should not be included in this fix as the problem is related to the alpha/transparency and not color profile.

About the environment:

Drupal 7.24

ImageMagick Version:

Version: ImageMagick 6.7.7-10 2013-09-10 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

PHP Version:

PHP 5.5.3-1ubuntu2.2 (cli) (built: Feb 28 2014 20:06:05)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

PdfPreview Version:

PDF Preview (pdfpreview) 7.x-2.1

pbuyle’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #17 works for me. And I agree, changing color profile should not be part of the solution.

However, accoding to ImageMagick documentation at http://www.imagemagick.org/Usage/masking/#alpha_off:

Also as many file formats do not allow transparency (such as JPEG), these file formats automatically do the equivalent of a "-alpha Off" when the image is saved (without actually doing so). Generally this results in all transparent areas typically turning black when saved as a JPEG image. See Alpha Remove - Removing Transparency below for the corect way to remove transparency before saving to a JPEG file format.

So -alpha off should not be needed for a JPG file. However, clearly my test file and bzitzow's own file show the contrary. It may safer to use -background white -alpha remove.

rbrownell’s picture

Issue tags: +apply.patch

I can confirm that this is working for me as well. I would suggest applying the patch in #17.

juanjo_vlc’s picture

Status: Reviewed & tested by the community » Fixed

  • mongolito404 committed 578698d on 7.x-2.x
    Issue #1707176: Black PDF Preview generated with some PDFs
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

nbuonin’s picture

Did this patch get rolled back by this later commit?: https://www.drupal.org/node/1748184

I'm getting black images, so I compared the commit from #21 above with what I downloaded yesterday and I don't see the remove alpha arg:

$args['alpha'] = '-alpha off';

in the module file.

rbrownell’s picture

Status: Closed (fixed) » Active

Reopening based on #23.

rbrownell’s picture

Status: Active » Needs review
pbuyle’s picture

The option is still present in _pdfpreview_convert_first_page() (cf. http://cgit.drupalcode.org/pdfpreview/tree/pdfpreview.module?h=7.x-2.x#n295)

Commit for http://cgit.drupalcode.org/pdfpreview/commit/?id=eb692f1 for #1748184: Quality taken from an imagemagick variable instead of PDFPreview did not remove the line (as clearly shown by the commit diff).

Both commits have not been released yet, you have to use the -dev version to have them.

pbuyle’s picture

Status: Needs review » Closed (fixed)
guillaumev’s picture

Status: Closed (fixed) » Needs work

Reopening this as, with imagemagick 6.8.9 (and apparently 6.8.x and 6.7.x), the correct option to use is "-alpha remove". See http://stackoverflow.com/questions/22994313/imagemagick-convert-pdf-with...

guillaumev’s picture

StatusFileSize
new559 bytes

Here is the patch that's working for me, but I believe a better patch is needed with a switch depending on the version of Imagemagick being used...

pbuyle’s picture

Status: Needs work » Needs review

The documentation for the -alpha option does define both remove and off, but the difference is unclear to me. Also, there is not indication that remove or off behaviors changed. In the comments for the linked Stack Overflow question, a user report not having issue with ImageMagick 6.8.9 and 6.7.7. It looks like a rarer issue.

However, if remove does always provide the wanted effect, I don't see any reason not to use it.

guillaumev’s picture

On my side, -alpha off generates black thumbnails, while -alpha remove does not...

pbuyle’s picture

@guillaumev Yes, obviously, otherwise you would not have submitted your patch. But does it work on other peoples' installs where -alpha off does work. We don't want to introduce a regression for these users. So we either need confirmed test/review that it does not break the feature for these users, or find a authoritative source for the options (sadly ImageMagick documentation does not mention which version introduced the options, not does it clearly explain the different behaviours).

a.ross’s picture

I had this issue with this ImageMagick 6.7.7
I haven't tried the other options (apart from this manual patch, I'm still on the old stable release), but -alpha remove works for me.

joran lafleuriel’s picture

I had this problem...

Solved using ImageMagick 6.7.7-10 and this module in version 7.x-2.x-dev

Thanks for your help...

pbuyle’s picture

pbuyle’s picture

This issue either need confirmation that -alpha remove works fine most users or a authoritative source that says it should (some IM expert or developer).

stijn.blomme’s picture

For some reason black images are also generated when your image styles are exported using features.
When manually creating an image style trough the UI everything seems to work fine...

fhey’s picture

There's a drupal_alter in the pdfpreview module, so I managed to fix this problem by adding the following arguments to the $args array in an alter:
$args['background'] = '-background white';
$args['flatten'] = '-flatten';

I would also like to note that $args['alpha'] = '-alpha remove'; did not work for me.

pbuyle’s picture

Please inform us on what IM version you are running, and if you use the patch in #29 or not.

ultrabob’s picture

I can confirm that #29 works for me. Using ImageMagick 6.9.3-0

However removing the alpha altogether and adding in `$args['flatten'] = '-flatten';` as in #38 but without the background argument also did it for me. If that works consistently it could get around the possible inconsistency with alpha arguments.

If I knew how to roll and submit a patch I'd do so.

ultrabob’s picture

I can confirm that my suggestion in #40 works for both 6.9.3-0 on Mac OS X 10.11 and on ImageMagick 6.7.7-10 on Ubuntu 14.04.3 LTS. Maybe I should go figure out how to roll a patch for drupal?

jrb’s picture

StatusFileSize
new592 bytes

Here's a patch for the solution in #40. This worked for me while the other solutions above did not.

ultrabob’s picture

Thanks @jrb. It appears that your patch matches more closely with #38. I found that I could remove the background option without impacting the result. Do you find that argument is necessary? I put my flatten argument before the other options, just in case that makes a difference.

pbuyle’s picture

Status: Needs review » Reviewed & tested by the community

Actually adding a white background make more sense than simply turning off/removing the alpha channel and expecting the background to turn white. And it make it obvious how to change the background color using an alter hook.

  • pbuyle committed d335817 on 7.x-2.x authored by jrb
    Issue #1707176 by bzitzow, guillaumev, johannesdr, jrb, kelvinleehk, mfb...
pbuyle’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.