View Single Post
Old 10-03-2020, 01:10 AM   #7
MarjaE
Guru
MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.MarjaE ought to be getting tired of karma fortunes by now.
 
Posts: 924
Karma: 53902736
Join Date: Jun 2015
Device: multiple
Here's an updated Mac implementation. It differs from my 1st draft in 3 respects:

1. It adds a Quartz step at the beginning, to reduce the risk of losing text information, and of sizing issues.

2. It adds the wait steps.

3. It adds another k2pdfopt step at the end, to remove the now-useless tables of contents.

I don't have the programming knowledge for j/p.s.'s suggestions.

Requires Automator with a shell script, using bash, and passing input as arguments (Mac-specific), Quartz (Mac-specific, but other apps may accomplish the same goals in Linux and Windows), Python 3, a couple scripts from Benwiggy's PDFSuite edited to work with Python 3, ghostscript, Willus's k2pdfopt, cpdf, and qpdf.

for f in "$@"
do
# Strip any table of contents and fit text to page sizes to avoid any scaling issues
/usr/local/bin/python3 /Users/Marja/Library/Services/quartzfilter3.py "$f" "/Users/Marja/Library/Filters/Generic RGB.qfilter" "/Users/Marja/Splice/GRGB.pdf"
wait
# Copy and Rasterize 1st page from source pdf using k2pdfopt
~/Applications/k2pdfopt -ui -mode copy -p 1 -x -o "/Users/Marja/Splice/Cover_rgb.pdf" "/Users/Marja/Splice/GRGB.pdf" $@
wait
# Copy images from same source pdf file using Ghostscript, rasterize images using K2pdfopt
# Due to compatibility issues, dumping to ~/Splice/Images.pdf
/usr/local/bin/gs -sDEVICE=pdfimage24 -dFILTERTEXT -dCompatibilityLevel=1.4\
-g800x1080 -r150 -dPDFFitPage \
-sstdout=%sstderr -dNOPAUSE -dQUIET -dBATCH -sOutputFile="/Users/Marja/Splice/Images.pdf" "/Users/Marja/Splice/GRGB.pdf"
wait
~/Applications/k2pdfopt -ui -mode copy -x -o "/Users/Marja/Splice/Images_rgb.pdf" "/Users/Marja/Splice/Images.pdf" $@
wait
# Copy text from source pdf file using Ghostscript, turn text black using Cpdf
# The color conversion strategy should help with the 2nd stage if I switch to Ghostscript
# - and -_ indicate standard output and input
# Due to compatibility issues, dumping to ~/Splice/Text.pdf
/usr/local/bin/gs -sDEVICE=pdfwrite -dFILTERIMAGE -dFILTERVECTOR -dCompatibilityLevel=1.4 -sColorConversionStrategy=RGB -sstdout=%sstderr -dNOPAUSE -dQUIET -dBATCH -sOutputFile="/Users/Marja/Splice/Text.pdf" "/Users/Marja/Splice/GRGB.pdf"
wait
/usr/local/bin/cpdf "/Users/Marja/Splice/Text.pdf" -blacktext -o "/Users/Marja/Splice/Blacktext.pdf"
wait
# Splice files using qpdf
/usr/local/bin/qpdf --collate "/Users/Marja/Splice/Cover_rgb.pdf" --pages "/Users/Marja/Splice/Cover_rgb.pdf" "/Users/Marja/Splice/Images_rgb.pdf" "/Users/Marja/Splice/Blacktext.pdf" -- /Users/Marja/Splice/SplicedRGB.pdf
wait
# Remove any table of contents, since it won't fit the spliced pdf
suffix="-SplicedRgbG.pdf"
base=`basename "$f" .pdf`
outputfile=$base$suffix
~/Applications/k2pdfopt -ui -mode copy -n -toc- -o /Users/Marja/Splice/"$outputfile" /Users/Marja/Splice/SplicedRGB.pdf $@
done
MarjaE is offline   Reply With Quote