« ZUGFeRD Update | Home | MBS FileMaker Advent … »

MBS FileMaker Advent calendar - Door 4 - Add GTC with DynaPDF

candy cane Monkeybread Monkey as an elf candy cane
Door 4 - Add GTC with DynaPDF

Today I would like to show you a tool that you can use in FileMaker with the MBS FileMaker Plugin: DynaPDF. With DynaPDF you have the possibility to create, edit, merge, analyze or sign PDF documents in FileMaker. DynaPDF offers you many possibilities around the topic of PDF.

Our monkey also wants to use one of these functionalities today. Because today we want to build the possibility to attach user terms and conditions to a PDF document. The user terms and conditions always remain the same and can be attached to any outgoing document, e.g. an invoice. I will show you how this works today in this door.

First we have to initialize DynaPDF, which means we have to tell FileMaker where the DynaPDF function library is located. The files are automatically delivered with the download of the plugin and can be found in Examples > DynaPDF. The files have the extension dll and dylib. Which of the files you need depends on your operating system. If you want to use DynaPDF on a Mac, you will need the path to the dynapdf.dylib file. If we are working with Windows, it also depends on whether we are working with a 32 or 64 bit version. If you are using FileMaker 19 or higher, this decision is easy, as only a 64 system can be used here. In any case, the library has the extension .dll. If you are not sure, just put both dll in the same folder and the plugin will grab the file it needs. The next piece of information we need is the DynaPDF license key. If you want to use DynaPDF in your solutions, you need an appropriate DynaPDF license and an MBS FileMaker Plugin license.

There are four different DynaPDF licenses. Which license you need depends mainly on which DynaPDF functions you use. If you need more information, please visit our website to find out which license is right for you. However, we do not necessarily need a license to try it out, but need to live with the watermark. After we have determined the path to the libraries and the license key, if available, we can now call the DynaPDF.Initialize function. In the parameters we first enter the path to the library and then the license key. If we do not yet have a license key, we pass an empty string here.

Nice to know:
If you want to test whether a specific license is sufficient for your solution, you can also enter the name of the license instead of the license key. If the license is not sufficient, an error is displayed.

In our examples, we always use the InitDynaPDF script for initialization. This searches for the library in the same folder where our database is located. You are welcome to copy this script for your projects. DynaPDF must be initialized before the first call of another DynaPDF function. It can be practical if we have a function that checks whether this has already been done. We can use the DynaPDF.IsInitialized function for this.

After this has happened, we can start our work. We created the portal yesterday and can now save files for a customer there. In this portal we would now like to have a button for each entry which is a PDF that we can use to attach the terms and conditions that are stored in a global variable $$GTC. These should then be transferred to our portal as a new entry. The name of the file is then: original name with GTC.

First we extend our Script Data. This is because we want to store the PDF file with our terms and conditions in our data table and then write it to the $$GTC variable. We therefore add this line to the script:

Set Variable [ $$GTC ; Value: Data::GTC ]

Now we want to create the script that will later be called behind a button in the portal. We have already seen that we have to make sure that DynaPDF has been initialized before using it for the first time.

We want to use DynaPDF here, so we first ask whether it is already initialized and if this is not the case, then we run the InitDynaPDF script that we have copied into our project.

Now we need to create a working environment for the PDF. To do this, we call the DynaPDF.New function. This then gives us a reference number with which we can continue working. In order to be able to import the file in the portal into our working environment, we first need to open it. As it is located in a container, we use the DynaPDF.OpenPDFFromContainer function and specify the reference and the container in which the file is located. If you want to work with files that are stored in a location on your computer's disk, use DynaPDF.OpenPDFFromFile. After the file has been opened, we now need to import the document into the working environment. We use the DynaPDF.ImportPDFFile function for this. As this is the first document in the working environment, we only need to specify the reference in the parameters. If required, you can also specify where the document should be inserted. We can also see this in the second document. If required, you can also specify scaling factors for x and y. The function returns how many pages have been imported into the working environment. We save this information because we need it for the second import. Now we do almost the same with the second file. First we open it again with DynaPDF.OpenPDFFromContainer from the variable that contains the container value and then we import it with DynaPDF.ImportPDFFile. Here we specify the reference as before, but now we need the page number where we want to insert the first page of this document. So we take the page number of the first document and add 1 to it to get this page. We want to save the PDF from our working environment. To do this, we use the DynaPDF.Save function. We enter our reference in the parameters, as well as the new file name, which we have to put together before we can save it.

To do this, we first retrieve the old file name from the field. As this still has a .pdf extension, we have to trim this extension. We first determine the length of the file name and subtract 4 characters from it. Then we pass the file name to the Left function, which outputs the length-4 characters of the file name. So we get the file name without the ending. We then append "with GTC.pdf" to this string so that we have the desired file name.

Now we want to write the new file to our database. To do this, we store the giftee's primary key in a variable and open a new window using the Files layout. Here we create a new entry and write the appropriate information in the fields. After we have closed the window again and released the DynaPDF reference in memory with DynaPDF.ReleaseAll, we give the portal a refresh so that the new file is also displayed directly.

Here you can find the script for today's implementation:

If [ MBS("DynaPDF.IsInitialized") = False ]
	Perform Script [ Specified: From list ; "InitDynaPDF" ; Parameter:    ]
End If
Set Variable [ $pdf ; Value: MBS("DynaPDF.New") ]
Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; Files::File) ]
Set Variable [ $r ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf) ]
Set Variable [ $pages ; Value: $r ]
Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; $$GTC) ]
Set Variable [ $r ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf; $pages+1) ]
Set Variable [ $Length ; Value: Length ( Files::File_Name )-4 ]
Set Variable [ $FileName ; Value: Left ( Files::File_Name ;$Length ) & " with GTC.pdf" ]
Set Variable [ $NewFile ; Value: MBS("DynaPDF.Save"; $pdf; $FileName;) ]
Set Variable [ $PK ; Value: Giftee::PrimaryKey ]
New Window [ Style: Document ; Name: "NewFile" ; Using layout: "Files" (Files) ]
New Record/Request
Set Field [ Files::File_Typ ; "pdf" ]
Set Field [ Files::File_Name ; $FileName ]
Set Field [ Files::File ; $NewFile ]
Set Field [ Files::ForeignKey ; $PK ]
Close Window [ Name: "NewFile" ; Current file ]
Set Variable [ $r ; Value: MBS("DynaPDF.ReleaseAll") ]
Refresh Portal [ Object Name: "Files" ]

We have reached our goal for today and will read again tomorrow.


Monkeybread Software Logo with Monkey with Santa hat
3 👈 4 of 24 👉 5
Claris FileMaker Plugin
04 12 24 - 08:19