Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

0004838

Suspended
Original poster
Oct 1, 2014
193
64
I have two scripts that I use for manipulating PDFs, and I'd like to merge them into a single one. However, I struggle to get the output from the first command to act as input for the second.

Script one simply flattens the PDF with Ghostscript and modifies the file extension to differentiate the output from the original:
Code:
/usr/local/bin/gs -sDEVICE=pdfwrite -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$@.flat.pdf" "$@"

Script two then locks the flattened PDF so that it can only be edited by those with the password:
Code:
/usr/local/bin/pdftk "$@" output "$@.locked.pdf" owner_pw foo

I'd like this to be a single script. Can someone assist?

Also, the use of the plain text password in the second script is unfortunate, but I don't yet know how to pull the password from Keychain Access. I know the CLI tools are there, but haven't successfully implemented them yet :( . If someone offered a clear example of implementing this that'd be great.
 

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
Hi,

I can't seem to get PDFTk to work here, for some reason, so I haven't been able to test this. However, Couldn't you just roll it all together into one script? Something (very roughly) like this...

Code:
theInputFile="$@"; theOutputFile=${theInputFile%.*}.flat.pdf ; /usr/local/bin/gs -sDEVICE=pdfwrite -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$theOutputFile" "$theInputFile" ; echo /usr/local/bin/pdftk "$theOutputFile" output "${theInputFile%.*}.locked.pdf" owner_pw foo
 
  • Like
Reactions: 0004838

0004838

Suspended
Original poster
Oct 1, 2014
193
64
Hi,

Finally got around to testing this and it works. So: thank you.

There was one little thing: what is the purpose of the "echo" in the final command? I had to remove it to get the script to run correctly.

I'm probably being greedy, but you wouldn't happen to know how to modify the script to prevent the creation of the intermediate, "${theInputFile%.*}.flat.pdf" file would you? Not a huge deal, as I can just prepend a clean-up command and send it to the Trash, but it'd be the cherry on top :)
 

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
There was one little thing: what is the purpose of the "echo" in the final command? I had to remove it to get the script to run correctly.

Ah, yes. Sorry. I should have mentioned that. I put the echo there so it'd print the pdftk command rather than actually run it. I did that because my copy of pdftk doesn't seem to be working properly. You were right to remove it.[/QUOTE]

I'm probably being greedy, but you wouldn't happen to know how to modify the script to prevent the creation of the intermediate, "${theInputFile%.*}.flat.pdf" file would you? Not a huge deal, as I can just prepend a clean-up command and send it to the Trash, but it'd be the cherry on top :)

Someone may correct me, but I don't think pdftk can write to the same file it read from. And if it will, then doing that sort of thing makes me nervous. I'd suggest sticking with the method I've shown. You can always add an extra bit of shell script to delete the intermediate file at the end if necessary.

regards
 

0004838

Suspended
Original poster
Oct 1, 2014
193
64
Ah, yes. Sorry. I should have mentioned that. I put the echo there so it'd print the pdftk command rather than actually run it. I did that because my copy of pdftk doesn't seem to be working properly. You were right to remove it.

Aha, understood.

Someone may correct me, but I don't think pdftk can write to the same file it read from. And if it will, then doing that sort of thing makes me nervous. I'd suggest sticking with the method I've shown. You can always add an extra bit of shell script to delete the intermediate file at the end if necessary.
I think I've learned from experience that pdftk cannot write to the same file it read from, it would explain some issues I had during script testing. So, I'll just use the clean-up script.

Thanks for the assistance.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.