Printing Reports


The Printing Reports topic describes a technique available to Alpha Anywhere database users.

This example shows how to print a report and deliver it to your Internet user. Since, a paper report is out the question, Alpha Five will compose the Invoice report from the invoice.set, save it in PDF format as foo.pdf, and then start Adobe Acrobat to display it in the client's browser. The expression ?" + time("hms03") added to the PDF filename insures that you do not open a cached version of the file.

Report.saveas("Invoice@c:\program files\a5v5\samples\alphasports\invoice.set", "pdf", "", "", session.session_folder + "\foo.pdf", .f.)
response.redirect(session.session_url + "foo.pdf?" + time("hms03"))

Creating the Dialog 

  1. Create a new dialog web component.

  2. Display the Form > Controls page.

  3. Under the Control Types list select "Dropdownbox" and click the '>' button.

  4. Enter "invoiceNumber" in the Name field, click Make label same as name, and click OK.

  5. Click the '...' in the DropDownBox Properties > Choices field to display the Define Choices dialog.

    • 1. Select "Dynamic" in the Define Choices > List Type list.

    • 2. Select "DBF-Table" in the Data Source > Data source type list.

    • 3. Select "invoice_header" in the Data Source > Table name list.

    • 4. Click the '...' button in the Data Source > Display value field/expression field to display the Expression Builder.

    • 5. Press F2, select "Invoice_Number", and click Insert. Click OK.

  6. Set the DropDownBox Properties > Height field to 6.

  7. Select "Multiple" in the DropDownBox Properties > Multi-select allowed field.

  8. Click the '...' button in the DropDownBox Properties > In-line style field.

    • 1. Display the Position tab.

    • 2. Click the ruler icon next to the Width field.

    • 3. Enter 2 into the Size field.

    • 4. Select "in" from the drop down list box.

    • 5. Click OK > OK.

  9. Change the Row Properties > Row label field to "Invoice Number".

  10. Check the Row Properties > Freeform layout check box.

  11. Click the '...' button in the Row Properties > Freeform template field to display the Freeform Column Layout dialog.

  12. Enter the following text and click OK. Note how this displays the invoiceNumber field with HTML text above and below it.

    <b>Invoice Numbers</b><br>
    {invoiceNumber} <br>
    Select one or more invoices.
  13. Display the Form > Properties page.

  14. Clear the Layout Options > Show row labels check box.

  15. Change the Submit/Reset Buttons > Submit button label field to "Print Invoices".

  16. Click the '...' button in the Server Events > AfterValidate field to display the Form > Events page.

  17. Enter the following code into the Form Events dialog and click OK.

    • IF eval_valid("invoiceNumber") = .f. Then
          goto skipreport
      end if
      dim filter as C
      dim order as C
      dim invnum as C
      dim filename as C
      'since InvoiceNumber is a multi-select DropDownBox, it is actually an array that contains the selected values.
      invnum = invoiceNumber.dump()
      invnum = remove_blank_lines(invnum)
      invnum = *for_each(x,"invoice_number = " + quote(x),invnum)
      filter = stritran(alltrim(invnum), crlf()," .or. ")
      Order = ""
      filename = session.session_folder + chr(92) + "tempreport.pdf"
      filename = report.saveas("Invoice@<adb_path>\invoice.set", "pdf", filter, order, filename,.f.)
      if file.exists(filename)
          currentform.RedirectTarget = session.session_url + "tempreport.pdf?" + rand_string(5)
      end if
    • Understanding the AfterValidate Script 

      The AfterValidate event occurs when you click the submit button (in this case renamed to "Print Invoices"). The first part of the script tests to see if any invoice numbers have been selected. When you select one or more invoice numbers, the invoiceNumber variable exists, and eval_valid("invoiceNumber") equals .T. . When no selection has been made, the result is .F., which causes the code to go to skipreport: and exit.

      IF eval_valid("invoiceNumber") = .f. Then
          goto skipreport
      end if
    • A multi-select drop down box saves the user's input into an array. The invoiceNumber.dump() method extracts these values and puts them into a CR-LF delimited list named invnum. The remove_blank_lines(invnum) function collapses the list when you skip selections in the list box (i.e. 1,3,4). The *for_each(x,"invoice_number = " + quote(x),invnum) statement converts the list into a series of table filter expressions. The stritran(alltrim(invnum), crlf()," .or. ") function replaces each CR with an " .or. ", resulting in a single filter expression that is assigned to the filter variable.

      invnum = invoiceNumber.dump()
      invnum = remove_blank_lines(invnum)
      invnum = *for_each(x,"invoice_number = " + quote(x),invnum)
      filter = stritran(alltrim(invnum), crlf()," .or. ")
      Order = ""
    • The following statement sets the path and filename of the new report and assigns it to the filename variable. The purpose of the session.session_folder is to hold these temporary files.

      filename = session.session_folder + chr(92) + "tempreport.pdf"
    • The REPORT.SAVEAS() function actually prints the report. It calls the Invoice_Report report of the invoice set. The method references uses <adb_path> alias, which makes the application portable to other servers. The REPORT.SAVEAS() function returns the filename of the report it creates.

      filename = report.saveas("Invoice_Report@<adb_path>\invoice.set", "pdf", filter, order, filename, .f.)
    • If the file.exists(filename) method determines that Alpha Five created the PDF file, the script sets the currentform.RedirectTarget property of the page to session.session_url + "tempreport.pdf?" + rand_string(5). When the currentform.RedirectTarget property has a value, Alpha Five interprets it as the next page to display. The report's filename is session.session_url + "tempreport.pdf?" + rand_string(5). One interesting aspect of the report's filename is the rand_string(5) appended to the end. This forces Acrobat to load and display the report.

      if file.exists(filename)
          currentform.RedirectTarget = session.session_url + "tempreport.pdf?" + rand_string(5)
      end if
  18. Click OK to close the Form Events dialog.

  19. Click File > Save As, enter "Print_Report" in the File name field, and click Save.

  20. Close the Dialog Builder.

Creating the Print_Report Page 

  1. Click New > OK to create a new A5W page.

  2. Click File > Save As, enter "Print_Report" in the File name field, and click Save.

  3. Select Insert Component > Select, pick "Print_Report", and click OK > OK.

  4. Save the page and close the HTML Editor.

  5. Right click the "Print_Report" page, select Publish (Local Webroot) and open.

  6. Select a couple of invoice numbers and click Print Invoices. The report will open in your browser.