Resize A4 PDF to SRA4 and center content - ghostscript

I'd like to be able to resize an A4 PDF to the slightly bigger SRA4 format, used by printing professionals. The content shouldn't be resized, but only centered. In other words, I'd like to add margins to my A4 document.
I managed more or less to do that with this Ghostscript (9.05) call:
gs -dPDFSETTINGS=/default -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite
-sOutputFile=out.pdf -dDEVICEWIDTHPOINTS=637 -dDEVICEHEIGHTPOINTS=907
-dFIXEDMEDIA in.pdf
The problem is that the A4 content is not centered in the SRA4 page, but is placed at the bottom left corner.
Any idea how to center the content?

I found a solution here, by using -c "<> setpagedevice", like so:
gs -dPDFSETTINGS=/default -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite
-sOutputFile=out.pdf -dDEVICEWIDTHPOINTS=637 -dDEVICEHEIGHTPOINTS=907
-dFIXEDMEDIA -c "<</PageOffset [21 32]>> setpagedevice" -f in.pdf
Content is perfectly centered in the SRA4 document.

Related

issue of converting .eps file to .png file with gs

I tried to use the command to convert the .eps file to .png file
gs -dSAFER -dEPSCrop -r300 -sDEVICE=pngalpha -o test.png test.eps
But the figure has the wrong bbox, just like viewing it in the letter size in gv.
If I used
gs -dEPSCrop -r300 -sDEVICE=bbox -o test.png test.eps
gs is able to find the correct bbox as (well close, the exact BoudingBox in the .eps file is "21 54 1101 558"):
%%BoundingBox: 35 62 1083 554
%%HiResBoundingBox: 35.040001 62.400002 1082.640041 553.440021
Is there something I miss to convert the .eps file to .png file with the bbox? Thank you.

how to save GhostScript output

I am creating an image on the fly using ghostscript. How do I write the my_image.png to /home/pi/Desktop location. I can get the image to print to screen, but not save the output to a location.
gs -sDEVICE=pngalpha -sOutputFile=- -q -r100 -g600x100 - >my_image.png
I changed the OutputFile line to
gs -sDEVICE=pngalpha -sOutputFile=/home/pi/my_image.png -q -r100 -g600x100 -

ghostscript pdfwrite specify jpeg quality

I'm trying to concatenate multiple pdf files which basically are the pages of a photobook containing jpg images. For my output pdf file I wish to adjust the image resolution to 300 dpi and I want to keep the best quality. The commands I'm using are:
gswin64c.exe -dNOPAUSE -dBATCH ^-dDownsampleColorImages=true -dColorImageResolution=300 ^-dDownsampleGrayImages=true -dGrayImageResolution=300 ^-dDownsampleMonoImages=true -dMonoImageResolution=300 ^-sDEVICE=pdfwrite -dJPEGQ=100 -sOutputFile=out.pdf in1.pdf in2.pdf
However, it seems that -dJPEGQ=100 has no effect on the output. Changing this parameter leads to the same filesize and artifacts are visible in the images for all values. Running the command with the option -dPDFSETTINGS=/printer I get better results without artifacts, however this option should also result in 300 dpi. So what is the correct command to specify the quality of the jpg images in the output file?
The solution is adjusting the DCTEncode filter with follwing commands:
gswin64c.exe -sOutputFile=out.pdf -dNOPAUSE -dBATCH ^-sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -c "<< /ColorACSImageDict << /VSamples [ 1 1 1 1 ] /HSamples [ 1 1 1 1 ] /QFactor 0.08 /Blend 1 >> /ColorImageDownsampleType /Bicubic /ColorConversionStrategy /LeaveColorUnchanged >> setdistillerparams" -f in1.pdf
which lead to a compressed file with satisfactory quality for me and can be adjusted to each individual needs.
Edit:
the .setpdfwrite argument is deprecated for recent ghostscript releases (> 9.50), so I removed it in the answer

How to convert CMYK eps to CMYK jpeg with ghostscript?

Ghostscript changes colospace to RGB when converting CMYK eps to jpeg.The problem is to keep colorspace untouched during conversion. Thanx in advance.
Assuming you want to convert any RGB that may be in the EPS to CMYK, use this command on Windows:
gswin32c.exe ^
-o c:/path/to/output-cmyk.jpeg ^
-sDEVICE=jpegcmyk ^
input-rgb.eps
and this on Linux/Unix/MacOSX:
gs \
-o /path/to/output-cmyk.jpeg \
-sDEVICE=jpegcmyk \
input-rgb.eps
Note, by default Ghostscript uses a resolution of 72 dpi for the JPEG output. If you want to change that to, say, 300 dpi, then add -r300 to the commandline.

How can I draw mirrored text onto an image using ImageMagick?

I'm using ImageMagick (through the MiniMagick Ruby Gem) to crop an image, I also add an annotation to the top of it, but I want it to be mirrored.
I know ImageMagick has a 'flop' command which mirrors an image, but I'm not sure how to generate the text, flop it and compose it onto my image.
Here is how I currently draw the text (non mirrored). This code uses MiniMagick, but it translates pretty directly to actual ImageMagick commands.
image.combine_options do |c|
c.resize "1000"
c.font Rails.root.join('app/assets/fonts/Menlo.ttc').to_s
c.fill '#888888'
c.pointsize '16'
c.draw "text 0,0 '#{annotation}'"
end
How can I modify or replace this to draw mirrored text?
First, you should, for your Minimagick code, do yourself the 'translate pretty directly to actual ImageMagick commands', if you really are interested to get the biggest possible input. Not everyone here wants to learn Minimagick first, before he can chime in with his suggestions.
Second, you didn't tell which way you want your text mirrored: Left-right? Upside-down? Combined?
Third, try to replace these 2 lines
c.pointsize '16'
c.draw "text 0,0 '#{annotation}'"
by these 3 lines
c.pointsize '16'
c.draw "text 0,0 '#{annotation}'"
c.flop
or by these 3 lines
c.pointsize '16'
c.draw "text 0,0 '#{annotation}'"
c.flip
or by these 4 lines
c.pointsize '16'
c.draw "text 0,0 '#{annotation}'"
c.flip
c.flop
Now that Nathan admits he's 'familiar' with -flip and -flop for images only, let's show him how one can use that to nevertheless get flipped and flopped text annotations onto unchanged images.
See these two examples:
Left with with normal text, right with mirrored text (left-right mirrored - Nathan didn't respond to the question if he wanted this, or if he wanted top-bottom mirroring).
How was it done?
The left image's annotation was done with the following command:
convert \
logo: \
-fill white \
-undercolor '#00000080' \
\( \
-gravity west \
-pointsize 32 \
-annotate +0+40 \
" Dude! Listen, don't argue... " \
\) \
-scale 310x \
normal.png
So this result is the basis for the next step. In the next step we will modify this command to create a mirrored text annotation. But, since we cannot write mirrored text directly, we use one of the many tricks up in our sleeves:
Mirror the image first (first -flop).
Write normal text on mirrored image.
Mirror the resulting image one more time (second -flop). This way...
...main image is back to normal,
...annotation text is mirrored now.
And this leads to the left image's annotation command:
convert \
logo: \
-flop \
-fill white \
-undercolor '#00000080' \
\( \
-gravity east \
-pointsize 32 \
-annotate +0+40 \
" Dude! Listen, don't argue... " \
\) \
-flop \
-scale 310x \
mirrored.png
Easy, eh?
(Just notice how we also had to change the gravity from west to east so that the text appears on the same spot...)
It's Nathan's own job now to translate this algorithm into Minimagick's language.

Resources