Site icon FileMakerHacks

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:

(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.

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.

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

…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.

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…

…which is performed via AppleScript:

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

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

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.

Exit mobile version