I am using ghost script to convert my postscript file to PDF/A.
Below is the command to generate the PDF/A file
gs -q -dPDFA=2 -dBATCH -dNOPAUSE -dNOOUTERSAV -sProcessColorModel=DeviceGray \
-sDEVICE=pdfwrite -dFastWebView=true -dPSFitPage -dDOPDFMARKS \
-sOutputFile=NewPDF.pdf PDFA_def.ps abc.ps pdfmarks
The PDFMarks file looks like:
[ /Title (Document title)
/DOCINFO pdfmark
[ /Page 1 /Title (COVER PAGE) /OUT pdfmark
[ /PageMode /UseOutlines /View [/FitPage]/Page 1 /DOCVIEW pdfmark
[ {Catalog} <</ViewerPreferences<</DisplayDocTitle true>>/PageLayout/OneColumn>> /PUT pdfmark
Still when the PDF/A generates, the Magnification Property is set to Default Only. I need to set it to FitPage.
I tried using /Fit,/FitB,/FitH etc but none of them seems to work.
There is no 'FitPage', using this option causes Acrobat to raise a warning when opening the resulting PDF file (Ghostscript does not attempt to fully validate pdfmark operands)
Fit uses the CropBox to determine the scaling, and you haven't specified a CropBox so unless your other PostScript does so, then there won't be one. Similar problems may exist with the other options you have mentioned.
The PUT pdfmark is also incorrect, the '<>' should be '<<'.
You do not need (and should not use) -dNOOUTERSAVE.
I wouldn't use -dFastWSebView unless you have a very good reason, it simply takes longer to process and produces a larger file for no really good reason.
PSFitPage won't do anything useful if you don't specify a fixed media size (which you haven't).
You don't need -dDOPDFMARKS, that's for PDF input only (and in any event defaults to true).
If you want the PDF/A file to be Gray you should set -sColorConversionStrategy=DeviceGray rather than meddling with the ProcessColorModel.
I used a simple empty PostScript program and added:
[ /PageMode /UseOutlines /View [/FitV] /Page 1 /DOCVIEW pdfmark
The resulting PDF file displayed as expected.
Related
well I've given up. I don't think I understand how GS works... as far as I understand GS replaces all fonts that are not embedded and should not touch already embedded ones? why is it replacing them? I have a pdf file that contains 2 embedded fonts and 1 not embedded (ArialMT).
I'm using command:
"gswin64c.exe -I "C:/Program Files/gs/gs9.56.1/Resource/Init" -sFONTMAP="Fontmap.GS" -dNOSAFER -dPDFACompatibilityPolicy=1 -sColorConversionStrategy=LeaveColorUnchanged -dBATCH -dNOPAUSE -sDEVICE="pdfwrite" -dAutoRotatePages=/None -dPDFA=3 -sOutputFile="pdfa.pdf" "original.pdf"
all I get with this command is this error:
GPL Ghostscript 9.56.1: Actual TT subtable offset xxxxx differs from one in the TT header yyyy. (multiple ones like this)
The following errors were encountered at least once while processing this file:
error executing PDF token
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> StreamServe Communication Server 16.6.1 GA Build 319 (64 bit) <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
the output is a pdf without ANY fonts...
is there any way to force GS not to replace font if it wasn't found on the system?
why is it replacing ArialMT with NimbusSans-Regular even though I have declared a specific path to ArialMT in my FontMap.GS file?
I'd rather not share this pdf file as it contains sensitive customer data.
(osadzony podzestaw=embedded subset)
Ghost Script substitution will require embeddable fonts on windows those are usually stored in C:\Windows\Fonts
Thus if font substitution was simple (without look-up) your command could be simplified
gswin64c.exe -sFONTPATH="C:\Windows\Fonts" -dNOSAFER -sDEVICE=pdfwrite -dNEWPDF=false -dPDFA=3 -dPDFACompatibilityPolicy=1 -sColorConversionStrategy=LeaveColorUnchanged -dAutoRotatePages=/None -o"pdfa.pdf" "original.pdf"
you need to add -dNEWPDF=false Since to include additional mapping you add -I "C:/Program Files/gs/gs9.56.1/Resource/Init" -sFONTMAP=Fontmap.gs
Thus the following should be a startpoint
gswin64c.exe -sFONTPATH="C:\Windows\Fonts" -I "C:/Program Files/gs/gs9.56.1/Resource/Init" -sFONTMAP=Fontmap.gs -dNOSAFER -sDEVICE=pdfwrite -dNEWPDF=false -dPDFA=3 -dPDFACompatibilityPolicy=1 -sColorConversionStrategy=LeaveColorUnchanged -dAutoRotatePages=/None -o"pdfa.pdf" "original.pdf"
It will not remove warnings using a PDF file from the same developer, the difference was now there is no mention of Nimbus, but the substitutions should be better/fuller as the warning messages verified the fonts were eventually applied from windows
Note the file is smaller although the fonts are embedded, and in side by side comparison they look the same.
GPL Ghostscript 9.56.1: PDFA doesn't allow images with Interpolate true.
and
The following errors were encountered at least once while processing this file:
missing white space after number
error executing PDF token
**** This file had errors that were repaired or ignored.
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
from their report.
If I save the file from Acrobat the file size drops but the same issues reside
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.
I convert a PostScript file to PDF by Ghostscript. I have a problem embedding/installing Type 1 fonts.
For installing Type 1 Font, I can add the PFA file path to Ghostscript Fontmap, which should be in /usr/share/ghostscript/version/FONTMAP, but I have no such file in /usr/share/ghostscript/9.50` or similar folders on Ubuntu 20.04.
How can I include the font file directly within the script:
Instead of
/Times-Bold findfont 10 scalefont setfont
something like
(/home/font.pfa) 10 scalefont setfont
Does PostScript/Ghostscript use AFM file data or read the glyph widths or just from the glyph structure provided in PFA file?
The fonts and Fontmap file can be placed in several directories. Here is a typical search path:
/usr/share/ghostscript/9.52/Resource/Init/Fontmap
/usr/share/ghostscript/9.52/lib/Fontmap
/usr/share/ghostscript/9.52/Resource/Font/Fontmap
/usr/share/ghostscript/fonts/Fontmap
/usr/share/fonts/Type1/Fontmap
/usr/share/fonts/Fontmap
I sometimes use fonts that are not installed in the search path just in the current working directory. I use the gs -P and either of these work:
(font.pfa) 12 selectfont
/font.pfa 12 selectfont
The search path can also be modified by adding the directories to the GS_FONTPATH or GS_LIB environment variables.
The AFM file is not mandatory and the metrics can be obtained from the font alone. Some programs use the AFM file instead of the actual fonts and so they are needed for those programs.
You do, you just have the path/name slightly wrong:
/usr/share/ghostscript/9.50/Resource/Init/Fontmap.GS
You can also use your own custom fontmap using a command line parameter: "-sFONTMAP=/path/to/custom/fontmap" (best to copy the system one, and add your customisations to the copy)
You can't, not like that, anyway - that's not how Postscript works. Postscript always references fonts by name (not by file/path), so whilst there ways to read the font file(s), you still need to know the font name(s) in order to scale and set the font(s).
Ghostscript does not use AFM files, it gets the metrics from the fonts and glyph outlines.
Hope that helps some....
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.
I have a PDF file that I need to change the orientation from landscape to portrait, using this command GS. The Orientation switch it is not changing the orientation. Is there a extra setting I need to add?
::Path to your Ghostscript EXE
set GSC="C:\Program Files (x86)\gs\gs9.16\bin\gswin32.exe"
%GSC% -sDEVICE=pdfwrite -sOutputFile="D:\TRANS\PDF\2_ZNOR_00000000000000000070.pdf" -dNOPAUSE -dEPSCrop -c "<</Orientation 3>> setpagedevice" -f "D:\TRANS\PDF\1_ZNOR_00000000000000000070.pdf"
It worked for me
List<string> switches = new List<string>
{
"-empty",
"-dQUIET",
"-dSAFER",
"-dBATCH",
"-dNOPAUSE",
"-dNOPROMPT",
"-dDEVICEHEIGHTPOINTS=612",
"-dDEVICEWIDTHPOINTS=792",
"-dFIXEDMEDIA",
"-dFitPage",
"-sDEVICE=pdfwrite",
"-dAutoRotatePages=/None",
"-sOutputFile=" + resultFilePath,
"-c",
"<</Orientation 1>> setpagedevice",
"-f"
};
switches.Add(this.FilePath);
Most of the switches you are using there will have no effect at all, and you haven't done anything to rotate the PDF file either.
The first thing to note is that Ghostscript is really the wrong tool for this job, it doesn't modify PDF files, it generates completely new ones. The process is described here
The -dEPSCrop switch only has an effect if the input is an EPS file, otherwise it does nothing.
The operands to setpagedevice are incorrect, you are passing an empty hex string <> when setpagedevice expects a dictionary <<>>. Even then an empty dictionary will do nothing.
I expect you intended to put <</Orientation 1>> setpagedevice but actually that's only a media matching request. In order to get Ghostscript to create a rotated PDF file you would have to alter the media size, set it to fixed, and set -dFitPage. You would also have to set the AutoRotatePages switch to None in order to prevent the automatic reorientation of the output so that the text is horizontal.
Since you haven't supplied the PDF file to look at, nor stated its media size, I can only guess at the sizes. However, something like
gswin32c.exe -sDEVICE=pdfwrite -sOutputFile=new.pdf -dDEVICEHEIGHTPOINTS=612 -dDEVICEWIDTHPOINTS=792 -dFitPage -dAutoRotatePages=/None input.pdf