« MBS FileMaker Plugin,… | Home | MBS FileMaker Advent … »

MBS FileMaker Advent calendar - Door 6 - Sending Out Wishlist Forms

candy cane Monkeybread Monkey as an elf candy cane
Door 6 - Sending Out Wishlist Forms

Today it is time for us to take care of the wish lists of the giftees. We want to prepare a form that we send by e-mail to the giftees, who fill out this form. In a later door we can then read this information from our returned document.

To do this, we first create the template we want to send. To do this, we first need a simple PDF file on which we can position our fields. We have the option of either building this form with DynaPDF, or we can use a pre-designed PDF in which we create fields. I chose this method now. The document was created with Pages and then output as a PDF via the print settings. We want to write several labels in this file. The labels are not only important for the labeling, so that the customer knows which data is to be entered there, but also for positioning the form fields. We want to position these behind the labels.

Now we have everything we need to create our template.

Since we are working with DynaPDF, we first have to initialize it again, as we already explained in door 4.

If [ MBS("DynaPDF.IsInitialized")=0 ]
	Perform Script [ Specified: From list ; "InitDynaPDF" ; Parameter:    ]
End If

We place our form in the same folder as the database. This allows us to determine the path to the form with a small workaround. We first determine the path to the current database with Get(FilePath) to determine the path to the current database. However, this is a FileMaker path that looks a little different to a native path. With the MBS function Path.FileMakerPathToNativePath we can convert a FileMaker path to a native path. Now we use the MBS function Path.RemoveLastPathComponent to remove the last component of this path, i.e. the file name of the FM database, from the path, so that we get the folder in which the two items are located. Now we append the name of the form file. To do this, we use Path.AddPathComponent and specify the folder path and the file name. We also want to specify the path where we will save our template file. Again, we use the folder path and append the new file name WishList.pdf.

Set Variable [ $PathFM ; Value: MBS("Path.FileMakerPathToNativePath"; Get(FilePath)) ]
Set Variable [ $Folder ; Value: MBS("Path.RemoveLastPathComponent"; $PathFM) ]
Set Variable [ $FormPath ; Value: MBS("Path.AddPathComponent"; $Folder;"WishListForm.pdf") ]
Set Variable [ $TemplatePath ; Value: MBS("Path.AddPathComponent"; $Folder;"WishList.pdf") ]

After we have determined the paths for loading the form file and for saving the template file, we would like to load the form file into a PDF working environment so that we can modify it.

To do this, we first create a new working environment with DynaPDF.New, open the form file for import with DynaPDF.OpenPDFFromFile, specifying the reference and path, and import the form file into our working environment with DynaPDF.ImportPDFFile. So that we can now also set the fields on the page, i.e. change the page, we have to make the page editable. To do this, we call the DynaPDF.EditPage function, specifying the reference and the page to be edited, in our case the first one.

Set Variable [ $pdf ; Value: MBS("DynaPDF.New") ]
Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromFile"; $pdf; $FormPath) ]
Set Variable [ $r ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf) ]
Set Variable [ $r ; Value: MBS("DynaPDF.EditPage"; $pdf; 1) ]

Before we set the fields, we first want to briefly configure their appearance. We can choose the background color and the color of the border. Our fields should be light green. We set the border color to white so that it does not disturb us. The color values are set as RGB values with DynaPDF.RGB.

Set Variable [ $r ; Value: MBS("DynaPDF.SetFieldBackColor"; $pdf; MBS("DynaPDF.RGB"; 214; 247; 204)) ]
Set Variable [ $r ; Value: MBS("DynaPDF.SetFieldBorderColor"; $pdf; MBS("DynaPDF.RGB"; 255; 255; 255)) ]

Now it's time for our fields. To create these fields, we use a self-written function that creates a field and positions it appropriately. This custom function is called CreatTextfield and has two parameters. The first parameter is the name of the field to be created and the second is the name of the label behind which the field is to be positioned. We will get to know this function in more detail in a moment.

Set Variable [ $r ; Value: CreateTextfield ( "one" ; "1." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "two" ; "2." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "three" ; "3." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "four" ; "4." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "five" ; "5." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "six" ; "6." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "seven" ; "7." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "eight" ; "8." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "nine" ; "9." ) ]
Set Variable [ $r ; Value: CreateTextfield ( "ten" ; "10." ) ]

After we have created our fields for the Christmas wishes, we would now like to create a field that is not visible, but is extremely important for our later import. We want to create an ID field that contains the primary key of our giftee. In this way, we can ensure that our document can be assigned to exactly this one giftee without any misunderstanding.

First we create the field with DynaPDF.CreateTextField in our PDF environment with the name ID. Since the text field has no parent field, we enter a -1 here. With the 0 in the parameters we indicate that the field has a single line, because we deactivate multiline. The 200 in the parameters describes the maximum length of the text of 200 characters. The last 4 parameters are for position and size. We position the field outside the visible PDF area, with a negative y value. With a width of 300 pixels and a height of 20 pixels, it offers enough space for our primary key.

