« Thanksgiving Sale | Home | All about EndOfLine »

Save PDF to container in FileMaker

You may know FileMaker has the possibility to save records as PDF to a file. From time to time clients ask how to script to get the PDF into a record. We like to show you a sample script on what you can do and run this either on server (e.g. triggered from FileMaker Go) or on client. 

The script takes the record ID as parameter. We switched to the target layout and the correct record based on the record ID to setup the context. That's important as on a server we need to setup context ourselves. On a client you can skip this, if you know you are on the right record already. We build a file path for the temporary PDF document named "temp.pdf". On a server with multiple scripts running at the same time, you may better use temp folder or put a new unique UUID in the file name. 

Next we ask FileMaker to save current record as PDF to the given file path. If that works and we don't get an error reported, we can use the Container.ReadFile function to read the PDF file. Please note, that we have different file path in either FileMaker style or native OS style for the plugin. If the read operation works, we can try to put the new PDF document into the field. If that works, we can commit changes to disk and report back OK. Per convention we have the script either return OK or an error message, so the calling script can check the result.

Here is the final script:

# pass record ID as parameter

Set Variable [ $RecordID ; Value: Get(ScriptParameter) ] 

# go to target layout

Go to Layout [ “Contact Details” (Contacts) ; Animation: None ]

If [ Get(LastError) ≠ 0 ] 

Exit Script [ Text Result: "Failed to go to layout!" ] 

End If

Go to Record/Request/Page [ With dialog: Off ; $RecordID ]

If [ Get(LastError) ≠ 0 ] 

Exit Script [ Text Result: "Failed to go to record " & $recordID ] 

End If

# build a path for FileMaker

Set Variable [ $name ; Value: "temp.pdf" ] 

Set Variable [ $path ; Value: Get(DocumentsPath) & $name ] 

Set Variable [ $NativePath ; Value: MBS( "Path.FileMakerPathToNativePath"; $path ) ] 

# Let FileMaker save records

Save Records as PDF [ Restore ; With dialog: Off ; “$path” ; Current record ; Create folders: Off ] 

If [ Get(LastError) = 0 ] 

# Read result PDF

Set Variable [ $PDF ; Value: MBS( "Container.ReadFile"; $NativePath) ] 

If [ MBS("ISError") = 0 ] 

# Put in container

Set Field [ Contacts::PDFFile ; $PDF ] 

If [ Get(LastError) ≠ 0 ] 

Exit Script [ Text Result: "Failed to assign PDF field" ] 

End If

Commit Records/Requests [ With dialog: Off ] 

Exit Script [ Text Result: "OK" ] 


Exit Script [ Text Result: "Failed to read PDF document: " & $PDF ] 

End If


Exit Script [ Text Result: "Failed to create PDF document: " & Get(LastExternalErrorDetail) ] 

End If

You may call the script above passing RecordID like this:

Perform Script [ Specified: From list ; “save pdf” ; Parameter: Get(RecordID) ]

Please don't hesitate to contact us with questions.  

27 11 20 - 14:34