Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator: Watermark PDF Documents not working

Hi,


The "Watermark PDF Documents" action of Automator is not working.


No matter which files you choose, and I tested a lot of combinations, the action ends up with the following error:


"Watermark PDF Watermark failed - 1 error"

"Command line tool returned error 1. (1)"


User uploaded file


I have a clean Install of Yosemite. I tested with 2 more Mac Book Pro's with Yosemite and the same happen.


To replicate you just have to create a new Automator, with 2 actions:

- Get Specified Finder Items, and choose a PDF file

- Watermark PDF Documents, and choose a PNG or JPG file


Run and see the error.


Thank you.

MacBook Pro, OS X Mavericks (10.9)

Posted on Oct 23, 2014 3:24 AM

Reply
53 replies

Nov 18, 2014 3:23 PM in response to jbdumont

Ok, so the python file looks ok

Now try to run the command from Terminal so that you can post the error messages.

Enter this in Terminal (copy a test PDF to the Desktop and name it: input.PDF)


~/Desktop/tool.py --under --xOffset 15 --yOffset 55 --angle 0 --scale 0.75 --opacity 0.5 --input ~/Desktop/input.PDF --output ~/Desktop/output.PDF ~/Dropbox/Signature.png


Update: I copied your tool.py that you posted, and it worked from Terminal for me with the above command.

Nov 18, 2014 4:38 PM in response to jbdumont

Great! Sorry it took so long.

After we ruled out the python script as the source of error, I went back and realized that the Apple forum software sometimes does some unexpected things, like autocorrect or adding spaces where none were posted 🙂)


BTW, if you want to expand the Workflow, what I did was select Application in Automator, and with the dispense items incrementally Workflow from http://automator.us/leopard/downloads/ I'm able to "drop" multiple PDF's or even a Folder of PDF's on the icon to process a group of PDF's:


User uploaded file

Dec 2, 2014 4:21 AM in response to joepfromuden

joepfromuden wrote:


Hey Tony,


i can't get the script working could you help me please? i have insert a image for the traceback. can you tell me what i do wrong.


Check that there are no spaces after the escapes "\":


~/Documents/tool.py \<--Check that there is no spaces after this!  
--under \<--Must be a line feed after every escape  
--xOffset 15 \  
--yOffset 55 \  
--angle 0 \  
etc... 

Dec 2, 2014 6:38 AM in response to Tony T1

Hey Tony,


First of al thanks for helping me out with the script, i hope you have a solution for me.

I have checked the spaces, and insert the script below.



~/Desktop/tool.py  \
--under  \
--xOffset  0  \
--yOffset  0  \
--angle  0  \
--scale  0.75  \
--opacity  0.5  \
--input  "$1"  \
--output  "${1%.*}  (COPY) .${1##*.}”  \
~/Desktop/b.jpg


And also the Tool.py script.


#!/usr/bin/python
# Watermark each page in a PDF document


import sys
import getopt
import math
import shutil
from Quartz.CoreGraphics import *
from Quartz.ImageIO import *


def drawWatermark(ctx, image, xOffset, yOffset, angle, scale, opacity):
  if image:
  imageWidth = CGImageGetWidth(image)
  imageHeight = CGImageGetHeight(image)
  imageBox = CGRectMake(0, 0, imageWidth, imageHeight)

  CGContextSaveGState(ctx)
  CGContextSetAlpha(ctx, opacity)
  CGContextTranslateCTM(ctx, xOffset, yOffset)
  CGContextScaleCTM(ctx, scale, scale)
  CGContextTranslateCTM(ctx, imageWidth / 2, imageHeight / 2)
  CGContextRotateCTM(ctx, angle * math.pi / 180)
  CGContextTranslateCTM(ctx, -imageWidth / 2, -imageHeight / 2)
  CGContextDrawImage(ctx, imageBox, image)
  CGContextRestoreGState(ctx)


