(How) can I set an offset when converting PDF to PCL with Ghostscript? - pdf

I need to shift the whole page content of a PDF a certain distance down and to the left when converting it to PCL.
I've already found how to do it with PDF to PDF:
How can I shift page images in PDF files more to the left or to the right?
Is there somehing similar for the PCL conversion?
Now I convert with the following command:
gswin32c.exe \
-q \
-dNOPAUSE \
-dBATCH \
-sDEVICE=pxlmono \
-dDuplex=false \
-dTumble=false \
-sPAPERSIZE=a4 \
-dMediaPosition=4 \
-sOutputFile="d:\out.pcl" \
-f"d:\in.pdf" \
-c \
-quit
Is there a possibility to do it directly or do I need to shift the content in a PDF to PDF conversion first (like in the linked question) and then convert it to pcl in a second conversion step?

I don't think it would work with the direct conversion of PDF => PCL.
You should do it in 2 steps, then it works:
first shift the contents on the PDF pages,
convert the PDF with the shifted pages to PCL.
First command:
gs \
-sDEVICE=pdfwrite \
-o pdf-shifted-by-1-inch-to-left-2-inches-to-top.pdf \
-g8420x5950 \
-c "<</PageOffset [-72 144]>> setpagedevice" \
-f input.pdf
Second command:
gs \
-sDEVICE=pxlmono \
-o pcl-output.pcl \
-sPAPERSIZE=a4 \
pdf-shifted-by-1-inch-to-left-2-inches-to-top.pdf

Related

Ghostscript does not compress all PDF's

I am trying to compress PDFs using Ghostscript. I have a folder of around 5000 PDF files, all of varying sizes (5-1000 pages) and appearances. The gs command that I use successfully compresses 2700 of them. However, the remaining 2300, remain uncompressed and in many, cases have increased in size after applying the command. Below is my command which I call through a python script. Please tell me what I should add or remove to ensure that all PDF's are compressed.
gs \
-q \
-dNOPAUSE \
-dBATCH \
-dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dSubsetFonts=true \
-dUseFlateCompression=true \
-dOptimize=true \
-dProcessColorModel=/DeviceRGB \
-dDownsampleGrayImages=true \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution=150 \
-dAutoFilterGrayImages=false \
-dDownsampleMonoImages=true \
-dMonoImageDownsampleType=/Bicubic \
-dCompressPages=true \
-dMonoImageResolution=150 \
-dDownsampleColorImages=true \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution=150 \
-dAutoFilterColorImages=false \
-dPDFSETTINGS= /screen \
-sOutputFile = output.pdf
input.pdf
Generally PDF compression is a difficult problem. Some documents can be very bloated and capable of being reduced greatly in size. Other documents are very well compressed already. Some documents aren't necessarily bloated and are already a good size based on their content.
Not all PDF software is created equally, some can work great for particular types of PDFs and others not so much. Unfortunately there's not a one size fits all answer for why a PDF can't be reduced in size further.

Convert PDF from RGB to CMYK with Ghostscript