Since the field should be invisible and read only, we set the corresponding properties in DynaPDF.SetFieldFlags.

Set Variable [ $IDField ; Value: MBS("DynaPDF.CreateTextField"; $pdf;"ID"; -1; 0; 200; 120; -50; 300; 20) ]
Set Variable [ $r ; Value: MBS("DynaPDF.SetFieldFlags"; $pdf; $IDField; "Hidden ReadOnly Invisible NoView"; 1) ]

Now we have done everything on our page and we can close the page again with DynaPDF.EndPage and write the template to the desired path with DynaPDF.OpenOutputFile. We can then release the environment again.

Set Variable [ $r ; Value: MBS("DynaPDF.EndPage"; $pdf) ]
Set Variable [ $r ; Value: MBS("DynaPDF.OpenOutputFile"; $pdf; $TemplatePath) ]
Set Variable [ $r ; Value: MBS("DynaPDF.Release"; $pdf) ]

We have already talked about the custom function CreateTextfield and want to take a closer look at this function now. This function has two input parameters FieldName and SearchText. A field is created with DynaPDF.CreateTextField as we have just seen. Before we can take this step, however, we must first determine the position of the field. With DynaPDF.FindText we first search for a specific text on the page. In our case, the name of the label. We receive the position of the label text as a return using eight coordinates that describe the corner points of a rectangle that completely covers the text we are looking for (x1 y1 x2 y2 x3 y3 x4 y4). Since we always want to have the same distance to the side edges of the fields, we only have to look for a y value of the upper edge and a y value of the lower edge when determining the missing data. We therefore use MiddleWords to determine the 1st y and the 3rd y value. Since our numbers are still in text format, but we want to calculate with them, we convert them into numbers with Math.TextToNumber. We can now call our already familiar DynaPDF.CreateTextField function. Let's take a look at the last 4 parameters that make up the positioning. The 120 is a fixed value and describes the distance of the field to the left side edge (x). Now it's the turn of the upper side edge. As we also want to have a little space above and below our text, we subtract 3 pixels from the y value. This results in the y value for the top edge. The value 300 is a fixed value and describes the width of the field. This is always the same in this example. Our last value is the height of the field. We calculate this height by subtracting the lower y value from the upper y value. The difference describes the height of the search result. As we have just mentioned that we want to leave a little space at the top and bottom, we add 5 to the height here.


Let ( 
	[ 
	Search = MBS( "DynaPDF.FindText"; $pdf; SearchText; 0 );
	posText1 = MiddleWords ( Search ; 2 ; 1 );
	posNum1 = MBS( "Math.TextToNumber"; posText1 );
	posText2 = MiddleWords ( Search ; 6 ; 1 );
	posNum2 = MBS( "Math.TextToNumber"; posText2 );
	Field = MBS( "DynaPDF.CreateTextField"; $pdf; FieldName; -1; 0; 200; 120; posNum1-3; 300; posNum2-posNum1+5 )
	]; 

	Field)

Export for the individual giftees

Now it's time for us to prepare the documents to be sent to our customers. We have already described that we need to install a protection so that we can assign the correct wish list to each giftee and fill the invisible ID field with the primary key of the giftee.

To do this, we now load our template with the already known functions into a new work environment and set the value with DynaPDF.SetTextFieldValue, specifying the reference, the name of the field to be set and the primary key.

As we do not want to save the PDF in a file this time but in a container, we use the DynaPDF.Save function to save the PDF file. Finally, we release the working environment.

If [ MBS("DynaPDF.IsInitialized")=0 ]
	Perform Script [ Specified: From list ; "InitDynaPDF" ; Parameter:    ]
End If

Set Variable [ $PathFM ; Value: MBS("Path.FileMakerPathToNativePath"; Get(FilePath)) ]
Set Variable [ $Folder ; Value: MBS("Path.RemoveLastPathComponent"; $PathFM) ]
Set Variable [ $TemplatePath ; Value: MBS("Path.AddPathComponent"; $Folder;"WishList.pdf") ]
Set Variable [ $pdf ; Value: MBS("DynaPDF.New") ]
Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromFile"; $pdf; $TemplatePath) ]
Set Variable [ $r ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf) ]
Set Variable [ $r ; Value: MBS("DynaPDF.SetTextFieldValue"; $pdf; "ID"; Giftee::PrimaryKey) ]
Set Variable [ $File ; Value: MBS("DynaPDF.Save"; $pdf; "Wishlist_" & Giftee::first_name & "_" & Giftee::last_name & ".pdf") ]
Set Field [ Giftee::WishlistOut ; $File ]

Set Variable [ $r ; Value: MBS("DynaPDF.ReleaseAll") ]

We can now create the individual ones and send them out to our recipients to fill in diligently.

That's it for today with our door and we'll see you again tomorrow.


Monkeybread Software Logo with Monkey with Santa hat
5 👈 6 of 24 👉 7
Claris FileMaker Plugin
06 12 24 - 09:26