def createImage(imagePath):
  image = None
  url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, imagePath, len(imagePath), False)
  if provider:
  imageSrc = CGImageSourceCreateWithDataProvider(provider, None)
  if imageSrc:
  image = CGImageSourceCreateImageAtIndex(imageSrc, 0, None)
  if not image:
  print "Cannot import the image from file %s" % imagePath
  return image


def watermark(inputFile, watermarkFiles, outputFile, under, xOffset, yOffset, angle, scale, opacity, verbose):

  images = map(createImage, watermarkFiles)

  ctx = CGPDFContextCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, outputFile, len(outputFile), False), None, None)
  if ctx:
  pdf = CGPDFDocumentCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, inputFile, len(inputFile), False))
  if pdf:

  for i in range(1, CGPDFDocumentGetNumberOfPages(pdf) + 1):
  image = images[i % len(images) - 1]
  page = CGPDFDocumentGetPage(pdf, i)
  if page:
  mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
  if CGRectIsEmpty(mediaBox):
  mediaBox = None

  CGContextBeginPage(ctx, mediaBox)
  if under:
  drawWatermark(ctx, image, xOffset, yOffset, angle, scale, opacity)
  CGContextDrawPDFPage(ctx, page)
  if not under:
  drawWatermark(ctx, image, xOffset, yOffset, angle, scale, opacity)
  CGContextEndPage(ctx)

  del pdf
  CGPDFContextClose(ctx)
  del ctx


def main(argv):


  verbose = False
  readFilename = None
  writeFilename = None
  under = False
  xOffset = 0
  yOffset = 0
  angle = 0
  scale = 1.0
  opacity = 1.0

  # Parse the command line options
  try:
  options, args = getopt.getopt(argv, "vutx:y:a:p:s:i:o:", ["verbose", "under", "over", "xOffset=", "yOffset=", "angle=", "opacity=", "scale=", "input=", "output=", ])
  except getopt.GetoptError:
  usage()
  sys.exit(2)

  for option, arg in options:

  print option, arg

  if option in ("-i", "--input") :
  if verbose:
  print "Reading pages from %s." % (arg)
  readFilename = arg


  elif option in ("-o", "--output") :
  if verbose:
  print "Setting %s as the output." % (arg)
  writeFilename = arg


  elif option in ("-v", "--verbose") :
  print "Verbose mode enabled."
  verbose = True


  elif option in ("-u", "--under"):
  print "watermark under PDF"
  under = True


  elif option in ("-t", "--over"):
  print "watermark over PDF"
  under = False

  elif option in ("-x", "--xOffset"):
  xOffset = float(arg)

  elif option in ("-y", "--yOffset"):
  yOffset = float(arg)

  elif option in ("-a", "--angle"):
  angle = -float(arg)


  elif option in ("-s", "--scale"):
  scale = float(arg)

  elif option in ("-p", "--opacity"):
  opacity = float(arg)

  else:
  print "Unknown argument: %s" % (option)


  if (len(args) > 0):
  watermark(readFilename, args, writeFilename, under, xOffset, yOffset, angle, scale, opacity, verbose);
  else:
  shutil.copyfile(readFilename, writeFilename); 

def usage():
  print "Usage: watermark --input <file> --output <file> <watermark files>..."

if __name__ == "__main__":
  print sys.argv
  main(sys.argv[1:])


thanks again!!!

Dec 3, 2014 6:33 AM in response to joepfromuden

joepfromuden wrote:


hey tony,


have have test the py scirpt thats okay.

but i have now in line 8 a problem.

thanks


ok, so the problem is within Automator.

Try with the command on one line (no '\') in the Automator Run Shell Script action:


User uploaded file


~/Library/Scripts/tool.py --under --xOffset 15 --yOffset 55 --angle 0 --scale 0.75 --opacity 0.5 --input "$1" --output "${1%.*} (COPY).${1##*.}" ~/Pictures/Other\ Images/Watermark\(COPY\).png

Automator: Watermark PDF Documents not working

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.