Page 1 of 1

combine a new pdf with an existing pdf

PostPosted: Tue Apr 10, 2012 12:17 am
by ajoyce
I've been trying to use Hazel to watch a folder and when a file containing a specific word (like "statement") comes into that folder then action is taken on it.
I want Hazel to get an existing PDF file and combine the newly created PDF with it so the newly created PDF is at the end of the existing PDF. Then I want to delete the previous newly created PDF and have the combined file replace the previous existing PDF file.

I've been trying to make an automator workflow to do this but I can't figure out how to get the newly found PDF file to pass into the workflow.
This is what I have:
Hazel's rules are:
If name contains "statement"
Run automator workflow "combine pdfs"

The automator workflow is:
Get Specified Finder Items (here is where I get the existing file "2012 Statements" and "newly created pdf "Statement"
Combine PDF Pages (appending pages)
Move Finder Items to "Bank" (with "replace existing files" unchecked)
Rename PDF documents (with "replace existing files" unchecked)
Name Single Item with "Basename only" to "New File" (I want to replace the existing file "2012 Statements" but it the automator workflow won't do that and I haven't been able to get Hazel to do it either.)

So, I've got the combined PDF document, now called "New File" but I need to delete the old "2012 Statements" file and change the "New File" to say "2012 Statements"
And I want Hazel to now delete the file in the folder she's watching called "Statement" so that it's ready for the next time a file like that is created.

Any ideas . . . anyone? . . . anyone?

Re: combine a new pdf with an existing pdf

PostPosted: Tue Apr 10, 2012 11:31 am
by Mr_Noodle
When using Automator workflows, you need to make them "open-ended". That means that the first action in the workflow must accept a file as input as Hazel needs to transmit that to the workflow. In your case, get rid of that first action.

Re: combine a new pdf with an existing pdf

PostPosted: Tue Apr 10, 2012 12:15 pm
by ajoyce
Thanks for the response. It's appreciated.

I take it that you are referring to the first step I had in the automator workflow, where it specifies the files to combine. However, if I don't specify the existing pdf, then the file that Hazel is looking at won't have any other file with which to combine, because the "combine PDF pages" step has to have either two or more files with which to work.

Re: combine a new pdf with an existing pdf

PostPosted: Tue Apr 10, 2012 4:49 pm
by Mr_Noodle
Ah, I see. I don't know if it exists but you need some sort of Automator action that takes in a file input and adds it to a list of files which you specify and then passes those on to the next action. Maybe google around to see if someone has written an Automator action for this.

Alternatively, see if there's an AppleScript to get Preview to do this (I figure it should be doable since Preview is providing the Automator action).

Re: combine a new pdf with an existing pdf

PostPosted: Fri Apr 26, 2013 5:23 am
by Jamixton
Thanks a lot man,
i like this solution better than what ive been using so far
( http://combinepdf.cc/ ) which was quite annoying.
so cheers man really liking hazel :)

pete

Re: combine a new pdf with an existing pdf

PostPosted: Fri Sep 26, 2014 11:27 am
by AdamRoxby
Hello there.

I would like to do something just like this and was wondering if you ever got it to work?

Regards

Re: combine a new pdf with an existing pdf

PostPosted: Sun Apr 14, 2019 5:11 pm
by Robert
Actually me too, does someone know how to accomplish that? I cannot find a solution with automator... :?:

Re: combine a new pdf with an existing pdf

PostPosted: Mon Apr 15, 2019 6:09 pm
by Robert
For anyone who comes here, searching for a solution. Vidman2019 helped me on macuser.de to find a solution with the shell program qpdf: https://www.macuser.de/threads/pdf-dateien-mit-gleichem-namen-zusammenfuehren.823125/#post-10158335

The code I use now is:
Code: Select all
for f in $1; do qpdf --empty "${f%.pdf}-joined.pdf" --pages "${f%.pdf}"*.pdf --; done


And the Hazel rule is set up like this:
Image

Re: combine a new pdf with an existing pdf

PostPosted: Sat Jan 15, 2022 5:10 pm
by brudderman
This looks like it could possibly be adapted to do what I want to do, but I need some help in working out the details. Here's my current workflow:

1. Every month I download statements for several bank accounts and credit cards and have Hazel set up to recognize (in the Downloads folder) which institution is involved so that statement can be renamed and moved to the proper folder. That has worked well.

2 At the end of each year I have been manually combining all the individually monthly statements in to a single file for that year and then deleting the individual monthlies so that I end up with just a yearly file. Again, this is easy.

What I would like to do is automate the #2 step in this sort of way:

1. As monthly statements are copied into the proper folder, I want to a) append them month by month into an ever-growing yearly file and 2) then delete the monthly file just imported and appended.

My monthly files are now named in this sort of way, but I can easily change the names if needed: Chase 2021-01, Chase 2021, Chase-02, etc. In this case, the annual file would be named Chase 2021. But that's flexible also.

I hope this explanation is clear, and any pointers toward getting it implemented, if it's even possible, would sure be appreciated. I have studied the QPDF documents and the discussion on the MacUser.de site but am in over my head.

Thanks!

Re: combine a new pdf with an existing pdf

PostPosted: Thu Sep 22, 2022 3:09 am
by slyfox
Robert wrote:For anyone who comes here, searching for a solution. Vidman2019 helped me on macuser.de to find a solution with the shell program qpdf: https://www.macuser.de/threads/pdf-dateien-mit-gleichem-namen-zusammenfuehren.823125/#post-10158335

The code I use now is:
Code: Select all
for f in $1; do qpdf --empty "${f%.pdf}-joined.pdf" --pages "${f%.pdf}"*.pdf --; done


And the Hazel rule is set up like this:
Image


Does this still work? I tried multiple versions of the script and I keep getting an error. However, running this command in termanl work.

Code: Select all
# for f in $1; do qpdf --empty "${f%.pdf}-joined.pdf" --pages "${f%.pdf}"*.pdf --; done

# for f in $1; do qpdf --empty --pages "${f%.pdf}"*.pdf -- merged.pdf; done

# for f in $1; do qpdf --empty --pages "${f%.pdf}"*.pdf '--' output.pdf; done


Re: combine a new pdf with an existing pdf

PostPosted: Thu Sep 22, 2022 9:13 am
by Mr_Noodle
You shouldn't assume any environmental variables (including PATH) are set when running a script in Hazel. You should set those yourself and also use full paths to any programs/files you reference.

Re: combine a new pdf with an existing pdf

PostPosted: Fri Oct 14, 2022 3:16 am
by slyfox
I have not been able to figure out a working version of the script. Hope the Hazel can introduce native PDF merge function.

Re: combine a new pdf with an existing pdf

PostPosted: Fri Oct 14, 2022 9:14 am
by Mr_Noodle
Did you follow my advice? For example, you don't specify a full path for qpdf in your script. If you don't do that, then you need to declare the proper path in your sript.

Re: combine a new pdf with an existing pdf

PostPosted: Wed Jan 11, 2023 4:11 pm
by slyfox
Mr_Noodle wrote:Did you follow my advice? For example, you don't specify a full path for qpdf in your script. If you don't do that, then you need to declare the proper path in your sript.


I don't know how to script so I gave up on that idea. Instead, I came up with a Siri Shortcut to achieve what I want.

The only problem is that I manually have to run it once per week. Passing folder as input from Hazel does not seem to work.

Link: https://www.icloud.com/shortcuts/9e8674 ... b4e427abfb

Re: combine a new pdf with an existing pdf

PostPosted: Thu Jan 12, 2023 10:45 am
by Mr_Noodle
Can you post the rule?