Tony T1

Q: Automator Watermark PDF Workflow

Seems with every major upgrade, Apple breaks the Automator Watermark PDF Workflow.

Can someone test this workflow in Yosemite so I know if the problem I'm having is with OS X 10.10.

OS X Yosemite (10.10)

Posted on Oct 22, 2014 9:48 AM

Close

Q: Automator Watermark PDF Workflow

  • All replies
  • Helpful answers

first Previous Page 3 of 4 last Next
  • by Space-Rat,

    Space-Rat Space-Rat May 19, 2015 2:26 AM in response to patriqq
    Level 1 (0 points)
    May 19, 2015 2:26 AM in response to patriqq

    Hi!

    Thanks to Tony T1 & Hiroto first - great work guys!

     

    I'm used this with previous OSX versions and now under 10.10 it's broken. So I managed to read through these topics here. What I've done so far:

     

    Created a new folder on my desktop which should contain the neccessary files. I downloaded the tool.py from this topic - chmod'ed it and it works within command line:

     

    ./tool.py -h

    ['./tool.py', '-h']

    Usage: watermark --input <file> --output <file> <watermark files>...

     

    Using the tool.py manually works great as well:

     

    ./tool.py --under --xOffset 30 --yOffset 60 --angle 0 --scale 0.24 --opacity 1 --input test.pdf --output output.PDF vorlage.png

    ['./tool.py', '--under', '--xOffset', '30', '--yOffset', '60', '--angle', '0', '--scale', '0.24', '--opacity', '1', '--input', 'test.pdf', '--output', 'output.PDF', 'vorlage.png']

    --under

    watermark under PDF

    --xOffset 30

    --yOffset 60

    --angle 0

    --scale 0.24

    --opacity 1

    --input test.pdf

    --output output.PDF

     

    Here it is: http://pastebin.com/WyUPdh7x

     

    Now I created an automator action based on Tony T1's - "Ask for finder objects" - "filter finder objects" - "execute shell sctipt" - it's all in german for me.

     

    Unfortunately I get the error on line 8. I already removed every space after the "\" and I already tried to put everything into one line - no success at all.

    Any idea about this?

     

    Bildschirmfoto 2015-05-19 um 11.21.22.png

     

    Line 8 is the input one. I already replaced that one completely with one from another quote here... no success so far.

     

    Ideas? Thanks a lot!

  • by Tony T1,

    Tony T1 Tony T1 May 19, 2015 6:50 AM in response to Space-Rat
    Level 6 (9,249 points)
    Mac OS X
    May 19, 2015 6:50 AM in response to Space-Rat

    Since error is related to matching quotes, try deleting and re-entering every "

  • by Space-Rat,

    Space-Rat Space-Rat May 19, 2015 6:59 AM in response to Tony T1
    Level 1 (0 points)
    May 19, 2015 6:59 AM in response to Tony T1

    That worked - thanks!

     

    Unfortunately the direct call of tool.py didn't work. Neither did ./tool.py - it only worked with the full path given. Little bit annoying since I can't copy the whole directory with the .py & workflow script to another computer with a different user without re-editing that stuff.

  • by Tony T1,

    Tony T1 Tony T1 May 19, 2015 7:08 AM in response to Space-Rat
    Level 6 (9,249 points)
    Mac OS X
    May 19, 2015 7:08 AM in response to Space-Rat

    For portability to other users, see if the $HOME variable works: $HOME/Desktop/tool.py (or ~/Desktop/tool.py)

  • by sumox,

    sumox sumox Nov 5, 2015 1:09 PM in response to Tony T1
    Level 1 (20 points)
    Nov 5, 2015 1:09 PM in response to Tony T1

    I am really sorry, and maybe I am a little bit slow on this one. I made a video where you see that it is not working for me 'out of the automator box'.

    Any help would be really welcome. Here is the screen recording: http://youtu.be/HEqs-1U9WNU

    The image I want to use as watermark is attached.Survi-Letterhead-A4.png

  • by Tony T1,

    Tony T1 Tony T1 Nov 13, 2015 10:02 AM in response to sumox
    Level 6 (9,249 points)
    Mac OS X
    Nov 13, 2015 10:02 AM in response to sumox

    The Watermark PDF Documents Action is now fixed in OS X 10.11 (El Capitan)

  • by VikingOSX,

    VikingOSX VikingOSX Nov 13, 2015 11:12 AM in response to Tony T1
    Level 7 (21,056 points)
    Mac OS X
    Nov 13, 2015 11:12 AM in response to Tony T1

    It worked the first time that tried it in Automator, and then consistently failed afterward. This was just after I had upgraded to 10.11.1. Not much room for pilot error in Automator when it works, and then doesn't without changing any Automator settings. I may take a closer look at it over the weekend.

  • by crkdln1,

    crkdln1 crkdln1 Jan 18, 2016 5:19 AM in response to VikingOSX
    Level 1 (0 points)
    Jan 18, 2016 5:19 AM in response to VikingOSX

    I have the same problem. 

     

    I am creating a banner image file from metadata.  That all works well.  When the image file (png) is passed into the watermark routine, it does not work.  The log shows that the image file is being converted to a pdf before or during the watermark process.  Not sure if that is the problem but haven't found a way around it.  Any suggestions?

  • by VikingOSX,

    VikingOSX VikingOSX Jan 18, 2016 6:17 AM in response to crkdln1
    Level 7 (21,056 points)
    Mac OS X
    Jan 18, 2016 6:17 AM in response to crkdln1

    I just created an Automator watermark application. I used a .png (and .jpg) image for my watermark, and chose the Draw watermark over PDF option. A very low opacity (0.09) value was used. The workflow consisted of the following:

    1. Ask for Finder Items
    2. Watermark PDF Documents
    3. Copy Finder Items (to Desktop with overwrite)

     

    With this result:

    Screen Shot 2016-01-18 at 9.05.01 AM.jpg

    The only observation is that the watermark was placed correctly, but behind the text, not over it as selected in the watermark action. Also, the final PDF was not overwritten as requested, but made a generational foo 2.pdf name. Neither an issue for me. Tested on OS X 10.11.2.

  • by crkdln1,

    crkdln1 crkdln1 Jan 18, 2016 9:40 AM in response to VikingOSX
    Level 1 (0 points)
    Jan 18, 2016 9:40 AM in response to VikingOSX

    Thanks.

     

    This does work.  When I then add the Set PDF Metadata and Get PDF Metadata and create image steps it does not work.  If I run the Set PDF Metadata, Get PDF Metadata and create image as a separate workflow and then run your workflow, it does create a watermark.  I will run them as separate steps but would be nice to have it in one workflow

  • by benwiggy,

    benwiggy benwiggy Aug 12, 2016 3:41 AM in response to VikingOSX
    Level 4 (1,430 points)
    Mac OS X
    Aug 12, 2016 3:41 AM in response to VikingOSX

    Has anyone produced a python script to watermark a PDF with text, rather than an image?

     

    I've been looking at CoreText, but it seems ludicrously complex. I'm almost tempted to have python create a PostScript file with the text, convert it to PDF, and then add it to the existing page.

     

    (I'm looking at automatic page numbering for PDFs.)

  • by VikingOSX,

    VikingOSX VikingOSX Aug 12, 2016 8:14 AM in response to benwiggy
    Level 7 (21,056 points)
    Mac OS X
    Aug 12, 2016 8:14 AM in response to benwiggy

    Have you looked at PDFAnnotationStamp in PDFKit? The old PDF Annotation Editor source code has an example of applying a stamp. It uses a PDF for the stamp though, and code would need to be added for scaling and rotation of the stamp box.

     

    You could extend the tool.py in Automator's Watermark PDF Documents action with an additional command-line switch for -text, and the PDFAnnotationStamp functionality.

     

    I could not find any existing watermark/stamping examples that used text instead of images or PDF, after several Google searches.

  • by benwiggy,

    benwiggy benwiggy Aug 12, 2016 8:19 AM in response to VikingOSX
    Level 4 (1,430 points)
    Mac OS X
    Aug 12, 2016 8:19 AM in response to VikingOSX

    Thanks, I'll have a look at that. I've not found much via web searches either. Adding text to a Core Graphics context used to be easy, but Apple have deprecated all the easy APIs, in favour of CoreText, which is monstrous.

     

    I've produced python scripts to combine, rotate, add graphics, count pages, make booklet spreads, apply quartz filters, etc, etc to PDFs, all using CoreGraphics, and I'm determined to persevere with adding text!!!

     

    It can be done if you download some cross-patform python PDF library, but I don't really want to do that.

  • by Hiroto,

    Hiroto Hiroto Aug 12, 2016 7:19 PM in response to benwiggy
    Level 5 (7,306 points)
    Aug 12, 2016 7:19 PM in response to benwiggy

    Hello

     

    Here's sample pybojc code to draw text watermark using Core Text CTLine as a proof of concept.

     

     

    #!/usr/bin/python
    # coding: utf-8
    
    import math
    from Quartz.CoreGraphics import *
    from CoreText import *
    
    def drawWatermarkText(ctx, line, xOffset, yOffset, angle, scale, opacity):
        #   CGContextRef ctx
        #   CTLineRef line
        #   float xOffset, yOffset, angle ([degree]), scale, opacity ([0.0, 1.0])
        if line:
            rect = CTLineGetImageBounds(line, ctx)
            imageWidth = rect.size.width
            imageHeight = rect.size.height
            
            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)
            CGContextSetTextPosition(ctx, 0.0, 0.0);
            CTLineDraw(line, ctx);
            CGContextRestoreGState(ctx)
    
    # parameters
    ifile = 'input.pdf'
    ofile = 'output.pdf'
    text = 'Watermark Sample'
    xOffset, yOffset, angle, scale, opacity = 0.0, 400.0, 60.0, 2.0, 0.2
    
    # create CoreText line (CTLine)
    font = CTFontCreateWithName('Helvetica', 36.0, None)
    astr = CFAttributedStringCreate(kCFAllocatorDefault, text, { kCTFontAttributeName : font })
    line = CTLineCreateWithAttributedString(astr)
    
    # create output pdf context
    ourl = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, ofile, len(ofile), False)
    ctx = CGPDFContextCreateWithURL(ourl, None, None)
    
    # create input pdf document
    iurl = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, ifile, len(ifile), False)
    pdf = CGPDFDocumentCreateWithURL(iurl)
    
    if pdf:
        for i in range(0, CGPDFDocumentGetNumberOfPages(pdf)):
            page = CGPDFDocumentGetPage(pdf, i + 1)
            if page:
                mbox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
                if CGRectIsEmpty(mbox): mbox = None
                CGContextBeginPage(ctx, mbox)
                CGContextDrawPDFPage(ctx, page)
    
                # elementary test
                CGContextSetTextPosition(ctx, 10.0, 10.0)
                CTLineDraw(line, ctx)
    
                # using general function
                drawWatermarkText(ctx, line, xOffset, yOffset, angle, scale, opacity)
    
                CGContextEndPage(ctx)
        del pdf
    CGPDFContextClose(ctx)
    del ctx
    

     

     

     

    Briefly tested with pybojc 2.2b3 and python 2.6.1 under OS X 10.6.8.

     

    Good luck,

    H

  • by VikingOSX,

    VikingOSX VikingOSX Aug 12, 2016 10:41 PM in response to Hiroto
    Level 7 (21,056 points)
    Mac OS X
    Aug 12, 2016 10:41 PM in response to Hiroto

    Hiroto,

     

    Works without change on 10.11.6 with Python 2.7.10.

     

    Screen Shot 2016-08-13 at 1.40.04 AM.jpg

first Previous Page 3 of 4 last Next