Yes, you'll have to convert each PDF page into a single JPG file (Ghostscript can do that).
Then stitch together the resulting JPG files using another program (ImageMagick or GraphicsMagic can do that using their montage
sub-commands).
I'm not aware of any software which can do that in one go.
PDF-to-JPG conversion (with Ghostscript):
You'll want to make sure that you get the best possible result. So make sure you tweak the commandline options so they work for you. I'd start with this:
gswin32c.exe ^
-dBATCH ^
-dNOPAUSE ^
-dSAFER ^
-sDEVICE=jpeg ^
-dJPEGQ=95 ^
-r600x600 ^
-sOutputFile=c:/path/to/jpeg-dir/pdffile-%03d.jpeg ^
c:/path/to/pdffile.pdf
This will create JPGs called pdffile-001.jpeg, pdffile-002.jpg etc. The parameter *-dJPEGQ=95" sets "JPEG Quality" to 95%. It uses a resolution of "600x600 dpi". You may need to additionally control the pagesize of the resulting JPGs in case your Ghostscript's default doesn't fit your needs:
gswin32c.exe ^
-dBATCH ^
-dNOPAUSE ^
-dSAFER ^
-sDEVICE=jpeg ^
-dJPEGQ=95 ^
-r600x600 ^
-dPDFFitPage ^
-dFIXEDMEDIA ^
-dDEVICEWIDTHPOINTS=800 ^
-dDEVICEHEIGHTPOINTS=600 ^
-sOutputFile=c:/path/to/jpeg-dir/pdffile-%03d.jpeg ^
c:/path/to/pdffile.pdf
or
gswin32c.exe ^
-dBATCH ^
-dNOPAUSE ^
-dSAFER ^
-sDEVICE=jpeg ^
-dJPEGQ=95 ^
-r600x600 ^
-dPDFFitPage ^
-dFIXEDMEDIA ^
-sDEFAULTPAPERSIZE=a4 ^
-sOutputFile=c:/path/to/jpeg-dir/pdffile-%03d.jpeg ^
c:/path/to/pdffile.pdf
multiple-to-single-JPG-stitching with montage
(ImageMagick or GraphicsMagick):
The montage
command (used in this example is ImageMagick) allows you to control the tiling pattern. If you use e.g. -tile 4x3
you'd get this imposition layout:
1 2 3 4
5 6 7 8
9 10 11 12
You could use this command to stitch together 12 individual JPGs into one:
montage ^
-border 0 ^
-tile 4x3 ^
c:/path/to/jpeg-dir/pdffile-*.jpeg ^
c:/path/to/final.jpg
Of course, montage
has many dozen of additional parameters which allow you to determine background, spacing, offsets, decoration, labels, rotation, cropping, caption etc. for the input and the resulting JPG.
EDIT: (I had wanted to give this hint already in my original answer, but forgot.) montage
by default will use tile sizes of 120x120 pixels. If you want to keep the original page sizes for each tile, you have to add -geometry
to the commandline. Assuming you had A4 (=595x852 pt) pages in your PDF, and you want to keep this, but also add a spacing of 11pt to the horizontal and 22 pt to the vertical direction of the tiling (plus 4pt strong gray border/frame lines around each tile), do this:
montage ^
-border 4 ^
-tile 4x3 ^
-geometry 595x842+11+22 ^
c:/path/to/jpeg-dir/pdffile-*.jpeg ^
c:/path/to/final.jpg
EDIT 2: (Missed still another important hint.) If you do not want to lose the good image quality during the stitching/montage process, which your PDF-to-JPG conversion had created, then also add the -quality 100
parameter to your commandline like this:
montage ^
-border 4 ^
-tile 4x3 ^
-geometry 595x842+11+22 ^
-quality 100 ^
c:/path/to/jpeg-dir/pdffile-*.jpeg ^
c:/path/to/final.jpg
Ooookay. After plenty of googling, and reading for more of the ImageMagick manual than I actually care for, here's the answer. Given that you have a grayscale image called source.png
, here are my commands.
To make the make the black pixels transparent and keeps the white pixels as they are, run this command:
convert source.png -alpha copy -fx '#fff' result.png
To instead make the white pixels transparent while keeping the black as-is, use:
convert source.png -alpha copy -channel alpha -negate +channel -fx '#000' result.png
Let's explain that last command a bit more thoroughly:
convert
– Is the ImageMagic command (one of several)
source.png
– The greyscale source image.
-alpha copy
– Copy contents of the previous file into the alpha channel.
-channel alpha
– Specify that following operators only should affect the alpha channel.
-negate
– Invert the alpha channel (will, because of the previous -chanel alpha
not affect any other part of the image).
+channel
– Specify that following operators only should should affect the color channels, and no longer modify the alpha channel. (This is the default, and therefore we need not provide it in the first, simpler example.)
-fx '#000'
– Replace color channel contents with black pixels. (Because of +channel
the alpha channel will not be affected).
It is quite important to include that final -fx
option, otherwise all semi-transparent pixels in generated image will retain colors. (Since these pixels are half-transparent, it might not be obvious, but the end result is not what one expect.)
I found the list of ImageMagick options quite useful.
Best Answer
Software Requirements
The following software packages are available for both Windows and Linux systems, and are required for a complete, working solution:
General Steps
Here is how the solution works:
FONT
tags).The script generates images that are all the same width for source files containing lines that are all under 80 characters in length. Source files with lines over 80 characters long result in images as wide as necessary to retain the entire line.
Installation
Install the components into the following locations:
C:\Program Files\Vim
C:\Program Files\Vim\vim73\colors
C:\Program Files\wkhtml
C:\Program Files\ImageMagick
C:\Program Files\GnuWin32
Note: ImageMagick has a program called
convert.exe
, which cannot supersede the Windowsconvert
command. Because of this, the full path toconvert.exe
must be hard-coded in the batch file (as opposed to adding ImageMagick to thePATH
).Environment Variables
Set the PATH environment variable to:
Batch File
Run it using:
Create a batch file called
src2png.bat
by copying the following contents:Improvements and optimizations welcome.
Note: The latest version of wkhtmltoimage properly handles overriding the background colour. Thus the line to remove the CSS for background colours is no longer necessary, in theory.