Converting HTML to PDF

Outside of plain text, you can't get much more standard than HTML and PDF. They're pretty much agreed up by all major operating systems and the users who use them. FileMaker certainly supports embedding PDFs within container fields and HTML is quite easily integrated into Web Viewers. Using HTML within FileMaker is one of the most flexible ways to present information.

About the only thing you could wish for, (Yes, I'm sure you could wish for a lot of things) is any easy way to convert your HTML into a PDF. Here's the cool thing, it's already been done!

Using the open source tool wkhtmltopdf, you can populate your web viewer with whatever information you wish. Need a pivot table? Need more display control for your printed Invoices than FileMaker's native Layout mode? Need to integrate information from multiple sources out on the web? Need to just attach a simple PDF to an outbound email?

You've got the solution now. This video and the provided technique file will get you up and running within a very short period of time. With FileMaker 13 and its Perform Script on Server, this solution is bound to be one of your most powerful tools available!

AttachmentSize
ConvertHTML2PDF.zip163.55 KB

Comments

Matt, I don't think you mentioned whether we should install the 32-bit or 64-bit version of wkhtmltopdf. With either version, I get an error message, "The command parameters may be incorrect" and the script terminates.

In the Terminal or Command window you'll need to test the shell command directly. This is how you'll see what error output you're getting.

-- Matt Petrowsky - ISO FileMaker Magazine Editor

Error: Failed loading page http:///Volumes/Macintosh (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http:///Volumes/Macintosh (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http://hd/private/var/folders/wh/n7sz9tvj2dv256hydqp1pjhc0000gp/T/S10/co... (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: HostNotFoundError

Sorry, I had this code in there and I'll have to update the file I guess. It may depend on the version of wkhtmltopdf which is installed. If the file is local, you can always try file:// as your uri scheme. Otherwise wkhtmltopdf should work with a local file ref.

Also check to see if there are spaces in the path. If there are, then I may not have escaped for that. There is a parameter on the FileOSPath function I think which allows you to escape it.

-- Matt Petrowsky - ISO FileMaker Magazine Editor

Also, you mentioned pivot tables in the web viewer. I don't think you've ever done a technique file for that, have you? Is there any technique that you would recommend?

In order to generate data in a "pivoted" format, I would probably personally use ScriptMaster/Groovy in order to manipulate the data. Since I don't have a strong example to start from - not having done one in a long while, I don't have a technique file.

-- Matt Petrowsky - ISO FileMaker Magazine Editor

I think this relates to the problems above. In the script 'Create PDF' the 18th line should be 'BE_ExecuteSystemCommand'. But in my file it came up 'Function Missing'. I think that command has been replaced with 'BE_ExecuteShellCommand' so I typed that in. It was accepted but I am still getting the error message "The command parameters may be incorrect' and no PDF is generated.

JJ

First, BE_ExecuteShellCommand was deprecated in favor of BE_ExecuteSystemCommand. This means you may have an older version of the Base Elements plugin.

Second, the problem you may be having deals with spaces within paths. The shell interprets spaces as a separation between command parameters and flags. You'll need to single quote the path if there are spaces within.

I'll update the file to fix this.

Matt

-- Matt Petrowsky - ISO FileMaker Magazine Editor

I've added single quotes around the mac file paths. This will fix the problems.

-- Matt Petrowsky - ISO FileMaker Magazine Editor

Matt,
I can't tell you how excited about the possibilities with this. I really needed clients who connect via web-direct to be able to generate pdf's and print them locally. This seems to do the trick. Almost. I just installed a test version on my Mac Mini server. If I access it with filemaker pro everything works fine and the conversion works. If I access it through the web I get he following error message "External Data Source could not be opened. (Not Found)" when I acknowledge that message this dialogue appears "Insert from URL has been canceled. Do you wish to continue with this script?". Whether I cancel or continue my pdf fails to be generated. I have installed Base Elements plug in and wkhtmltopdf on the server itself. Any ideas what might be going wrong?

Chris A

Chris Andersson

Unfortunately, I've not worked with Web Direct and did not test with it. It sounds like it would require some testing/debugging to make it happen. Ultimately, it can likely be solved. With a plugin like either BaseElements or ScriptMaster on the server side, there's not much you can't do - it just takes some debugging.

-- Matt Petrowsky - ISO FileMaker Magazine Editor