Ghostscript 9.23 flattening of PDF annotations - ghostscript

Prior to Ghostscript 9.21, annotations could be flattened into the document using the following code:
c:\gs\gs9.23\bin\gswin64c -dSAFER -dDOPDFMARKS -dBATCH -dNOPAUSE
-sDEVICE=pdfwrite -dCompatibilityLevel=1.3
-sFONTPATH=c:/gs/fonts;%windir%/fonts;. -sPDFPassword=
-dPDFSETTINGS=/prepress -dPassThroughJPEGImages=true
-sOutputFile=file_noPW.pdf file.pdf
This would make an annotations permanent to the file, much like Acrobat's preflight option. As of version 9.21, this no longer happens.
Is there a way to flatten the annotations? I use this to make sure pdf's that are imported using latex have all the annotations present, as pdflatex and xelatex do not import annotations.

Its always worth reading the documentation. If you look here section 7.4, PDF file output, you will see the PreserveAnnots switch described.

Related

Ghostscript : how to set the %d Command to a specific value

i use GS with this command line
gswin32c.exe -sDEVICE=tiffg4 -dSAFER -dBATCH -dNOPROMPT -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dNOPAUSE -r600 -g6120x7920 -dPrinted=false -sOutputFile="C:\test\test_%d.tif c:\test\xyz.pdf
That works fine, but it always start with the "1" value, so the output is "test_1.tif".
How do i need to modify the comanndline, to force GS to start with another value, e.g. 7.
so GS convert the multipage PDF "xyz.pdf" to singlepage tiff like "test_7.tif, test_8.tif" and so on.
background:
i have several pdf and the naming of the pdf is allways different. but i want one ongoing name for the tif file which allways begin with "test" in this case and the incremental value for each pdf page.
also my first pdf extract have to be the first tiff files, then the 2nd PDF file extract have to append and so on.
hopefully its a litte bit clear what i need ;)
BR Ralf
The %d convention is simply a C printout format and as mentioned in KenS comment cannot be "offset"
see https://www.ghostscript.com/doc/current/Use.htm#One_page_per_file
but there are workarounds mentioned in
How to set printf %d offset for Ghostscript?
However they are perhaps not the most efficient, but may help in some cases, just beware the comments below them.
The simplest way I will alter the range is to apply a preset such as
1%d to produce 10 11 12..., or
2%02d = 201 202 ...
Again you are limited to the first page will always be ###1 and if you need it to start at ###5 then you need to rename all files post production. That can often be easily scripted depending how well you prepare the desired names.

Accessing files from Ghostscript commands with -dSAFER

I'm trying to write a number of gs commands for server-side use. The user-provided PDF/JPEG files, I have to work with cannot be assumed to be safe (broken or even malicious files could be provided). Therefore, I'm trying to write all of my Ghostscript commands with -dSAFER, to guarantee at least a basic level of security.
Unfortunately, -dSAFER appears to be incompatible with certain gs commands. Take for example the following command:
# count number of pages in PDF
gs -dQUIET -dBATCH -dNOPAUSE -dNOSAFER -dNODISPLAY \
-c "(input.pdf) (r) file runpdfbegin pdfpagecount = quit"
How would you re-write this command with -dSAFER? The command fails if I just add -dSAFER, because gs can't read the file input.pdf (which is what I expect). How do I tell gs that is permitted to read input.pdf, but nothing else? Maybe there's a way to permit reading of files only from certain directories?
Here's a second example command:
# convert JPEG to single-page PDF
gs -dQUIET -dBATCH -dNOPAUSE -dNOSAFER \
-sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSettings=/printer \
-sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray \
-sOutputFile=output.pdf \
viewjpeg.ps \
-c "(input.jpg) << /PageSize 2 index viewJPEGgetsize 2 array astore >> setpagedevice viewJPEG"
This command has exactly the same problem as the first one. How do I re-write this with -dSAFER?
Please include a link to the relevant documentation in your answer if you think that -dSAFER is really not needed for my commands.
You can add directories to the search list using -I, IIRC any such directory is permitted to be read. For the current directory you can also use -P- and -P.
See Use.htm in the ghostpdl/doc directory.
However, even if adding the current directory allows you to read the file, this will soon stop working with -dSAFER anyway. There's an ongoing programme to 'harden' the interpreter when -dSAFER is set by hiding/remobing any non-standard operators especially if there's any possibility they could be misused.
Your proposed usage is, simply, incompatible with -dSAFER. The commands you are using will almost certainly be specifically prohibited because they are inherently unsafe. Essentially by messing about inside PDF files like that, your PostScript program is unsafe.
Of course, you know that your PostScript program is safe, and since you are not executing any part of the PDF, the PDF is also safe. The PDF would only be unsafe if you attempted to actually execute the contents, which your program doesn't do. It simply opens the file reads the Pages tree, and tells you the value associated with the Count key.
So you don't need to set -dSAFER in this particular example anyway. However any such program which did execute the PDF content (eg by rendering any part of it, or sending it to the pdfwrite device) would not be safe.
Your second example also doesn't need SAFER, since a JPEG can't contain code to access the disk.

