Level: Intermediate, Version: FM 12 or later

Extending FileMaker’s PDF capabilities with PDFtk Server

One of the great things about FileMaker is its ability to not only create a PDF from a single “print job”, but to take a series of separate jobs and concatenate them into a PDF via the “Save Records As PDF [append]” feature, like so:

7-13-2014 2-20-22 PM

(The above code comes from PDF Catalog with Table of Contents, which appeared on this site last year.) Basically, anything you can print from FileMaker, you can instead output as a PDF.

But what if you have existing multipage PDFs stored in container fields, and want to combine them into a larger PDF? This is something FileMaker cannot do natively. Recently I had a need to do just this, and happened to mention it to Jon Rosen at ErgoSoma, who suggested I take a look at a cross-platform utility called PDFtk Server.

7-15-2014 8-23-53 AM

It was a good suggestion, and today’s demo, pdftk server demo, requires PDFtk Server to function. (You can get a sense of how powerful this utility is by taking a look at the PDFtk Server Manual. Note that PDFtk Server is free to download and use, but  if you distribute it with a commercial product will require the purchase of a license.)

Note also that while most of the screen shots in today’s article come from the Windows platform, the technique we’re about to look at works equally well on both Mac and PC.

7-15-2014 8-33-13 AM

In the demo we have four PDFs stored in a “pdfstorage” table like so…

7-15-2014 8-37-00 AM

…and we want to combine the individual PDFs into a single large PDF.

The basic recipe is spelled out on the PDFtk Server Examples page.

7-15-2014 8-46-50 AM

And it turns out, as you’ll see in the demo, the “cat” can be eliminated from the above command, with no negative consequence (to the best of my knowledge, at any rate).

In a nutshell, the demo will…

  1. Prompt the user to name the output PDF
  2. Export the existing PDFs to the user’s FileMaker Temp folder
  3. Name each one sequentially like so: “pdf_1.pdf”, “pdf_2.pdf”, etc.
  4. Build a list of the full file path + name for each of these files
  5. Allow PDFtk to work its magic (create the output PDF on the user’s desktop)
  6. Open it

…using, where appropriate, Perform AppleScript on the Mac side, and Send Event on the Windows side, and at this point I would like to say thank you to Bruce Robertson for writing the majority of the AppleScript code used in this demo.

The Mac version assembles the code into this variable…

script_final

…which is performed via AppleScript:

7-15-2014 1-17-05 PM

Meanwhile the Windows version assembles this variable (it’s actually all one line, but shown here wrapped for readability):

7-15-2014 1-22-51 PM

…and then executes it like so (the second “Send Event” opens the PDF after it’s been created):

7-15-2014 1-24-35 PM

Also, if you’re wondering about the escaped quotation marks they prevent potentially problematical characters from causing difficulties on the Windows platform, e.g., the output file path + name must be quoted if it contains a space or parentheses.

Finally, at the risk of stating the obvious… this technique can just as easily be applied to a combination of native FileMaker generated outputs and existing stored PDFs. In the demo I chose to use only stored PDFs to keep the example simple.

13 thoughts on “Extending FileMaker’s PDF capabilities with PDFtk Server”

  1. Hi Kevin,
    We’ve used this: http://pdfmerger.codeplex.com/ and SmartPill. It’s the bomb. All you need to do is include their PHP in the extensions folder and point to it while collating your PDF’s. You specify the path and the file to append to. We have created document “stitchers” that are very flexible and you can get fancy and just have containers in line items in FM that folks can drag and drop PDF’s to that you just output to disk to stitch together. This way FM is also controlling the sequence and the storage.

    Best- J. Colibri Solutions LLC NY

  2. hi Kevin, another great article as always – and a timely one for me as I have just dealt with this exact issue for a client where we needed to combine multiple PDF’s into a single PDF. The route I ended up going with was to use 360works scriptmaster, combined with some Groovy code from Matt Petrowskys old 2008 article – “http://www.filemakermagazine.com/videos/combining-pdfs-the-blazing-fast-method” worked okay, didn’t give much performance gain which is why we tried it over native FileMaker append, but it allowed us to arbitrarily append page numbers from the various PDF’s. I might also try this method to see if the append action is faster. thanks !

    1. Hi Daniel,

      Thanks for taking the time to comment. I would be interested in hearing if you notice any speed difference. Not quite sure what you mean by “arbitrarily append page numbers”. Would you mind elaborating?

      TIA,
      Kevin

      1. In this particular example, rather than put all of PDF B onto the end of PDF A (and so on), the nature of the resulting PDF required that we pick pages out of order from the various PDF’s. The end PDF was to be printed as A3 booklet, and as such, page numbers were to be pages 1 and 2 from all the PDFs to begin with, and then pages 3 and 4 after all of those from all the PDFs (in reverse order), such that when the final PDF was printed as A3 booklet we ended up with proper booklets 1 per PDF…

        Through using loops in the code we were able to append pages 1 and 2 of PDF A, followed by 1 and 2 of PDF b…. and then 3-4 of PDF B, followed by 3-4 of PDF a…. so rather than a straight up append, “arbitrary” insomuch as it could take a given page from a given PDF and append only that page…

        I’ll let you know how I get on with performance testing, cheers!

        1. Thanks for the expanded explanation Daniel. PDFtk has the ability to do this as well, though I did not show it in my example.

          Looking forward to your test results.

  3. I am using Windows 7 and FileMaker Pro Advanced 14. When I downloaded and opened this demo file, the demo would not work. While stepping through the script with the debugger, I found that a File Error 100 was being displayed at line 62 – the Send Event script step. After much troubleshooting, the problem remained. In a last ditch effort, I rebuilt that script step – simply reviewed all of the setting and saved it and then the script – I made no changes to what was originally coded. The script then ran fine. With the demo working, I imported the script into my file and made the appropriate changes, again – the problem occurred, and the fix worked. This may be a glitch related to the “Send Event” script step, or perhaps it’s unique to my computer. I haven’t tried moving the file to another machine, or opening the demo on another machine so I can’t provide any more details, but should you bump into this issue this fix worked for me.

    As a side note, I had 210 records which all had PDFs which I needed to combine into a single file for printing. This solution produce the 410 page PDF quickly and flawlessly.

    Thanks Kevin – as usual another great tool for my toolbox.

    1. Good to know. Thanks for taking the time to write that up, Jeff. You may save someone else a lot of grief.

  4. Thank you so much for this! I also came across an Error 100 related to the file not being saved on line 62. Mine however was related to the desktop path. I am a Mac user that develops on a Parallels VM as most of my users are PC users. It did not like the Get to the what amounts to a shared desktop. Specified something else and boom no more 100.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.