ghostscript how to retain pdf size while converting pdf to grayscale - ghostscript

I am using Ghostscript 8.x to convert a pdf to grayscale.I am using the following command:
gs -dNOPAUSE -dBATCH -q -sOutputFile=- -sDEVICE=psgray 2016-12-15-165043474.pdf | ps2pdf - output.pdf
This successfully converts my pdf to grayscale but I lose the original pdfsize. The resulting pdf has a lot of whitespace looks like A4 size.
My input pdf has fixed width of 3cm (height may vary).I want the output pdf to be of same size.
Please suggest.

Don't use the psgray device! This is seriously deprecated and has been removed totally in recent versions of Ghostscript. By using this you are converting the PDF to PostScript and then converting it back to PDF. More steps than you need (with each conversion potentially introducing problems), and that's where you are getting the default media size from.
Simply use the pdfwrite device to do all the work, but you will need a reasonably recent version of Ghostscript to do it. Possibly more recent than the old version you are obviously using currently.
gs -sDEVICE=pdfwrite -sColorConversionStrategy=DeviceGray -sOutputFile=out.pdf input.pdf

Related

Convert RGB pdf to CMYK preserve pdf

I am using ghostscript 9.25 windows.
I am trying to convert RGB pdf to CMYK preserve pdf using following command:
gswin32c.exe
-dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite -sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -sOutputFile=out.pdf input.pdf
input.pdf file here
https://www.dropbox.com/s/8jfnov526nhb9m9/blank.pdf?dl=0
output.pdf file here
https://www.dropbox.com/s/ftrmm32mmixaxqh/out.pdf?dl=0
but my output becomes light compared adobe output, expected result is it should be dark when i do in adobe CMYK preserve option, i am getting little dark compared to ghostscript output. Am I doing anything wrong?
Should I use any icc profile?
Thanks
You say you are using ImageMagick, yet you give a Ghostscript command line....
I presume that when you say CMYL you mean CMYK.
There is nothing immediately obviously wrong with your command line, but you have given no example file, nor any reason why you expect the result to be 'dark'.
If you want to control the conversion then you will need to supply at least one and possibly up to 4 ICC profiles. You will certainly need a CIE->CMYK Output profile, and you might like to supply ICC profiles for Gray->CIE, RGB->CIE and CMYK->CIE as well, in order to override the default ones Ghostscript is using.
[EDIT]
The problem is nothing to do with colour conversion. Your original file contains nothing except a very large image, which is compressed with the Flate filter (lossless). It looks like this:
You've turned off auto filtering, but you haven't told Ghostscript which compression filter to use for images, so it sticks with the default, which is JPEG (DCT). The image now looks like this:
For the nature of your original image, JPEG (lossy) compression is an outstandingly bad choice. The output image compresses less well, and it loses fidelity. You should change to using Flate compression instead of JPEG for images of this kind.
By the way, the image in your original PDF file was defined in CMYK space already.

Ghostscript doesn't keep landscape page orientation

Example:
Create a one sheet landscape PDF from Excel. Open it using Adobe Reader, where it opens as landscape. Print it using Ghostscript. Open printout using Adobe Reader, and it should still be landscape, but it isn't.
This issues happens whether or not the source is a PDF or Excel itself. I use a PDF source in the example because I figured that it would be easier to illustrate the issue.
I know it can probably be forced to be printed using landscape. What I don't understand is why Ghostscript is not using the orientation of the original document. How can I get Ghostscript to keep the orientation of the original document?
PDFWrite config file:
-I"C:\Program Files\gs\gs9.22\lib";"C:\Program Files\gs\fonts"
-sDEVICE=pdfwrite
-r300
-dNOPAUSE
-dSAFER
-sPAPERSIZE=A4
-dCompatibilityLevel=1.4
-dPDFSETTINGS=/prepress
Redmon config:
#"C:\Program Files\gs\pdfwrite.txt" -sOutputFile="%1" -c 30000000 setvmthreshold .setpdfwrite -f -
Printer driver:
Ghostscript PDF
You've left the AutoRotatePages switch at the default. The default is to rotate pages so that the majority of text is horizontal, if you don't want that, then set -dAutoRotatePages=/None.
Since you apparently already have a PDF file ? Why use Ghostscript to create another one ? You stand the chance of losing stuff that way.
Since the input is PDF there is no point in the -sPAPERSIZE switch, since that only sets the dfefault media, which is immediately overridden by the PDF interpreter.
If I were you I wouldn't set -dPDFSETTINGS at all, nor -r300 (the resolution setting is mostly pointless for pdfwrite)

GhostScript PDF to PostScript

I have to convert pdf files (created with jasperreports) to postscript.
I'm using ghostscript (Version 9.19) to make the conversion.
The commmand i'm using is:
gswin64c -dNOPAUSE -dBATCH -sDEVICE=ps2write -sOutputFile=file.ps file.pdf
The conversion is done without problem, but when i open the postscript file generated (using GSview 5.0), the top margin is crop by 2-3 cm, and some information to print is lost.
I have changed the device from ps2write to eps2write, used the property -g<width>x<height> with the page size in pixels, but the problem persist.
The file is to be printed in a preformated paper, so i can not use the postscript generated to print.
Can someone help?
Thanks
Its not possible to say with great certainty, but it sounds like the PDF mediaBox is larger than the media you have specified to GSView.
You can try using the -dDEVICEWIDTHPOINTS and -dDEVICEHEIGHTPOINTS along with -dFIXEDMEDIA and -dPDFFitPage, that should allow you to set up a specific media size, override the size in the PDF file and scale the result to fit the specified size.
Perhaps you could post an example PDF file, without that its very hard to comment sensibly.

Is it possible to not render text using the Ghostscript command line?

I want to do some image processing in MATLAB. However, my source files are PDF's, so I'm converting them to images using the Ghostscript command line.
gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r300 -sOutputFile=test%d.png test.pdf
However, there is some text overlaying the images I would like to process. Is it possible to disable the rendering of fonts? Ugly workarounds are acceptable too, like forcing gs to use an empty font (does such a thing exist?) or something.
If you are using a recent version of Ghostscript (and if you aren't, then upgrade) you can use:
-dFILTERTEXT
See Ghostscript documentation: Use.htm => "Other parameters".

ghostscript option to make a pdf with flattened images

Is there an option to print a pdf in ghostscript as images?
I can use:
gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r300 -sOutputFile=p%03d.png my.pdf
Then use imagemagick to make a pdf out of them with:
convert *.png new.pdf
PDF printers seem to have an option that does the same thing that is a checkbox that says "print as image". I could not find anything in the ghostscript docs that sounded like that was an option. There may be a term for it that I just don't know to look for.
It is kind of hard to explain why you would want to take a pdf document that is text and turn it into a document of images of text that is 4 times the size of the original but that is what I want to do.
Currently the only way to do that would be to start with a PDF which contains transparency operations, and select a CompatibilityLevel of 1.3 or less.
I have an idea to implement this feature, but I have not had time to work on it.
You can do it as a 2-pass approach using Ghostscript to render an image, then using the view* scripts to read the image back into Ghostscript and produce a PDF. No better than using convert of course.

Resources