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

MBS FileMaker Advent calendar - Door 23 - Printing a PDF

candy cane Monkeybread Monkey as an elf candy cane
Day 23 - Printing a PDF

Today our monkey wants to make a list of the presents that need to be bought by Christmas Eve so that he can check off what is there and what still needs to be bought. Our monkey would like to document this in a PDF.

So first we have to create this list. Do you remember how we read out the wish lists on day 10?

We now need the list of things that our Giftee wants. This list contains the wishes as text, each separated by a line break. We now want to write these entries in a table. Each gift should have a data record and be able to be assigned to the gift without mistakes. To do this, we first create a new table Gifts, which has the fields Giftee and Gift. The Giftee table is linked to this table via its primary key and the Giftee Foreinkey in the Gifts table to enable it to be assigned to a Giftee. In our script, we first transfer the gift list for a person into a variable and ask how many entries this list has. We also note the PrimaryKey for the person and then open a window to our new table. We then go through each entry in our list in a loop and combine it with the primary key of the gift to form a record. Once we have added all the entries to our database, we close the window again.

Set Variable [ $Gifts ; Value: Giftee::Presents ]
Set Variable [ $Length ; Value: ValueCount ( $Gifts ) ]
Set Variable [ $Key ; Value: Giftee::PrimaryKey ]
New Window [ Style: Document ; Name: "Gifts" ; 
	 Using layout: "Gifts" (Gifts) ]
Set Variable [ $i ; Value: 1 ]
Loop [ Flush: Always ]
	Exit Loop If [ $i>$Length ]
	New Record/Request
	Set Field [ Gifts::Giftee ; $Key ]
	Set Field [ Gifts::Gift ; GetValue ( $Gifts ; $i ) ]
	Set Variable [ $i ; Value: $i+1 ]
End Loop
Close Window [ Name: "Gifts" ; Current file ]

The layout in which we would like to display our data looks like this:

When this list is now also filled with the other data, we would like to print this list as a PDF. An additional requirement is that we also want to print the layout in landscape, because the layout is too wide. This is now very easy under Windows. Here we have the Printer.Print function from the Printer component, which also contains other functions relating to printing under Windows.

Here we can use a large number of parameters to set how we want to print our layout on a printer or as a PDF. First we enter the name of the printer, if we leave this parameter empty, the current printer is used, the parameters that follow are optional. We can first specify whether our print dialog should be displayed and if not, how we want to proceed with it. We want our dialog to be closed with OK. For this purpose, we pass the 1. You can find further options in our function references. Now we specify how many copies we want to make of our printout, we want to leave this setting at zero and therefore leave this parameter blank. We would then like to specify the paper format. This should be A4. Now comes the orientation of the paper, which we specify as desired with landscape. Then we can specify the paper source and select manual (Man). If you have a multi-page document where you only want to print a certain range, you can also specify this with start and end page in the next two parameters. If you want collate pages, you can specify this with a 1 or 0. We do not need this parameter and leave it empty, as well as the next one with which we can decide whether we want to print all records that are currently displayed, a single record or an empty record. We stick to the default setting here too. In the last optional parameter, we can now enter that we want to print the document to a file, i.e. make it a PDF. Here, of course, we set a 1.

Else If [ Get(SystemPlatform)= -2 ]
	# Windows
	Set Variable [ $file ; Value: MBS( "Printer.Print"; 
		"" ; 1; ""; "A4"; "landscape"; "Man"; ""; ""; ""; ""; 1  ) ]

If we run this script under windows, we will be asked at the end of the save dialog where we want to save it and can then use it.

This is more difficult to implement on the Mac. Here we have the functions from the PrintDialog component at our disposal. The information is divided into two large areas: PrintDialog, which can set information for the print dialog, and PageSetupDialog, in which we make the settings for the appearance of the page. First we have to install both areas and make them editable.

Set Variable [ $r ; Value: MBS("PrintDialog.Install" ) ]
Set Variable [ $r ; Value: MBS("PrintDialog.Enable") ]
Set Variable [ $r ; Value: MBS("PageSetupDialog.Install") ]
Set Variable [ $r ; Value: MBS("PageSetupDialog.Enable") ]

Afterwards we would like to reset the PageSetupDailog Options once, so that we can avoid legacy issues.

Set Variable [ $r ; Value: MBS("PageSetupDialog.ClearOptions") ]

Now we can select a printer for both areas. If we enter an empty text in the parameters here, we assume that the current printer is to be used, just as for Windows.

Set Variable [ $r ; Value: MBS("PrintDialog.SetPrinterName"; "") ]
Set Variable [ $r ; Value: MBS("PageSetupDialog.SetPrinterName"; "") ]

Using PageSetupDialog.SetPaperOrientation, we can now set the paper orientation and specify landscape in the parameter. We should not forget that we want to return the document as a PDF. To do this, we use the PrintDialog.SetPrintToPDF function and enter 1 in the parameters to activate this option. We still need a storage location for our file. We want to define this as a path as already known. We save our file on the desktop in a file called Shoppinglist.pdf. We then pass this path to the PrintDialog.SetDestinationPath function. This will later return the file in this path.

Set Variable [ $r ; Value: MBS("PageSetupDialog.SetPaperOrientation"; "landscape") ]
Set Variable [ $r ; Value: MBS("PrintDialog.SetPrintToPDF"; 1) ]
Set Variable [ $Desk ; Value: MBS("Folders.UserDesktop") ]
Set Variable [ $Path ; Value: MBS("Path.AddPathComponent"; $Desk; "Shoppinglist.pdf") ]
Set Variable [ $r ; Value: MBS( "PrintDialog.SetDestinationPath";$path ) ]

We do not want to bother the user with extra dialogs, so we hide both dialogs. To do this, we use the specified SetNoDialog functions.

Set Variable [ $r ; Value: MBS("PrintDialog.SetNoDialog"; 1) ]
Set Variable [ $r ; Value: MBS("PageSetupDialog.SetNoDialog"; 1) ]

Now we start the function Menubar.RunMenuCommand with a delay of 0.1 seconds using Schedule.EvaluateAfterDelay. This function can run a menu for command in FileMaker. In our case we want to call the Page Setup dialog. For this we enter the number 57606. This stands for the Page Setup Dialog menu command. If you are interested in other menu commands, please have a look at our help.

The Page Setup dialog is not displayed as we have switched it off with the SetNoDialog command. However, it must be called up so that the plugin can insert the information, but it is closed before the user can see it. The same applies to the Print dialog. This must be set to Show On, otherwise the plugin cannot make the settings.

Set Variable [ $r ; Value: MBS( "Schedule.EvaluateAfterDelay"; ,1; 
	"MBS(\"Menubar.RunMenuCommand\"; 57606 )") // run Page Setup Dialog in 0.1 seconds ]
Pause/Resume Script [ Duration (seconds): 1 ]
Print [ With dialog: On ]

We have now reached the end of our script and can try it out.

See you again tomorrow for the last door. Until then, have fun with your PDFs.


Monkeybread Software Logo with Monkey with Santa hat
22 👈 23 of 24 👉 24
23 12 24 - 13:13