The Problem and Exact Symptom
Error: /invalidfileaccess in --file-- aborts the run and Ghostscript writes no output PDF. Since version 9.50, -dSAFER is enabled by default, so the ICC profile that PDFX_def.ps opens with the PostScript file operator is blocked unless its path is whitelisted with --permit-file-read=.
Exact Fix
gs -dPDFX -dBATCH -dNOPAUSE \
-sDEVICE=pdfwrite \
-sProcessColorModel=DeviceCMYK \
-sColorConversionStrategy=CMYK \
--permit-file-read=/usr/share/color/icc/ \
-sOutputFile=out-x3.pdf \
PDFX_def.ps input.pdf
The trailing / permits every file directly inside that directory; use --permit-file-read=/usr/share/color/icc/* to include subdirectories. PDFX_def.ps must precede the input file because it injects the OutputIntent through pdfmark operators.
-dPDFX defaults to PDF/X-3; use -dPDFX=1 for PDF/X-1a. No --permit-file-write= is needed: the file named by -sOutputFile= is already writable under default SAFER.
Copy PDFX_def.ps from the lib directory of your Ghostscript installation, then edit its % Customize lines so /ICCProfile uses the identical path string you whitelisted:
/ICCProfile (/usr/share/color/icc/ISOcoated_v2_300_eci.icc) def % Customize
% Inside the {OutputIntent_PDFX} dictionary:
/OutputCondition (Commercial and specialty offset) % Customize
/OutputConditionIdentifier (ISO Coated v2 300%) % Customize
/RegistryName (http://www.color.org) % Do not change
| Flag / Setting | What It Does | Required or Optional |
|---|---|---|
-dPDFX |
Enforces PDF/X output rules (defaults to X-3) and forces -dCompatibilityLevel=1.3 |
Required |
-dBATCH |
Exits after the last input file | Required for unattended runs |
-dNOPAUSE |
Suppresses per-page prompts | Required for unattended runs |
-sDEVICE=pdfwrite |
Selects the PDF writer device | Required |
-sProcessColorModel=DeviceCMYK |
Sets the process color model; DeviceGray is the only other valid choice |
Required |
-sColorConversionStrategy=CMYK |
Converts all content to CMYK; RGB is not allowed for PDF/X-3 |
Required |
--permit-file-read=/usr/share/color/icc/ |
Adds the ICC directory to the SAFER permitted-read list | Required on 9.50+ when embedding a profile |
-sOutputFile=out-x3.pdf |
Destination PDF | Required |
PDFX_def.ps |
Prefix file emitting GTS_PDFXVersion, Trapped, and the OutputIntent pdfmarks |
Required, before the input |
/ICCProfile |
Fully qualified path to the press ICC profile | Required unless a registered condition is used without embedding |
/OutputConditionIdentifier |
Names the characterized printing condition | Required |
/RegistryName |
Must be (http://www.color.org) |
Required |
Windows (gswin64c)
gswin64c -dPDFX -dBATCH -dNOPAUSE -sDEVICE=pdfwrite ^
-sProcessColorModel=DeviceCMYK -sColorConversionStrategy=CMYK ^
--permit-file-read=C:/color/icc/ ^
-sOutputFile=out-x3.pdf PDFX_def.ps input.pdf
Use forward slashes (Artifex’s recommendation), quote paths containing spaces, and separate multiple permitted paths with ; instead of :. Path matching stays case-sensitive even on Windows.
Fallback for trusted local files
-dNOSAFER disables the sandbox entirely (it is equivalent to -dDELAYSAFER). The official PDF/X documentation explicitly does not recommend it. Prefer --permit-file-read=, and never combine -dNOSAFER with PostScript from untrusted sources. Permit lists apply to a single invocation only; nothing persists between runs.
Why It Happens and Tool Edge Cases
- CI base images upgraded past 9.50. Conversion scripts written for 9.2x ran with unrestricted file access and break unchanged on current Debian/Alpine images. Command-line files (
PDFX_def.ps,input.pdf) are auto-added to the permitted-read list, but the ICC opened by(r) fileinside PostScript is not. The pre-9.50save/.setsafe/restoretrick no longer lifts the restrictions; controls now stay active until the interpreter exits. - The stock sample’s relative ICC path.
lib/PDFX_def.psships with/ICCProfile (ISO Coated sb.icc), resolved against the working directory. Thefileoperator ignores-IandGS_LIBsearch paths, so Docker, systemd, or headless LibreOffice workers with a different CWD fail beforeSAFEReven evaluates the read. Ghostscript ships no press profile; supply one (for example ISO Coated v2 from ECI) yourself. - RGB exports and missing OutputIntents. Pandoc, WeasyPrint, and InDesign exports contain RGB objects and no
/OutputIntents, which KDP/IngramSpark preflight rejects. Runningpdfwritewithout-dPDFXand the definition file produces a plain PDF, not PDF/X. Setting-sColorConversionStrategy=RGBtrips the definition file’s own guard:ERROR: ColorConversionStrategy must be /DeviceGray or /DeviceCMYK.followed by/rangecheck. For content that cannot convert cleanly, thePDFACompatibilityPolicydistiller parameter (1= elide with warning,2= abort) also governs PDF/X runs. - Common Mistake That Breaks the Fix.
SAFERmatching is literal string comparison: whitelisting/usr/share/color/icc/while referencing the profile through~/…, a symlink, a relative path, or different casing still fails. Omitting the trailing/matches only a file with that exact name, not the directory’s contents;*covers subdirectories. On Windows the list separator is;, not:.
Verification and Rollback
Confirm the run exited cleanly:
echo $?
Expected output is 0, with no /invalidfileaccess in the log and out-x3.pdf present. Then inspect the PDF/X structures:
qpdf --qdf --object-streams=disable out-x3.pdf out-x3.qdf
grep -a -E "GTS_PDFXVersion|/S /GTS_PDFX|DestOutputProfile" out-x3.qdf
Expected output (object numbers vary):
/GTS_PDFXVersion (PDF/X-3:2002)
/S /GTS_PDFX
/DestOutputProfile 12 0 R
A missing /GTS_PDFXVersion means the definition file never executed; check that it precedes the input file. Also inspect /TrimBox and /BleedBox in out-x3.qdf: pdfwrite writes both equal to /MediaBox by default, adjustable through the PDFXTrimBoxToMediaBoxOffset and PDFXSetBleedBoxToMediaBox distiller parameters.
For a full conformance pass, run Acrobat Pro Preflight’s PDF/X-3 verification profile; it should report no errors.
Roll back if the conversion introduces regressions. pdfwrite never modifies input.pdf, so the source stays your restore point:
# Take this backup BEFORE editing the definition file in Section 2:
cp PDFX_def.ps PDFX_def.ps.bak
# Rollback:
cp PDFX_def.ps.bak PDFX_def.ps
rm -f out-x3.pdf out-x3.qdf
If you used the -dNOSAFER fallback, delete the switch; default SAFER returns on the next invocation with no persistent state. If CMYK conversion shifts colors unacceptably, re-export from the source application with the target press profile instead of converting out-x3.pdf a second time.



