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
Comment #1
jtwalters commentedOr 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
Comment #2
leda.ch commentedThe solution #1 did not work for me, but the code above (issue summary) works a charm!
Thanks for this!
leda
Comment #3
lpc commentedHi,
Solution works great, thanks!
Lukas
Comment #4
kelvinleehk commentedI 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.
Comment #5
ayalon commentedThanks!
#4 is the correct and working solution! Please commit it!
Comment #6
joel_guesclin commentedI 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?
Comment #7
MetalG commentedI'm using win7 I manually edited the file, but its not working.. :(
can Anyone give me a correct working pdfpreview.module file?
thx
Comment #8
symvan commentedHi,
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
under function
_pdfpreview_convert_first_page(filepdfpreview.module).The above options:
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.
Comment #9
axe312 commentedArguments from #8 are working correctly.
here is the patch for the current dev, should also work on the stable release!
Comment #10
xaqroxI'm getting this error:
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
convertcommand, courtesy ofdpm():I'm using pdfpreview-7.x-2.1, imagemagick-7.x-1.0, and
convert -versionsays 6.6.9-7... any suggestions?Comment #11
xaqroxI have solved my problem but not in general. I found two problems:
convertdoes not have a "Remove" option. I saw "Off" in the output ofconvert -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.-resizeoption 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:
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.
Comment #12
sakthipriyadevakumar commentedthe code above works for me...thanks...
Comment #13
johannesdr commentedThe 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.
Comment #14
johannesdr commentedComment #15
mfbOk here's my take on a fix: set colorspace to rgb and turn alpha off.
Comment #16
guillaumev commentedCan confirm that the last patch provided in this issue (comment #15) is working.
Comment #17
bzitzow commentedUPDATE: 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:
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:
PHP Version:
PdfPreview Version:
Comment #18
pbuyle commentedThe 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:
So
-alpha offshould 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.Comment #19
rbrownellI can confirm that this is working for me as well. I would suggest applying the patch in #17.
Comment #20
juanjo_vlc commentedComment #23
nbuonin commentedDid 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:
in the module file.
Comment #24
rbrownellReopening based on #23.
Comment #25
rbrownellComment #26
pbuyle commentedThe 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.
Comment #27
pbuyle commentedComment #28
guillaumev commentedReopening 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...
Comment #29
guillaumev commentedHere 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...
Comment #30
pbuyle commentedThe documentation for the -alpha option does define both
removeandoff, but the difference is unclear to me. Also, there is not indication thatremoveoroffbehaviors 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
removedoes always provide the wanted effect, I don't see any reason not to use it.Comment #31
guillaumev commentedOn my side, -alpha off generates black thumbnails, while -alpha remove does not...
Comment #32
pbuyle commented@guillaumev Yes, obviously, otherwise you would not have submitted your patch. But does it work on other peoples' installs where
-alpha offdoes 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).Comment #33
a.ross commentedI 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 removeworks for me.Comment #34
joran lafleuriel commentedI had this problem...
Solved using ImageMagick 6.7.7-10 and this module in version 7.x-2.x-dev
Thanks for your help...
Comment #35
pbuyle commentedComment #36
pbuyle commentedThis issue either need confirmation that
-alpha removeworks fine most users or a authoritative source that says it should (some IM expert or developer).Comment #37
stijn.blomme commentedFor 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...
Comment #38
fhey commentedThere'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.
Comment #39
pbuyle commentedPlease inform us on what IM version you are running, and if you use the patch in #29 or not.
Comment #40
ultrabob commentedI 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.
Comment #41
ultrabob commentedI 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?
Comment #42
jrbHere's a patch for the solution in #40. This worked for me while the other solutions above did not.
Comment #43
ultrabob commentedThanks @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.
Comment #44
pbuyle commentedActually 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.
Comment #46
pbuyle commented