- This lesson will teach you the following concepts
- Principles for printing on Internet
- Direct print
- Generating HTML, PDF or XML documents
- What is a report made of?
- Creating the "Invoice" report
- Creating the query
- Creating the report based on a query
- Modifying the "Invoice" report
- Displaying the printed report from a button
- Implementing the print
- Print test
Lesson 5.2 Printing an invoice
This lesson will teach you the following concepts
- Creating a report based on a query
- Printing a report based on a query with parameters
Estimated time: 45 mn
We are going to give the user the ability to directly print the invoice corresponding to the sought order.
|If the pages have not been created in the previous lessons, you can follow this lesson by opening a corrected project: on the "Home" pane, in the "Online help" group, expand "Tutorial" and select "Full WEBDEV site (With pages)".|
A full corrected project is also available: on the "Home" pane, in the "Online help" group, expand "Tutorial" and select "Full WEBDEV site (Answer)".
Principles for printing on Internet
In fact, we should not talk about "printing" on Internet. Indeed, when a document must be "printed", a file is generated beforehand (in HTML, PDF or XML format) and it is transmitted to the browser. As soon as the file transfer is completed, the Web user decides whether the document that was received will be printed or not.
However, "printing" on the server remains possible. But the document that is printed (on the server printer or on a shared network printer) will not be accessible to the Web user.
The printed or generated document is prepared and formatted by the report editor. The information found in the document can come from a database.
Two types of prints are available:
- The direct print (on the server printer).
- The generation of various documents (HTML, PDF, ...).
The direct print consists in printing on a printer directly. This printer is plugged into the server or accessible from the network.
The direct print with WEBDEV is recommended for Intranet or Extranet only. This type of print is used to print the logs for customer connections, to directly print the order on the printer of sales department.
Generating HTML, PDF or XML documents
Printing from HTML, PDF or XML documents consists in creating a document and in displaying it on the browser of Web user. To create this document, all you have to do is use the WEBDEV report editor. A formatted file can be "generated" by your WEBDEV site from a data source. The file can be in HTML, PDF, RTF or XML format.
The main benefit of this method is that the generated file can be transmitted to the browser. The Web user can print the document or store it on his computer.
Printing in a file can be used for an Internet site and for an Intranet/Extranet site (for example: to transmit an order form in PDF format to the Web user, ...).
What is a report made of?
In our example, the invoice that must be printed will correspond to a report. A report is a project element used to group and format the data to print.
To create and use reports, WEBDEV proposes a specific editor: the report editor.
A report includes:
- different blocks. The blocks are used to define the areas where the data will be displayed. The available blocks are as follows:
- Start of document: block displayed on the first page only.
- Page header: block displayed at the top of each page.
- Body: block containing the data. This block is printed as long as there is data to print.
- Page footer: block displayed at the bottom of each page.
- End of document: block displayed on the last page only.
- controls used to display the data.
Let's see the report that we want to create:
Creating the "Invoice" report
First of all, we are going to list the information that will be displayed in the report:
- The order characteristics: date and order number.
- The personal customer details: name, address, zip code, city and country.
- The characteristics of order lines:
- Ordered quantity,
- Product reference,
- Product caption,
- Total BT,
- Total IOT.
To easily create this report, the data to print will be grouped in a query. This query can be used by the report or by any other element of WEBDEV project (Table control, Looper control, ...).
|WEBDEV proposes to create reports from several data sources: data files, queries, controls, text files, ...|
In most cases, we advise you to group the data to print via a query and to create a report based on this query. To add an information into the report, all you have to do is add the corresponding item into the query.
The reports based on data files must be simple reports, which means reports used to display data coming from a single data file.
Creating the query
- The query editor will be used to create the base query of report.
- Click among the quick access buttons. The window for creating a new element is displayed: click "Query". The wizard for query creation starts.
- Select the "Select" option.
Indeed, this query will be used to select the records that will be printed in the report. Go to the next step.
- The description window of query is displayed.
- First of all, give a name to the query: type "QRY_Invoice" instead of "QRY_NoName1" in the "Query name" area.
- To build the query, we are going to select the elements that will be displayed in the result. The query will contain the content of Orders file, the content of OrderLine file and the content of Customer file.
- In the left section of the screen, select the Orders file and click the blue arrow ( ): the items of Orders file are displayed in the middle of the screen.
- Repeat this operation for the OrderLine and Customer files.
The description window of query is as follows:
At this time, this query is used to select all orders and the corresponding order lines.
We are now going to reorganize the query items. Indeed, this order will be used to create the different controls of the report based on this query. We want the product caption to be positioned just after the product reference.
- To reorganize the order of items:
- Select the ProductCaption item of OrderLine file.
- Click the "top arrow" button on the right of the list of items. The item moves up.
- Position the item just after the product reference (OrderLine.Reference).
We want to select the data corresponding to a single order whose identifier is known. Therefore, we are going to define the order number in parameter.
- To manage the "Order identifier" parameter:
- Select the Orders.OrdersID item (in the middle of the screen).
- Click in the fourth column: select "New condition".
|When describing a query, the section that lists the query elements includes 4 columns:|
To access one of these characteristics, all you have to do is click in the corresponding column.
- The item name,
- The ability to display (or not) the item in the query result,
- The sort management for the item,
- The number of conditions associated with the item.
- In the window that is displayed, we are going to specify that the selection condition corresponds to a parameter: Perform the following operations:
- Select "Is equal to".
- Check "the parameter".
- Specify the parameter name: "pOrderID".
- Validate the description window of condition. The number "1" is displayed on the right of Orders.OrdersID item, indicating that a selection condition was defined.
- The order identifier will not be viewed so let's make it invisible: click the eye found on the item line and select "Don't display".
- Similarly, make the following items invisible:
- Validate the description window of query (green button at the bottom of the screen).
- Validate the backup information
- The graphic query representation is displayed:
Creating the report based on a query
- To create a report:
- Click among the quick access buttons.
- The window for creating a new element is displayed: click "Report" then "Report". The wizard for report creation starts.
- The wizard for report creation proposes several types of reports:
- Select "Table". Go to the next step.
- Select the data source of report. The report will be based on the query that was just created. Select "From a data file or from an existing query". Go to the next step.
- In the list of data files and queries, select the "QRY_Invoice" query. Go to the next step.
- The wizard asks you to specify whether a break is required. No break will be used in this report. This concept will be presented later in this tutorial. Answer "No". Go to the next step.
- You are going to specify the order in which the items will be printed and how they will be distributed in the different blocks:
- The items regarding the customer will be displayed in the "Start of document" block. Indeed, this information must not be repeated on each order line.
- The items regarding the order will be displayed in the "Page header" block. Indeed, this information must not be repeated on each order line.
- The items regarding the order lines will be displayed in the report body. These items will be displayed on all order lines of order.
- The items regarding the totals of order will be displayed in the "End of document" block. Indeed, this information must not be repeated on each order line.
The following table presents the different assignments of items in the order presented in the wizard:
|OrderNum||Start of document |
|TotalBT ||End of document |
|TotalVAT||End of document|
|TotalIOT ||End of document |
|Reference ||Body |
|Product caption ||Body|
|Company||Start of document |
|FullName||Start of document |
|Address||Start of document |
|ZipCode||Start of document |
|City||Start of document |
|StateDep||Start of document |
|Country||Start of document |
- Go to the next step.
- The wizard proposes to create a counter, a sum or a mean on the numeric items found in the report. In this report, the calculations are performed by the query. Click the "No calculation" button. Go to the next step.
- This screen is used to define the report layout.
We will keep the default values with the "Portrait" orientation.
When choosing the print margins, don't forget to take into account the physical margins of printers. The physical margins of printers are margins where no print is allowed. Furthermore, the physical margins differ according to the printer models.
- Go to the next step.
- This screen allows you to select the skin template used for the report. We recommend that you use the same skin template as the one used for the pages. In our case, select the "Evolution" skin template for example and go to the next step.
- All we have to do now is give a name and a title to the report.
- Type the title: "Invoice".
- Type the name: "RPT_Invoice".
- The table as it is defined does not fit on an A4 page in portrait mode. WEBDEV proposes
Choose "Reduce the table".
- to print the report on 2 pages in width,
- to switch to landscape mode,
- to reduce the table size.
- Validate the backup information.
- The report is displayed in edit in the report editor:
- The different order lines are grouped in a table.
Modifying the "Invoice" report
We are going to perform modifications in the report that was just created.
- We want to position the information regarding the customer and the order in the page header:
- Delete the captions found in front of the customer details (Name, ...).
- Position the item containing the city beside the zip code.
- Enlarge (with the handles) the control containing the company name: the control size must be identical to the size of Zip Code and City controls.
- Align the controls:
- Select the Company control.
- Press Ctrl and select (with the mouse) the controls containing the address, the state and the country.
- On the "Alignment" pane, click "Justify (Ins. and Out.)".
- Select the customer details and move them (with the mouse) to the right of report.
- Move the order number up (to the top of "Page header" block).
- We are going to create a border around the customer details:
- Create a Static control: on the "Creation" pane, in the "Text" group, click "Static".
- Click in the page header, at the location where the customer details are displayed.
- Press Shift and enlarge the static control (with the handles) so that it contains all customer details. This operation is used to enlarge the static control without moving the controls found below.
- In the popup menu of Static control, select "Edit the caption" and delete the caption. Click in the report to validate the modification.
- To configure the border:
- Select the Static control that was just added.
- Press the R key and click the picto that appears in the top right corner of control.
- Select "Edit the border". The configuration options are displayed:
- the dark blue color in the context-sensitive colors.
- a simple border (thin line).
- a rounding set to 2 mm in width and in height.
- We are going to position the totals properly in the "End of document" block:
- Select the controls corresponding to the totals found in the "End of document" block.
- Position these controls with the mouse in the bottom right corner of table.
- The report is displayed in the report editor:
- Our "Invoice" report is created. Save the report by clicking among the quick access buttons.
- Check the modifications performed by running the report in "Preview" mode (click among the quick access buttons).
- Close the print preview.
Displaying the printed report from a button
As already seen at the beginning of this lesson, the site being run on a server, the document will be printed on a printer connected to the server (and therefore inaccessible to the user).
Therefore, the print will be done in PDF format on the server and the generated PDF document will be downloaded or displayed on the computer of Web user. Then, the document can be printed by the user on his printer.
In our site, the "PRT_Invoice" report will be printed from a button found in the page used to find an order. This button will be used to print the invoice for the selected order.
Implementing the print
To print the "RPT_Invoice" report:
- Position on the "PAGE_Multicriteria_Search" page: click the "PAGE_Multicriteria_Search" button found in the button bar.
- On the "Creation" pane, in the "Usual controls" group, click "Button".
- Click on the right of Table control to create the button.
- Select the button and press Enter on the keyboard. The caption becomes editable.
- Replace "Button" by "Print" and validate.
- Display the code of this control (F2 key) and type the following server click code:
Let's study this code:
- iDestination is used to define the print format (PDF format in this case).
- The "RPT_Invoice" report being based on a query with parameters, the parameter must be passed to the query before running the report. This operation is performed by iInitReportQuery. In our case, the parameter corresponds to the number of the current order, displayed in the Table control.
- iPrintReport is used to generate the "Report_Invoice" report in the specified format (PDF in our case).
- FileDisplay is used to display the invoice to the user:
- iLastFile returns the path of the last file generated by a report.
- "application/pdf" if the MIME type of returned file. When this type is specified, the browser can display the file directly or it can choose the application that will be used to perform the display on the computer of Web user.
- Close the code editor.
- Save the page ( or Ctrl + S).
- Now, all we have to do is run a real test:
- Run the test of "PAGE_Multicriteria_Search" page.
- Specify the criteria and start a search.
- Select one of the orders displayed in the Table control.
- Print the order via the "Print" button.
- The browser opens the PDF file. The browser has replaced the current page by the PDF file. We are going to change this behavior.
- Close the browser.
- To open the PDF file in another tab or browser:
- Double-click the "Print" button in the "PAGE_Multicriteria_Search" page. The description window is displayed.
- In the "General" tab, in the Target control, select "New browser".
|New tab or new window?|
The display in a new tab or in a new window can be chosen neither by the user nor by the site developer. It is the browser that chooses whether a new tab or a new window is opened. The behavior can differ according to the browser used!
Click [Add] to post a comment