Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • stata shell pdftk on mac

    Hi Statalist,

    I don't know enough to be sure, but please let me know if this isn't the right place to be asking this question.

    I am trying to merge a series of pdfs saved to a single folder using a tool called pdftk (https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) that runs from the terminal. To do this, I would like to include in my .do file the following shell commands:

    Code:
    loc save "/Users/my name/Desktop" 
    loc title "title of my document" 
    
    ...
    putpdf save "`save'/part1_name1.pdf", replace
    ...
    putpdf save "`save'/partN_nameN.pdf", replace
    
    
            if c(os) == "MacOSX" {
                !cd "`save'" && pdftk part*.pdf cat output "`title'.pdf"
                !cd "`save'" && rm part*.pdf
            }
            
            if c(os) == "Windows"  {
                !cd "`save'" && pdftk.exe part*.pdf cat output "`title'.pdf"
                !cd "`save'" && del part*.pdf
            }
                
           di "See `save'/`title'.pdf"
    This logic functions fine on Windows, but not on Mac. Specifically, when I arrive at the first shell command on a MacOSX, it returns "/bin/bash: pdftk: command not found" while the second shell command successfully erases all files `save'/part*.pdf. This made me think that I had not correctly configured the pdftk package on my Mac, but when I run the same set of commands directly from terminal on Mac, I can successfully merge and erase the pdfs as intended.

    Any guidance would be much appreciated.

    I am using STATA 15.1 and have macOS High Sierra version 10.13.6. Additionally, I have pdftk 2.02.

    Thanks in advance.

    Julian

  • #2
    This is a fine place to ask this question.

    I'm using the same setup as you, except I've moved on to macOS Mojave 10.14.2. Where we differ is that my default shell is tcsh rather than bash. Despite those differences, I think the same principles will hold.

    The issue seems to me to be that Stata does not invoke the shell as a login shell (does not use the "-l" option) and consequently the environment variable PATH is not set to anything you may have customized, and you instead get that shell's default PATH. Here's some output copied from my terminal window:
    Code:
    lisowskiw 1% echo $SHELL $PATH
    /bin/tcsh /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/MacGPG2/bin:/Users/lisowskiw/bin:/Applications/Stata/StataSE.app/Contents/MacOS
    lisowskiw 2%
    and here the output of the same command when run from within Stata:
    Code:
    . shell echo \$SHELL \$PATH
    
    /bin/tcsh /usr/bin:/bin:/usr/sbin:/sbin:/Users/lisowskiw/bin:/Applications/Stata/StataSE.app/Contents/MacOS
    So my guess is that in the MacOSX branch you need to give the full path to pdftk, or alternatively install it into a directory that is in the default PATH.

    Let us know if this solves your problem or not, thanks in advance.

    Comment


    • #3
      Hi William,

      Thanks very much for figuring all that out for me. The problem was indeed with my PATH variable -- running "echo $SHELL $PATH" reminded me that a few months ago I (stupidly?) changed the default. Though this doesn't return $PATH to its default, the following work around functions for me:

      Code:
       if c(os) == "MacOSX" {            
      
      !source /etc/profile && cd "`save'" && pdftk part*.pdf cat output "`title'.pdf"            
      !cd "`save'" && rm part*.pdf        
      
      }

      Comment


      • #4
        Good workaround, not one I thought of, and definitely more general than just using a full path to the pdftk executable. I hope to remember this in the future if I ever run into the problem. Thanks for sharing it.

        I will add that it was not stupid to change the PATH variable. You shouldn't avoid solid UNIX practices just because shelling out from Stata doesn't work as well as you and I would hope.

        Comment

        Working...
        X