Servoy: print to screen/file/email/printer

I wanted a convenient way to manage giving the user a option to print any content to a screen preview, file, email or a printer for any print job in the solution. What I came up with is what follows.

First, you'll need a UI, mine looks like this (red text added to show global variables):

Servoy print screenshot

Then you need a method to show that as a dialog -- this one returns true if the user clicked OK, using a global to store the dialog result from the dialog itself:

JavaScript:
  1. var winloc = globals.dialog_getLocation();
  2. application.showFormInDialog(forms.print_SelectDestination,null,null,500,360,'Print',true,false,true);
  3.  
  4. if( globals.print_destination_result == 1 )
  5.     return true;
  6. return false;

Then you need a method to call to actually do a print job. Before doing the print job, you'll need to set the foundset set on the form you're planning on printing, then pass the form name to this method:

JavaScript:
  1. var frmName = arguments[0];
  2. var printCurrentOnly = ( arguments[1] == null || arguments[1] == true );
  3.  
  4. if( !globals.print_destination_select() )
  5.     return;
  6.  
  7. if( globals.print_destination == 'printer' )
  8. {
  9.     forms[frmName].controller.print( printCurrentOnly );
  10.     return;
  11. }
  12. if( globals.print_destination == 'pdf' )
  13. {
  14.     var f = "";
  15.     if( globals.util_isValidString( globals.print_destination_pdf ) )
  16.         f = globals.print_destination_pdf;
  17.     else
  18.         f = plugins.file.createTempFile( "preview", ".pdf" );
  19.  
  20.     var success = plugins.pdf_output.startMetaPrintJob();
  21.     if( success )
  22.     {
  23.         forms[frmName].controller.print(printCurrentOnly,false,plugins.pdf_output.getPDFPrinter() );
  24.     }
  25.     var pdf = plugins.pdf_output.endMetaPrintJob();
  26.     plugins.file.writeFile( f, pdf );
  27.     globals.util_openFile( f );
  28.     return;
  29. }
  30. if( globals.print_destination == 'email' )
  31. {
  32.     var success = plugins.pdf_output.startMetaPrintJob();
  33.     if (success)
  34.     {
  35.         forms[frmName].controller.print( printCurrentOnly,false,plugins.pdf_output.getPDFPrinter());
  36.     }
  37.     var thePdf = plugins.pdf_output.endMetaPrintJob();
  38.     var attachment1 = plugins.mail.createBinaryAttachment( 'output.pdf', thePdf );
  39.     var success = globals.mail_send( globals.print_destination_email_to, globals.print_destination_email_from, globals.print_destination_email_subject, globals.print_destination_email_body, null, null, new Array(attachment1) );
  40.     if( !success )
  41.     {
  42.         globals.dialog_error( "Unable to send mail." );
  43.     }
  44.     return;
  45. }

Of course, there's a few minor details I left out. You need to set the destination global in the dialog. You need a bit of UI coding to highlight and select destinations. You'll notice in the code that if you select PDF as a destination, and leave the filename blank, it will create a temp file and open it. It works very nicely, however.

2 responses to “Servoy: print to screen/file/email/printer”

  1. Jason

    Where is the authentication done for your smtp server? Do you hide that in your global method “mail_send” ?

Leave a Reply

Switch to our mobile site