Combine PDFs – Without Using FM Append

merge-pdf

Ever wanted to combine PDFs using FileMaker? Here’s a sample file that does the trick.

LDprototype_combinePDFs 002
[EDIT: Scroll down for new protytype.]

The sample file combines PDF files that are stored in containers. It uses 360 Works’ Scriptmaster to register external functions that work their magic on the PDF files. But let’s back up a bit.

When I first looked into this, I thought, “This should be easy.” Just use Append to existing PDF, an option in FileMaker’s Save Records as PDF script step.

Nuh-uh. The Append option only works if the PDF is generated from a FileMaker record. The Append option does not work for PDFs that are stored in containers.

So, I searched and discovered Scriptmaster, plus a custom jar library, which I think is called itext. Not sure, but I’ve heard that itext has licensing restrictions, which prohibit commercial use.

Anyway, I’m a novice when it comes to jar libraries. Others have commented that not all my script steps are necessary. Thing is, the prototype works. And until I see something better, I’m sticking with it.

Acknowledgements

1. 360 Works for their Scriptmaster awesomeness
2. Ocean West on FMForums for his/her MergePDF external function
3. John Renfrew for his help on the FileMaker Community Forum.
4. Matt Petrowsky for his FileOSPath custom function

Update
2015-12-16 – The prototype broke when ScriptMaster was updated. Or maybe it was the prototype. All I know is that the prototype appears to work OK using ScriptMaster v 4.32. Check it out…

LDprototype_combinePDFs 003

Update
2016-01-09 – The prototype now includes ScriptMaster v4.32 for Windows. Plus script steps that test for:
1. The presence of v4.32
2. The system platform (Windows or Mac)
3. The application architecture (Windows 32- or 64-bit)

Unfortunately, this latest version of the prototype was not tested on Windows. None of the prototypes were. So, I’m really curious if it even works.

Crossing my fingers…

LDprototype_combinePDFs 004

13 comments
theharrisonlee
theharrisonlee

does anyone have a version that works on windows as well ??


Please provide me with more information i am a total beginner at this and desperately need this function for my system as well


Thanks so much 

Bilbo28
Bilbo28

I have found the problem on Windows but don't know how to solve it.


When you use MergePDF groovy function, it opens the files parentRecord.pdf and Attachment.pdf in Filemaker to create the Assembled.pdf. The problem is that the groovy function does not close them after proceeding to the merge. It olny closes Assembled.pdf. Therefore parentRecord.pdf and Attachment.pdf are "writing or deleting locked" as long as you don't exit filemaker. This is why we can't create the following Attachment.pdf.


It shloud be a command in the groovy code to close those 2 files.

Bilbo28
Bilbo28

Actually the problem is different that the one I was suggesting. It seems that once a parentRecord.pdf, or attachment.pdf is already present, windows considers that this file is already opened with filemaker and does not let you work on it anymore. The solution could be creating different PDFs (ie. attachment1.pdf, attachment2.pdf, etc), and once done, merge them all together, before erasing them from the folder.

Bilbo28
Bilbo28

Hi,


Very nice job!


Unfortunately prototype 4 doesn't work on windows. It is suspect a path problem. I think that something like "filewin:/c/....." is needed. I'm trying to figure it out.


On my work database I made an opening script that detects OS platform and sets up a global variable that defines a path which will be used on the computer. This way, I my database works as well on windows as on mac. With this method I don't need to define paths in all my scripts. 


Best


Andrei

eswanborg
eswanborg

Any updates to this great solution?  In v14 with ScriptMaster 4.42 the iText functions seem to fail.  Would love to get this same functionality working again in the latest FMP version too.  Thanks so much for sharing this.

ldicroce
ldicroce moderator


@eswanborg


Yes, recent version(s) of ScriptMaster appear to break the prototype. Version 4.32 works. It's included in the latest prototype — LDprototype_combinePDFs 003.fmp12 — which will be posted shortly.


Thank you for your interest,

=L=  

eswanborg
eswanborg

@ldicroce @eswanborg Update works great with the ScriptMaster you've included.  Thank you!  It still does not seem to work with SM v4.42 which is the latest version from 360Works.  If I can find v4.32 for Windows I'll be all set on both platforms.  If you have any thoughts on it working with v4.42 as well i'd appreciate any info that way as well.  But again - thanks for the great template and process - it's a huge help to us.

ldicroce
ldicroce moderator

@eswanborg @ldicroce I updated the prototype. Hopefully  LDprototype_combinePDFs 004 works for you.


If you could let me know, one way or another, that would be great.


Thanks,

=L=

eswanborg
eswanborg

I will check it on Monday when I'm back in the office. Thanks so much for making the updates! Great solution for our needs! Really appreciate your knowledge and support.

eswanborg
eswanborg

@ldicroce @eswanborg Very happy to report that it is working on Windows!  Thanks so much.


One note though, I had to again find the FM Forums thread and hand edit the RegisterGroovy "MergePDF" variable.  There are lines that need to be commented out in order for it to work.  

//  Document document = null;¶

//  PdfCopy  writer = null;¶


Those are still in your example and do not seem to be necessary for either Mac or PC users.


Again - thank you so much!!

eswanborg
eswanborg

@ldicroce And i should add - I also added this line as the other forum suggested too (same variable):


while (f < inFiles.length) {¶

    // Create a reader for the next document¶

    reader = new PdfReader(inFiles[f]);¶