]> Sergey Matveev's repositories - dotfiles.git/blobdiff - bin/bin/my-ps2pdf
Less .sh and more explicit my-
[dotfiles.git] / bin / bin / my-ps2pdf
diff --git a/bin/bin/my-ps2pdf b/bin/bin/my-ps2pdf
new file mode 100755 (executable)
index 0000000..4f34bdb
--- /dev/null
@@ -0,0 +1,44 @@
+#!/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
+qpdf --linearize --compress-streams=y $1.pdf $1.pdf.pdf
+mv $1.pdf.pdf $1.pdf