Software tips, techniques, and news.
By Weihao Ding Posted on March 3rd, 2015 in Free Downloads, FileMaker
Interactive containers were introduced in FileMaker 12 to enable users to view the content of PDFs, audit files and video files directly in FileMaker Pro. A common use case for interactive containers is to use them to scroll through pages of a PDF, zoom in and out, and copy text, all without having to leave FileMaker Pro.
The problem, however, is that interactive containers aren’t fully supported in FileMaker Go. For example, PDFs only show their first page and to view the full content the user will have to tap on the container field and choose “View” to open up the PDF file in a PDF reader app.
In some use cases, the user may want to be able to do data entry and view the content of a PDF file at the same time. In FileMaker Go they will have to switch back and forth which could harm the usability and user experience. One workaround for this problem is to display the PDF in a web viewer instead of a container field. This can be done natively in FileMaker Go.
Web viewer, as a browser inside FileMaker, not only can show web pages, but it can show files on your device as well. Hence, we can display PDFs on our devices in a web viewer. The URL we use to retrieve the PDF file is called File URL
File URL usually takes the form of
file://host/path
If file resides on a different machine, you should specify the fully qualified domain name as host. Otherwise you can omit the host part and it is taken to be “localhost”.
Path is the directory path to the file you want to retrieve. It takes the form of directory/directory/…/file_name.
As an example, let’s say I want to retrieve a txt file named “foo.txt” stored in folder folder1/folder2/ on my device. Since the file is local, I would omit the host part. Path would be folder1/folder2/foo.txt. Put them together, the file URL is “file:///folder1/folder2/foo.txt”.
Note that I used three “/“ in between “file:” and “folder1/…”. The reason is, technically speaking, when host is omitted, you should keep the “/“ after it. However many interpreters can handle situations where the third “/“ got omitted together with the host. On the iPad we can use either form. Both “file:///folder1/folder2/foo.txt” (three slashes) and “file://folder1/folder2/foo.txt” (two slashes) are recognizable on iPad.
So in order to display the PDF in a web viewer, all we need to do is to export the PDF and use the path to the exported file as a file URL in the web viewer.
1. Since we will display the PDF in a web viewer, you will need to add a web viewer to your layout. Double click the web viewer in Layout mode to set the Web Address for the web viewer to be a global variable that’s going to store the file URL.
2. Then you need to name your web viewer using the inspector.
3. Build a script to export the PDF file and display it in web viewer. Here is an example:
#PURPOSE: To export PDF file to temp folder and set web viewer #Start, End Context: Sample File, Sample File #2015-02-16, WD, Created script. # # #----------INITIALIZE----------- Allow User Abort [ Off ] Set Error Capture [ On ] Freeze Window # # #----------HANDLE UI ERRORS----------- Perform Script [ "UI - Halt If Not in Browse Mode" ] Perform Script [ "UI - Halt If No Records in Found Set" ] Perform Script [ "UI - Commit Record and Halt If Invalid" ] # # #----------POPULATE PARAMETERS, VARIABLES, CONSTANTS----------- #Constants Set Variable [ $TASK_NAME; Value:"View PDF in Web Viewer" ] Set Variable [ $ERROR_MSG_SUMMARY; Value:"Unable to view field PDF in web viewer. " ] Set Variable [ $CONTAINER_PDF; Value:"CONTAINER: PDF" ] Set Variable [ $WEB_VIEWER_PDF; Value:"WEB VIEWER: PDF" ] # # #----------MAKE SURE THERE IS A PDF TO VIEW----------- If [ IsEmpty ( Sample File::pdf ) ] Show Custom Dialog [ Title: "Sorry"; Message: $ERROR_MSG_SUMMARY & "There is no PDF to view. Please insert a PDF file to the container field."; Default Button: "OK", Commit: "Yes" ] Go to Object [ ] Insert PDF [ ] Exit Script [ Result: 0 ] End If # # #----------EXPORT PDF TO TEMP FOLDER----------- #Copy pdf to temp container Set Field [ Sample File::zgcTempExport; Sample File::pdf ] #Grab temp path Set Variable [ $filePath; Value:Get( TemporaryPath ) & GetContainerAttribute( Sample File::zgcTempExport ; "filename" ) ] #Generate FM file path Set Variable [ $filePathFM; Value:"filemac:" & $filePath ] #Export to temp path Export Field Contents [ Sample File::zgcTempExport; "$filePathFM" ] #Error trap export Set Variable [ $lastError; Value:Get( LastError ) ] If [ $lastError ≠ 0 ] Go to Layout [ original layout ] Show Custom Dialog [ Title: $TASK_NAME; Message: $ERROR_MSG_SUMMARY & "Could not export PDF to temp folder." & "¶¶" & "ERROR: " & fmErrorMessage( $lastError ); Default Button: "OK", Commit: "Yes" ] Exit Script [ Result: 0 ] End If #Generate file URL Set Variable [ $$fileURL; Value:"file:/" & $filePath ] # # #----------SET WEB VIEWER----------- #Go to target layout Go to Layout [ "Example" ] #Set web viewer Set Web Viewer [ Object Name: $WEB_VIEWER_PDF; Action: Reset ] Go to Object [ Object Name: $WEB_VIEWER_PDF ] # # #----------WRAP UP----------- Exit Script [ Result: 1 ]Example Script Breakdown
#Copy pdf to temp container Set Field [ Sample File::zgcTempExport; Sample File::pdf ]
#Grab temp path Set Variable [ $filePath; Value:Get( TemporaryPath ) & GetContainerAttribute ( Sample File::zgcTempExport ; "filename" ) ]
#Generate FM file path Set Variable [ $filePathFM; Value:"filemac:" & $filePath ]
#Export to temp path Export Field Contents [ Sample File::zgcTempExport; “$filePathFM” ] # #Error trap export Set Variable [ $lastError; Value:Get( LastError ) ] If [ $lastError ≠ 0 ] Go to Layout [ original layout ] Show Custom Dialog [ Title: $TASK_NAME; Message: $ERROR_MSG_SUMMARY & "Could not export PDF to temp folder." & "¶¶" & "ERROR: " & fmErrorMessage( $lastError ); Default Button: “OK”, Commit: “Yes” ] Exit Script [ Result: 0 ] End If
#Generate file URL Set Variable [ $$fileURL; Value:"file:/" & $filePath ]
#Go to target layout Go to Layout [ “Example” ] # #Set web viewer Set Web Viewer [ Object Name: $WEB_VIEWER_PDF; Action: Reset ] Go to Object [ Object Name: $WEB_VIEWER_PDF ]
By combining web viewers and file URLs you can show the contents of a PDF in FileMaker Go that works in a similar way as interactive containers in FileMaker Pro. This will expand the use cases for FileMaker Go solutions and enhance the usability when users need to do data entry and view the content of a PDF at the same time.
Did you know we are an authorized reseller for FileMaker Licensing?
Contact us to discuss upgrading your FileMaker software.
Weihao is a FileMaker Certified Developer, a creative thinker and a great communicator. He thrives on utilizing his keen technical skillset to solve problems in a manner that will make a positive difference for customers. His passion and drive to demonstrate excellence is evident in all of his work.