I get the well known error if I try to include a file which is outside the known paths:
Error: /invalidfileaccess in --file--
I know that I could use -DNOSAFER but I want to avoid this, since this would allow malicious code to be executed.
Is there an option to tell gs "you are allowed to read any file, but you must not remove or delete any files"?
(Answer duplicated from gGt an output file with a count of PDF pages for each file with Ghostscript; both these SO issues popped up while I was looking for a solution myself and no dice. :'-( )
Note that since GhostScript 9.50 the SAFER mode has been turned ON by default, resulting in /invalidfileaccess errors like these, which would not go away by making sure all directory separators in thee path are / forward slashes, fiddling with the current path, etc.
This has cost me quite some hair and several hours today, but anno 2020 you'll need to do something like this:
Solution 1: Add source path to the accepted list using --permit-file-read=<path> to make the /invalidfileaccess go away
gs -q --permit-file-read=d:/ -dNODISPLAY -c "(d:/test.pdf) (r) file runpdfbegin pdfpagecount = quit"
i.e. make sure that the path to the PDF you're loading in the PostScript command/script is in the approved list a.k.a. permission list, using one of the --permit-file-xyz commandline arguments.
Solution 2: Quick hack with -I<path> to make the /invalidfileaccess go away
gs -q -Id:/ -dNODISPLAY -c "(d:/test.pdf) (r) file runpdfbegin pdfpagecount = quit"
i.e. make sure that the path to the PDF you're loading in the PostScript command/script is in the approved list a.k.a. permission list.
The -Id:/ in the example above is just a quick hack to make sure the source path of your path is on that list, given this bit from the official documentation:
Finally, paths supplied on the command line (such as those in -I, -sFONTPATH parameters) are added to the permitted reading list. Similarly, paths read during initialisation from Fontmap, cidfmap, and the platform specific font file enumeration (e.g. fontconfig on Unix systems) are automatically added to the permit read lists.
Note / Aside about expectations when using -I like that:
Another surprise for me was that
gs -q -I d:/ -dNODISPLAY -c "(test.pdf) (r) file runpdfbegin pdfpagecount = quit"
i.e. specifying the source path of the PDF in the -I include set and then omitting it in the PostScript command, still gave me an /invalidfileaccess so make sure to specify a proper absolute path for the PDF you're loading.
Notes
Applies to PostScript scripts, not the -f commandline
This problem only occurs with file loads inside -c PostScript commands, not with PDF/PS source files specified directly on the commandline using -f <file>.
How to check if this is your actual problem
Test 1: Try the same without the extra -I<path> or --permit-file-read=<path>: does the error return? If yes, then bingo!
When this commandline (note the missing -Id:/ or --permit-file-read=d:/ is the only change):
gs -q -dNODISPLAY -c "(d:/test.pdf) (r) file runpdfbegin pdfpagecount = quit"
gives you the "invalid file access" error as in:
Error: /invalidfileaccess in --file--
Operand stack:
(d:/test.pdf) (r)
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval--
Dictionary stack:
--dict:737/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)--
Current allocation mode is local
Last OS error: Permission denied
GPL Ghostscript 9.52: Unrecoverable error, exit code 1
than you quite probably have a -dSAFER problem which is fixed by the above addition of the PDF source directory to the accepted paths list (see also the GhostScript documentation at 'NOSAFER' and onwards including the section about --permit-file-read=pathlist et al.
Test 2: Does the problem go away when you run with -dNOSAFER? If yes, then bingo!
When this commandline:
gs -q -dNOSAFER -dNODISPLAY -c "(d:/test.pdf) (r) file runpdfbegin pdfpagecount = quit"
works okay, then that's a sure sign you need either of the solutions described above to get rid of the /invalidfileaccess error.
WARNING: Older answers you find on the Net probably won't work anymore
As I've looked at quite a few discussions, both here at SO and elsewhere, including various bugtrackers, nobody seems to mention this anywhere as all those pages/entries are from before GhostScript release 9.50 and 2019AD.
Quoting from the 9.50 change notes:
The file access control capability (enable with -dSAFER) has been completely rewritten, with a ground-up rethink of the design. For more details, see: SAFER.
It is important to note that -dSAFER now only enables the file access controls, and no longer applies restrictions to standard Postscript functionality (specifically, restrictions on setpagedevice). If your application relies on these Postscript restrictions, see OLDSAFER, and please get in touch, as we do plan to remove those Postscript restrictions unless we have reason not to.
IMPORTANT: File access controls are now enabled by default. In order to run Ghostscript without these controls, see NOSAFER
Important Note for Windows Users: See below under Incompatible Changes
The SAFER change is from 2019-09-30 (release 9.50)
No. You either use -dSAFER to prevent file access, or you don't (or use -dNOSAFER if your environment already sets -dSAFER).
No possibility of 'a little bit secure'.
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 typed the following command at a Windows 10 command prompt:
C:\hoge>"exiftool(-k).exe" "-FileName<CreateDate" -d "%Y-%m-%d_%H-%M-%S.%%e" C:\hoge
Warning: [minor] The ExtractEmbedded option may find more tags in the movie data - C:/hoge/C0001.MP4
1 directories scanned
1 image files updated
-- press RETURN --
What does this warning mean?
How can I avoid warnings?
This warning is informational only. It is letting you know that you might not be seeing all the data that can be extracted from the file and to add the -ee (extractEmbedded) option to your command if you want to see all of it. Things like GPS track data will not be shown without it because that content can be extensive.
You can safely ignore this warning and it can be suppressed by adding the -m (ignoreMinorErrors) option option to your command. This, however, will also suppress all other minor warnings. Actual errors will still be reported.
I am creating a pdf file using ghostscript 9.15. I have been using ghostscript for about 5 years and haven’t encountered any serious issues till this week.
Essentially I am running the query below….
“c://distiller/exes/gswin32c.exe" -sDEVICE="pdfwrite" -dNOPAUSE -dBATCH -dNOSAFER -dQUIET -sFONTPATH="C:/Windows/Fonts" -sOUTPUTFILE="C:\distiller\test.pdf" "C:\ExactDistiller \example.ps"
and I get the error below
Current allocation mode is local Last OS error: No such file or
directory Current file position is 197197 GPL Ghostscript 9.15:
Unrecoverable error, exit code 1
I am getting the error “No such file or directory” but I have checked every file path and checked every font is installed and all seems fine but still no luck. I just want to know is there a way of finding out what file / directory is causing the issue?
Well the first thing I would do is not set -dQUIET, turning off information isn't a good way to debug a problem. Not setting -dBATCH and -dNOPAUSE might be helpful too as it might show you more of what is going on. Note that -dNOSAFER is in fact the default, you don't need to set that unless you have previously set your environment to include -dSAFER
In short, when trying to debug the problem, simplify it as far as possible, don't set any switches unless you know you need them, or they contribute to the problem.
You could then paste the stdout transcript which might shed some illumination.
Secondly, your directory appears to contain a trailing space before the separator "c:\ExactDistiller \example.ps", is that correct ? A 'No such file or directory' error will be thrown (without a PostScript error) if you get the input filename incorrect.
If there is a PostScript error revealed when you stop issuing -dQUIET then you can paste it in your original question (by editing it) and I'll take a look. It may be necessary to see your example.ps file, depending on what that reveals.
I'm looking for Tsung source code. There is a line like following in file tsung.sh.in:
ERL_OPTS=" $ERL_DIST_PORTS -smp auto +P $MAX_PROCESS +A 16 +K true #ERL_OPTS# "
What does the #ERL_OPTS# mean?
This seems to be something that gets substituted by autoconf during the build process.
Generally, a .in file gets preprocessed by some build script. Autoconf uses #IDENTIFIER# to indicate the place where the actual value has to be put in. The preprocessed version loses the .in extension, thus generating tsung.sh in this particular case.
Okay so here is what I want to do. I want to add a print option that prints whatever the user's document is to a PDF and adds some headers before sending it off to a device.
I guess my questions are: how do I add a virtual "printer" driver for the user that will launch the application I've been developing that will make the PDF (or make the PDF and launch my application with references to the newly generated PDF)? How do I interface with CUPS to generate the PDF? I'm not sure I'm being clear, so let me know if more information would be helpful.
I've worked through this printing with CUPS tutorial and seem to get everything set up okay, but the file never seems to appear in the appropriate temporary location. And if anyone is looking for a user-end PDF-printer, this cups-pdf-for-mac-os-x is one that works through the installer, however I have the same issue of no file appearing in the indicated directory when I download the source and follow the instructions in the readme. If anyone can get either of these to work on a mac through the terminal, please let me know step-by-step how you did it.
The way to go is this:
Set up a print queue with any driver you like. But I recommend to use a PostScript driver/PPD. (A PostScript PPD is one which does not contain any *cupsFilter: ... line.):
Initially, use the (educational) CUPS backend named 2dir. That one can be copied from this website: KDE Printing Developer Tools Wiki. Make sure when copying that you get the line endings right (Unix-like).
Commandline to set up the initial queue:
lpadmin \
-p pdfqueue \
-v 2dir:/tmp/pdfqueue \
-E \
-P /path/to/postscript-printer.ppd
The 2dir backend now will write all output to directory /tmp/pdfqueue/ and it will use a uniq name for each job. Each result should for now be a PostScript file. (with none of the modifications you want yet).
Locate the PPD used by this queue in /etc/cups/ppd/ (its name should be pdfqueue.ppd).
Add the following line (best, near the top of the PPD):
*cupsFilter: "application/pdf 0 -" (Make sure the *cupsFilter starts at the very beginning of the line.) This line tells cupsd to auto-setup a filtering chain that produces PDF and then call the last filter named '-' before it sends the file via a backend to a printer. That '-' filter is a special one: it does nothing, it is a passthrough filter.
Re-start the CUPS scheduler:sudo launchctl unload /System/Library/LaunchDaemons/org.cups.cupsd.plist
sudo launchctl load /System/Library/LaunchDaemons/org.cups.cupsd.plist
From now on your pdfqueue will cause each job printed to it to end up as PDF in /tmp/pdfqueue/*.pdf.
Study the 2dir backend script. It's simple Bash, and reasonably well commented.
Modify the 2dir in a way that adds your desired modifications to your PDF before saving on the result in /tmp/pdfqueue/*.pdf...
Update: Looks like I forgot 2 quotes in my originally prescribed *cupsFilter: ... line above. Sorry!
I really wish I could accept two answers because I don't think I could have done this without all of #Kurt Pfeifle 's help for Mac specifics and just understanding printer drivers and locations of files. But here's what I did:
Download the source code from codepoet cups-pdf-for-mac-os-x. (For non-macs, you can look at http://www.cups-pdf.de/) The readme is greatly detailed and if you read all of the instructions carefully, it will work, however I had a little trouble getting all the pieces, so I will outline exactly what I did in the hopes of saving someone else some trouble. For this, the directory with the source code is called "cups-pdfdownloaddir".
Compile cups-pdf.c contained in the src folder as the readme specifies:
gcc -09 -s -lcups -o cups-pdf cups-pdf.c
There may be a warning: ld: warning: option -s is obsolete and being ignored, but this posed no issue for me. Copy the binary into /usr/libexec/cups/backend. You will likely have to the sudo command, which will prompt you for your password. For example:
sudo cp /cups-pdfdownloaddir/src/cups-pdf /usr/libexec/cups/backend
Also, don't forget to change the permissions on this file--it needs root permissions (700) which can be changed with the following after moving cupd-pdf into the backend directory:
sudo chmod 700 /usr/libexec/cups/backend/cups-pdf
Edit the file contained in /cups-pdfdownloaddir/extra/cups-pdf.conf. Under the "PDF Conversion Settings" header, find a line under the GhostScript that reads #GhostScript /usr/bin/gs. I did not uncomment it in case I needed it, but simply added beneath it the line Ghostscript /usr/bin/pstopdf. (There should be no pre-cursor # for any of these modifications)
Find the line under GSCall that reads #GSCall %s -q -dCompatibilityLevel=%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile="%s" -dAutoRotatePage\
s=/PageByPage -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -c .setpdfwrite \
-f %s Again without uncommenting this, under this I added the line GSCall %s %s -o %s %s
Find the line under PDFVer that reads #PDFVer 1.4 and change it to PDFVer, no spaces or following characters.
Now save and exit editing before copying this file to /etc/cups with the following command
sudo cp cups-pdfdownloaddir/extra/cups-pdf.conf /etc/cups
Be careful of editing in a text editor because newlines in UNIX and Mac environments are different and can potentially ruin scripts. You can always use a perl command to remove them, but I'm paranoid and prefer not to deal with it in the first place.
You should now be able to open a program (e.g. Word, Excel, ...) and select File >> Print and find an available printer called CUPS-PDF. Print to this printer, and you should find your pdfs in /var/spool/cups-pdf/yourusername/ by default.
*Also, I figured this might be helpful because it helped me: if something gets screwed up in following these directions and you need to start over/get rid of it, in order to remove the driver you need to (1) remove the cups-pdf backend from /usr/libexec/cups/backend (2) remove the cups-pdf.conf from /etc/cups/ (3) Go into System Preferences >> Print & Fax and delete the CUPS-PDF printer.
This is how I successfully set up a pdf backend/filter for myself, however there are more details, and other information on customization contained in the readme file. Hope this helps someone else!