]> Sergey Matveev's repositories - dotfiles.git/commitdiff
ps2pdf sanitizes XMP. Rewritten on zsh
authorSergey Matveev <stargrave@stargrave.org>
Thu, 27 Jan 2022 16:19:33 +0000 (19:19 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 27 Jan 2022 18:19:00 +0000 (21:19 +0300)
bin/bin/ps2pdf.sh [deleted file]
bin/bin/ps2pdf.zsh [new file with mode: 0755]

diff --git a/bin/bin/ps2pdf.sh b/bin/bin/ps2pdf.sh
deleted file mode 100755 (executable)
index b892beb..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh
-
-gs \
-    -dBATCH -dNOPAUSE -dSAFER \
-    -sDEVICE=pdfwrite -dPDFA \
-    -dPDFSETTINGS=/default \
-    -dAutoRotatePages=/None \
-    -sColorConversionStrategy=DeviceRGB \
-    -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode \
-    -dAutoFilterGrayImages=false -dGrayImageFilter=/FlateEncode \
-    -dMonoImageFilter=/FlateEncode \
-    -dOptimize=true \
-    -dEmbedAllFonts=true \
-    -sOutputFile="$1".pdf "$1"
diff --git a/bin/bin/ps2pdf.zsh b/bin/bin/ps2pdf.zsh
new file mode 100755 (executable)
index 0000000..5f7bef2
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/env zsh
+# Original ps2pdf is just a trivial caller to gs too.
+# That ones is lossless, tries to be closer to PDF/A-1.
+# It also copies Producer metainformation tag to Creator,
+# removes all empty metainformation fields, removed fields
+# with uuid:s, removes Format (that is application/pdf).
+
+set -e
+opts=(
+    -dQUIET
+    -dBATCH
+    -dNOPAUSE
+    -dSAFER
+    -sDEVICE=pdfwrite
+    -dPDFSETTINGS=/default
+    -dPDFA=1
+    -dCompatibilityLevel=1.4
+    -dPDFACompatibilityPolicy=3 # that will skip some errors about XMP
+    -dAutoRotatePages=/None
+    -dOptimize=true
+    -sColorConversionStrategy=RGB # advised for PDF/A
+    -sProcessColorModel=DeviceRGB # advised for PDF/A
+    -dAutoFilterColorImages=false
+    -dAutoFilterGrayImages=false
+    -dAutoFilterMonoImages=false
+    -dColorImageFilter=/FlateEncode
+    -dGrayImageFilter=/FlateEncode
+    -dMonoImageFilter=/FlateEncode
+    -sOutputFile="$1".pdf
+)
+gs ${=opts} $1
+
+toremove=(Format)
+exiftool $1.pdf | while read l ; do
+    f=${${(s/:/)l}[1]:gs/ //}
+    [[ "$l" =~ ": " ]] || toremove=($f $toremove)
+    [[ "$l" =~ ": uuid:" ]] && toremove=($f $toremove)
+done
+for i ({1..${#toremove}}) toremove[$i]=-${toremove[$i]}=
+exiftool -zip -overwrite_original -quiet \
+    -tagsFromFile $1 -Producer\>Creator \
+    ${=toremove} $1.pdf