Missing ICC Data in ghostscript

For some documents that I need to convert into images (jpeg) through the (templated) command gs -dNOPAUSE -sDEVICE=jpeg -dBATCH -r133 -sOutputFile=/my/path/%d.jpg file, sometimes I get the following error :
./base/gdevp14.c:3632: pdf14_update_device_color_procs_push_c(): Missing ICC data
the process then launches a signal 11 (which is a segfault I believe). I'm using Ghostscript 9.05 on the machines where this crashes, but on another machine equipped with Ghostscript 9.10, it runs just fine, for (exactly) the same document.
I thought that I could be missing something, but what could it be ?
thanks !
Update to something a little more recent. The current version is 9.16. If you continue to experience problems then file a bug report. I would guess that your bug has been fixed somewhere between 9.05 and 9.10.....
By overriding the ICC settings, it seems that it is doing fine : gs -dNOPAUSE -sDEVICE=jpeg -dOverrideICC -dBATCH -r133 -sOutputFile=/my/path/%d.jpg file.
A PDF that was failing, once exported to a new PDF worked just fine, so I guess there was something amiss in the original PDF, that ghostscript didn't like very much. Overriding the ICC settings with the default ones on the current machine seems to do the trick.

Ghostscript and pxlcolor: how to generate additional PJL entries in generated PCL

I have to set the Outbin in a PCL file that is generated via Ghostscript with the device=pxlcolor. I'm looking araund but didn't find the necessary parameter.
Can someone pls advice?
My commandline on windows is:
C:/Programme/gs/gs9.06/bin/gswin32c.exe -dBATCH -sDEVICE=pxlcolor -dFIXEDMEDIA -dMediaPosition=4 -dDuplex=true -sOutputFile=output.pcl -f Input.pdf
That produces a pcl file that takes it sheet(s) from the correct input tray/PaperSource.
There is no provision for this, you will have to add these manually after the file is generated.

discoloration of images created by ghostscript

I m converting a pdf into image using ghostscript
It almost produces good images but for one pdf the created image is having a white strip
however this strip is not present in the original pdf.
script which i hv used is below
gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=2 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile=fileName.gif -dJPEGQ=100 -r300 -q pdfName.pdf -c quit
Here the created image http://www.pictureshack.us/images/65486_1.gif
i am using Ghostscript 8.70 and the issue is fixed for the latest update i.e 9.06, but i cant upgrade to this...moreover found the issue its due -r300, so when i specify resolution of 100 then it works fine for this pdf but not for many other pdf...
You don't say what version of Ghostscript you are using, and you haven't provided the original PDF file, so I can't test it on current code. However, this 'looks like' a transparency bug, and may well be fixed in recent versions of Ghostscript.
I would suggest you upgrade to 9.06 if you haven't already and try that. If the problem persists please open a bug report at http://bugs.ghostscript.com Please attach the original PDF file and the command line you are using.

Resources