I'm using the following command to convert the color profile of a PDF from RGB to CMYK:
gs -dSAFER \
-dBATCH \
-dNOPAUSE \
-dNOCACHE \
-dPDFX \
-sDEVICE=pdfwrite
-sColorConversionStrategy=CMYK
-sOutputFile=cmyk.pdf rgb.pdf
The PDF of the test is for demo purposes to check the support of what I need.
On my local machine the script produces this as result:
.
But when I try to convert this inside a Docker container the same script produces this:
Dockerfile:
FROM ubuntu
RUN apt-get install -y ghostscript`

Ghoshscript total coverage "incorrect"

I am using Ghostscript (version 9.2) Linux version to convert PS to PDF file. Below is the script
#!/bin/ksh
/home/ghostscript/ps2pdf \
-dBATCH -dNOPAUSE \
-dEmbedAllFonts=true -dAutoRotatePages=/None \
-sDEVICE=pdfwrite \
-r1440 \
-dOverrideICC=true \
-dUsePDFX3Profile=1 \
-sProcessColorModel=DeviceCMYK \
-sColorConversionStrategy=CMYK \
-sICCProfilesDir=/home/ghostscript/ \
-sDefaultRGBProfile=AdobeRGB1998.icc \
-sOutputICCProfile=ISOnewspaper26v4.icc \
-sImageICCProfile=ISOnewspaper26v4.icc \
-sFONTPATH=/opt/igenfonts \
-dDEVICEWIDTHPOINTS=779.76 -dDEVICEHEIGHTPOINTS=1035.36 \
-sOutputFile=$1.pdf \
$1.PS
ISOnewspaper26v4.icc is a Total Coverage 240 ICC profile from http://www.color.org. PDF is converted successfully. But in Adobe Acrobat 8.0 profressional, total coverage of 240 shows color saturation exceeding 240.
Could anyone with the experience help?
Thanks
This isn't a programming question and so is off-topic for Stack Overflow. If you think you've found a bug your best solution is to open a bug report.
You are using a slightly out of date version of Ghostscript and a very out of date version of Acrobat. You haven't explained how you are using Acrobat to determine 'total coverage' or indeed what you mean by 'total coverage'.

Ghostscript: How to convert EPS to PDF with the same page size

I want to convert a EPS figure to a PDF figure with the same width and height.
The following command:
gswin32 -dSAFER -dNOPLATFONTS -dNOPAUSE -dBATCH \
-sDEVICE=pdfwrite -sPAPERSIZE=letter -dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/printer -dCompatibilityLevel=1.4 -dMaxSubsetPct=100 \
-dSubsetFonts=true -dEmbedAllFonts=true -sOutputFile="test.pdf" \
-f "test.eps"
only produces a PDF file with the page size of a letter.
Any help would be much appreciated.
Here is the test EPS file: https://dl.dropboxusercontent.com/u/45318932/test.eps
EPS files cannot contain a media size request. In the absence of any media size request Ghostscript uses the default.
However.....
From the documentation:
http://www.ghostscript.com/doc/9.15/Use.htm#EPS_parameters
-dEPSCrop :
Crop an EPS file to the bounding box. This is useful when converting an EPS file to a bitmap.
To make KenS' answer more explicit, using the test.eps sample file you linked to... the following command will suffice to do what you want:
gswin32 \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/printer \
-dEPSCrop \
-o test.pdf \
test.eps
The -o test.pdf is (for not too ancient versions of Ghostscript!) shorthand for -dNOPAUSE -dBATCH -sOutputFile=test.pdf.
Your test.eps uses a font named /SHZENL+Tahoma_00. Ghostscript will automatically embed this font, and it will be a subset by default (the prefix SHZENL may change in the PDF, though).
Here is a screenshot from the page the command from your question created. That page is 612 x 792 pts (letter size):
Here is the screenshot from the page the command given in my answer created. Its page size is 360 x 216 pts:

How do you convert PDFs to PNGs with ghostscript?

I'm usually able to use ghostscript to convert PDFs to PNGs with the command:
gs \
-q \
-dNOPAUSE \
-dBATCH \
-sDEVICE=pnggray \
-g2550x3300 \
-dPDFFitPage \
-sOutputFile=output.png \
input.pdf
But this doesn't work for some PDF files. For example, the command above converts this PDF file to this PNG -- the original PDF is just a small image in the lower left corner of the PNG, instead of filling the entire page.
Is there a more robust way to convert PDFs to PNGs with ghostscript, or perhaps some other command line tool?
Note: If I generate a new PDF file from the problematic one via "print -> save as pdf" in Preview on OS X, then the command works fine.
Just use ImageMagick's convert.
convert foo.pdf foo.png
You can have more precise control over the page number with format strings, e.g.:
convert foo.pdf "foo-%03d.png"
And of course there are the myriad other ImageMagick options, but the basic command above is all you need most of the time.
Edit: about your "bad.pdf":
The short answer is to add the option -dUseCropBox to your gs command or -define pdf:use-cropbox=true to the convert command.
gs \
-q \
-dNOPAUSE \
-dBATCH \
-sDEVICE=pnggray \
-g2550x3300 \
-dPDFFitPage \
-dUseCropBox \
-sOutputFile=output.png \
input.pdf
or
convert \
-density 300 \
-define pdf:use-cropbox=true \
foo.pdf \
foo.png
If you view the PDF in a text editor you can see that a CropBox and a MediaBox are specified, and that the CropBox is much smaller.