PDF/A importieren und als normale PDF speichern

Hallo,

ich habe für mich ein ziemlich nerviges Problem mit PDF/A-Dateien gelöst. Da ich beim Import von gewissen PDF-Dateien einen Stempel setze habe ich sehr häufig das Problem gehabt, dass der Stempel den Text-Layer einer PDF/A-Datei unbrauchbar gemacht hat. Das Team von DEVONthink hat zwar immer wieder Anpassungen in den letzten Jahren vorgenommen, aber es kommt trotzdem sehr häufig zu dem Fehler, da Änderungen in macOS nicht immer gleich berücksichtigt werden können.

Ich habe jetzt mit einem AppleScript in einer intelligenten Regel unter Verwendung von ghostcript (brew install ghostscript) und pdfinfo (brew install poppler) das Problem weitestgehend behoben, indem ich die PDF/A-Dateien beim Import und vor dem Stempeln als normale PDF speichere.

Die Pfade müssten ggf. angepasst werden (einfach “where gs” und “where pdfinfo” im Terminal eingeben und das Ergebnis ins Skript einsetzen), insb. bei Apple Silicon und Intel Prozessoren ggf. unterschiedlich:

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			if (kind of theRecord is "PDF+Text" or kind of theRecord is "PDF" or kind of theRecord is "PDF-Dokument" or kind of theRecord is "PDF-Document" or kind of theRecord is "PDF document") then
				set thePath to path of theRecord
				set posixPath to POSIX path of thePath
				set tempFolderPath to POSIX path of (path to temporary items from user domain)
				set gsPath to "/opt/homebrew/bin/gs" -- Path to Ghostscript
				set pdfinfoPath to "/opt/homebrew/bin/pdfinfo" -- Path to pdfinfo
				set tempPdfPath to tempFolderPath & "DEVONthink_temp.pdf"
				
				-- Command to check PDF/A and PDF/UA compliance
				set checkCommand to pdfinfoPath & " -meta \"" & posixPath & "\" | grep -E 'PDF/A|PDF/UA|pdfaid|pdfuaid|pdfa'"
				
				try
					-- Run the check command and capture the output
					set checkOutput to do shell script checkCommand
					
					-- Check if any known PDF/A or PDF/UA conformance markers are present in the output
					if checkOutput is not "" then
						-- PDF is compliant with some form of PDF/A or PDF/UA, attempt conversion
						set convertCommand to gsPath & " -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -sOutputFile=\"" & tempPdfPath & "\" \"" & posixPath & "\""
						do shell script convertCommand
						
						-- Replace original file with converted file
						do shell script "mv -f " & quoted form of tempPdfPath & " " & quoted form of posixPath
						
						log message "The file " & thePath & " was PDF/A compliant and has been converted to standard PDF." info "PDF/A Conversion"
					else
						-- PDF is not PDF/A compliant, no action needed
						log message "The file " & thePath & " is not PDF/A compliant and no conversion is necessary." info "PDF/A Check"
					end if
				on error errMsg
					-- Handle error during PDF compliance check or conversion
					log message "Error: " & errMsg & " while processing " & thePath info "PDF Compliance Process"
				end try
				
			end if
		end repeat
	end tell
end performSmartRule

Für alle, die es nützlich finden …

Es gibt zwar noch Renderer (zB FoxIt) die fehlerhafte PDF/A in Umlauf bringen, aber das löse ich auch noch in den nächsten Tagen und poste das auch hier.

Grüße
Chris