3

We have a folder with over 900 PDF documents, and these need to be watermark starting in january. The watermark is available as a JPEG or PNG, along with a string containing various characters for authoritie or companies, and if posible, they should also be signed. I found these answers but i not quite sure how to proced.

How can I solve this with a script, and what extensions do I need? Or can I combine two extensions? We usually work with Windows system but have recently received two Ubuntu desktops. I'm not an administrator, just an enduser, and i have a experience with Bash and Perl and debian or ubuntu. Would greatly appreciate some guidance.

3
  • 1
    when you say "signed", you mean with a digital signature, right? In which format is your signing key available? Commented Oct 26, 2024 at 16:20
  • yes these shoulf be signed certificates but i dont know which ones they are, because we will receive the requirements from the auhtorities in the first two weeks in januars. we dont create the certifiaces ourselved we will receive them premade. we should start looking for ways to do this quicky and easily for the future because everythig doing manually with different pdf programms would be a nigthmare. Commented Oct 26, 2024 at 16:47
  • 2
    "self-signed certificate" and "we don't create the certificates ourselves" is kind of self-contradicting. Commented Oct 26, 2024 at 16:55

1 Answer 1

3

If you're familiar with bash, the process of "apply something to a lot of files" is trivial: just make the "something" either a shell function

something() {
  infile="$1"
  outfile="$2"
  dothefirststep "${infile}"
  dothesecondstep -o "${outfile}" …
}

or put the something in a shell or perl script file (say, something.sh or something.pl), make it executable, and then run each file matching a glob through it:

for file in *.pdf; do something "${file}" "${file%.pdf}-signed.pdf"; done

Now, how does one add watermarks? Never done this myself, because I consider it a nuisance to the user that should not be done, but OK, it's your job, can't tell you what to do. How you'd do it:

Preparation:

  1. Get your certificate in PEM format and your private signing key (matching that certificate, of course) in PEM format
  2. take your watermark images and make page-sized PDFs out of them, with transparent background. Inkscape is an easy way to create such single-page PDFs.
  3. Write a script to put a watermark atop of each page. I'd probably use Python and the PyPDF2 library. The following is top-of-my-head, untested code. You'll probably have to fiddle with it until it works (I call it watermark.py:
#!/usr/bin/env python3
from sys import argv
from PyPDF2 import PdfReader, PdfWriter

with open(argv[1], "rb") as in_file, open(argv[2], "rb") as wtrmrk_file:
  out = PdfWriter()
  document = PdfReader(in_file)
  watermark = PdfReader(wtrmrk_file).pages[0]

  for page in document.pages:
    page.merge_page(watermark)
    out.add_page(page)

  with open(argv[3], "wb") as out_file:
    out.write(out_file)

(chmod 755 watermark.py, and you can run it as /path/to/watermark.py infile.pdf watermark.pdf outfile.pdf)

  1. You'll want to install libpodofo-utils (or however it's called on your distro, but on debian, that's the name), and make sure you have the podofosign program.

Now, everything falls into place, in bash:

sign() {
  infile=$1
  outfile=$2
  podofosign -in "${infile}" -cert "yourcert.pem" -pkey "yourkey.pem" -out "${outfile}"
}

mark() {
  infile=$1
  outfile=$2
  /path/to/watermark "${infile}" "yourwatermark.pdf" "${outfile}"
}

mark_and_sign() {
  infile=$1
  mark "${infile}" "${infile%.pdf}-marked.pdf"
  sign "${infile%.pdf}-marked.pdf" "${infile%.pdf}-signed.pdf"
  # optionally delete unsigned watermarked file:
  # rm "${infile%.pdf}-marked.pdf"
}

for file in *.pdf; do
  mark_and_sign "${file}"
done
1
  • 2
    Note that defining them with the keyword functionis a bashishm, function foo() { ... } can simply be written as foo() { ... }, which is the POSIX way to do that. Commented Oct 27, 2024 at 14:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.