Sign in Join
Wiki copytemporary

LazReport Documentation: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: LazReport Documentation

OldNewLine
1+## Overview
2+
3+LazReport is a group of components to add reporting capabilities to applications. It uses a visual designer to create banded reports and includes a report engine with previewer and an interpreter to run user scripts. The report designer can be invoked at runtime.
4+
5+## License
6+
7+LazReport is based on FreeReport 2.32 and thanks to Fast Reports Inc. it's available under modified LGPL, the same license as the Lazarus LCL. See files lazreport/license.txt, license-rus.txt and license-lazreport.txt for details.
8+
9+## Authors
10+
11+FreeReport was created for Fast Reports Inc.
12+
13+LazReport initial port was made by Olivier Guilbaud.
14+
15+Lazarus integration and fixes by Jesus Reyes A.
16+
17+Many contributors, see lazreport/doc/contributors.txt file
18+
19+## Installation
20+
21+To install LazReport in the Lazarus IDE:
22+
23+1. Open LazReport Package. Menu: Package-\>Open Package File (.lpk)...
24+2. Open file components/lazreport/source/lazreport.lpk
25+3. Install
26+
27+The next time Lazarus is started, it should show a LazReport tab in the component palette.
28+
29+## Documentation
30+
31+Developer's manual and User Guides specific for LazReport are yet to be written. In the mean time, most LazReport features are described in the FreeReport Developer's Manual (see components/lazreport/doc/fr_eng.odt (or fr_eng.pdf) in your Lazarus installation). Platform specific things described there like OLE objects are not implemented in LazReport, also some examples or pictures make reference to samples available only on Delphi.
32+
33+Until LazReport documentation is elaborated, this Wiki page will be used as a documentation container; maybe in the future the missing documentation could be generated from here. Users are welcomed to add topics that they feel need to be documented.
34+
35+### Operators
36+
37+The following operators are supported:
38+
39+| Operator type | Operators |
40+|----|----|
41+| Logical | \>, \<, BUT, AND, NOT, =, \< \>, \> =, \< = |
42+| Mathematical\|\|-, *, +, MOD, / | |
43+
44+### Functions
45+
46+The following functions are available for use:
47+
48+#### Statistical functions
49+
50+SUM(\<X \>)
51+Returns the sum of values represented by \<X\>, which is generally a data field.
52+
53+AVG(\<X \>)
54+Returns the average of \<X\> values.
55+
56+COUNT
57+Count the number of occurrences.
58+
59+MIN(\<X \>)
60+Returns the lowest value of all values represented by \<X\>
61+
62+MAX(\<X \>)
63+Returns the highest value of all values represented by \<X\>
64+
65+#### Date functions
66+
67+> **Note:** December 2013: inspection of lr_pars source code seems to indicate these functions work with the current locale date format, which limits interoperability. To do: verify this; extend function with an extra format parameter which defaults to e.g. ISO8601
68+
69+FORMATDATETIME(\<X\>, \<Y \>)
70+Returns \<Y\> TDateTime in format \<X\>. \<X\> uses the default syntax for [Lazarus formats](http://www.freepascal.org/docs-html/rtl/sysutils/formatdatetime.html).
71+
72+FORMATFLOAT(\<X\>, \<Y \>)
73+Returns the numeric value \<Y\> in format \<X\>. \<X\> uses the default syntax for [Lazarus formats](http://www.freepascal.org/docs-html/rtl/sysutils/formatfloat.html).
74+
75+STRTODATE(\<X \>)
76+Returns the TDateTime(? not date?) representation of string \< X \>. \< X \> must be formatted correctly.
77+
78+STRTOTIME(\<X \>)
79+Returns the TDateTime time part representation of string \< X \>. \< X \> must be formatted correctly.
80+
81+#### String functions
82+
83+LOWERCASE(\<X \>)
84+Returns lowercase \<X\>
85+
86+UPPERCASE(\<X \>)
87+Returns uppercase \<X\>
88+
89+NAMECASE(\<X \>)
90+Returns \<X\> with initial capitals
91+
92+COPY(\<X\>, \<Y\>, \<Z \>)
93+Similar to Pascal Copy: returns string \<X\> from position \< Y \> for \< Z \> characters.
94+
95+#### Numeric functions
96+
97+FRAC(\<X \>)
98+Returns the decimal part of number \< X \>
99+
100+INT(\<X \>)
101+Returns the integer part of number \< X \>
102+
103+ROUND(\<X \>)
104+Returns the number \< X \> rounded to an integer (up? down? nearest integer? banker's rounding?)
105+
106+STR(\<X \>)
107+Returns the string representation of number \< X \>
108+
109+#### Logical/boolean functions
110+
111+IF(\<X\>, \<Y\>, \<Z\>)
112+Returns \< Y \> if expression \< X \> is true. If false, returns \< Z \>.
113+
114+### Adding your own functions
115+
116+Though it is probably not needed much, LazReport allows you to define your own functions. As mentioned, see [[LazReport Documentation|LazReport Documentation#Documentation]] for an overview of existing functions.
117+
118+To add your own functions:
119+
120+#### Add template to definition file
121+
122+> **Note:** This was taken from the Delphi/FastReport/FreeReport tutorial in Portuguese. It should be verified and probably modified to work with current Lazarus/LazReport
123+
124+You need to define your functions following the following template in the file FR.lng (**is this still the correct name for Lazreprot?**):
125+
126+, FCT = CATEGORY\|FCT (\<X\> \<Y\>, \<z\>)\|Help
127+
128+where:
129+
130+- FCT is the function name.
131+- CATEGORY is the category name.
132+- FCT (\<X\> \<Y\>, \<z\>), is the layout of the required parameters, with a maximum of 3 parameters.
133+- Help Is a small help text which should allow the user to understand the purpose of the function.
134+- \| is a separator.
135+
136+Each parameter is separated by commas and can be an expression, a constant, a variable, a field or data. Alphanumeric type constants should be limited by a character. The variables are delimited by **and**.
137+
138+Data fields are limited to the variables and have the following format:
139+
140+Dataset_Name. "Field_Name"
141+
142+Let's add the SQRT and POS functions by adding the following lines to **FR.lng** todo: still the same name?
143+
144+SQRT = Mathematics and Trigonometry\|SQRT(\<X\>)\|Returns the square root of \<X\>\
145+POS = Strings\|POS(\<X\>,\<Y\>) Returns the position of string \<X\> within string \<Y\>
146+
147+#### Add function source code
148+
149+A function definition is nice, but we haven't specified how these functions work. Therefore, some source code to add the functions POS and SQRT below:
150+
151+``` pascal
152+Unit FR_OGFct;
153+
154+interface
155+
156+implementation
157+
158+uses FR_Pars, FR_Class; // Standard declarations needed for Lazreport!! Note: FR_pars etc? Are these names ok?
159+
160+type
161+ // Our own functions are declared here.
162+ TfrOGFunctionLibrary = class(TfrFunctionLibrary)
163+ public
164+ constructor Create; override;
165+ procedure DoFunction(FNo:integer procedure; p1, p2,
166+ p3:Variant; var valley:string); override;
167+ p3:Variant; var valley:string);
168+ end;
169+
170+//**************************
171+// * TfrOGFunctionLibrary
172+constructor TfrOGFunctionLibrary.Create;
173+begin
174+ inherited Create;
175+ // Add our functions to the list of available functions:
176+ with List do
177+ begin
178+ Add('POS');
179+ Add('SQRT');
180+ end;
181+end;
182+
183+Procedure TfrOGFunctionLibrary.DoFunction(FNo:Integer procedure;
184+ p1, p2, p3:Variant; Var valley:String); //valley?
185+Var
186+ Par1, Par2: Varying;
187+ Result: Variantying; //variantying? variant?
188+begin
189+ Try
190+ Case FNo of //this is the index of the functions as added in the constructor
191+ 0 :
192+ begin // POS function
193+ Par1:=VarToStr(Parser.Calc(p1));
194+ Par2:=VarToStr(Parser.Calc(P2));
195+ Result:=Pos(Par1,Par2);
196+ end;
197+ 1 : // SQRT function
198+ Result:=SQRT(Parser.Calc(P1));
199+ end;
200+ Except
201+ // String with error details
202+ Result:='Error in function '+List.Strings[FNo];
203+ end;
204+ Val:=VarToStr(Result); // Resulting string
205+end;
206+
207+Procedure DoInit;
208+begin
209+ frRegisterFunctionLibrary(TfrOGFunctionLibrary);
210+end;
211+```
212+
213+Save the report then File\>List and this window will appear:
214+
215+<figure>
216+<img src="imagem21.jpg" title="imagem21.jpg" />
217+<figcaption>imagem21.jpg</figcaption>
218+</figure>
219+
220+You may notice the appearance of a small (...) button, active only in the case of an expression. Click below to open the editor:
221+
222+<figure>
223+<img src="imagem22.jpg" title="imagem22.jpg" />
224+<figcaption>imagem22.jpg</figcaption>
225+</figure>
226+
227+You may notice the similarity. If you click the Add button it will load the editor parameters:
228+
229+<figure>
230+<img src="imagem23.jpg" title="imagem23.jpg" />
231+<figcaption>imagem23.jpg</figcaption>
232+</figure>
233+
234+Depending on the number of parameters, you have more or less edit areas. The (...) button makes it possible to open the expression generator to set the parameter.
235+
236+### Export Filters
237+
238+LazReport has the ability to save the report displayed in the preview window using a user-defined format. It has an extension mechanism with support for plugins that can be used to add any amount of export filters.
239+
240+An export filter is a specially designed class which is registered in LazReport and allows you to save the report using one or more formats.
241+
242+When LazReport is installed, it includes some filters ready for use, these are: TfrTextExportFilter for exporting report as plain text, TfrCSVExportFilter used for exporting the report as CSV format and TfrHTMExportFilter for exporting the report as simple html file. Also included are some packages that provide additional filters, these are (see Menu Package-\>Install/Uninstall Packages ...): lrCairoExport (components/lazreport/source/addons/cairoexport/lrCairoExport.lpk) which provides support for exporting to PDF and PS formats by using the Cairo Graphics Library and lr_extexp (components/lazreport/source/addons/imgexport/lr_extexp.lpk) with support for improved html export and some image formats like BMP, JPG and PNG. Also included in LazReport is the LazReportPDFExport (components/lazreport/source/addons/pdfexport/LazReportPDFExport.lpk) package, this package provides PDF export using [[PowerPDF]] which is available in Lazarus-CCR repository.
243+
244+A filters package is installed in the same way as any other Lazarus Package; after restarting Lazarus, the filters (usually) appear as icons in the LazReport tab on the Component Palette.
245+
246+An export filter is added to your application by simply dragging one of these icons to a form or data module where TfrReport component exists, this is automatically registered and remains ready to use.
247+
248+This can also be done manually by including the export filter unit name in the Uses clause of the unit where TfrReport is used.
249+
250+LazReport export filters are invoked using one of two ways:
251+
252+1. Save the report in the preview window. Using this method (pressing the Save button) when the Save As dialog is presented, the user can choose a format from the list of registered formats. The list is populated by all registered export filters.
253+2. Through code using the TfrReport ExportTo method. See below for an example:
254+
255+``` pascal
256+ if TheReport.PrepareReport then
257+ TheReport.ExportTo(TfrHTMExportFilter, 'exportedfile.html');
258+```
259+
260+Where TheReport holds an instance of TfrReport component. In this sample a TfrHTMExportFilter is used to generate a file named 'exportedfile.html'. It's not necessary to prepare the report again if it has been prepared previously.
261+
262+In order to use TfrHTMExportFilter the developer has to drag and drop an instance of the TfrHTMExportFilter component from the LazReport tab in the component palette to the form in the Form Designer. As an alternative the unit **lr_e_htm.pas** file can be added to the unit uses clause.
263+
264+Since LazReport 0.9.6, export filters support has been enhanced: now export filters can take parameters which users can customize either by changing values directly or by presenting the end user some UI. In order to make changes in parameters, the developer can create an event handler for TfrReport.OnExportFilterSetup event, available by selecting the TfrReport component and selecting the Events tab in Object Inspector.
265+
266+The OnExportFilterSetup (of type TExportFilterSetup) event handler takes an argument sender of type TfrExportFilter, in order to use this type, the **lr_class.pas** unit must be added to the unit uses clause. All ExportFilter clases share this event and the developer has to type-cast the sender argument to the desired export filter class, for example:
267+
268+``` pascal
269+if sender is TfrHTMExportFilter then
270+begin
271+ TfrHTMExportFilter(sender).UseCSS := false;
272+end;
273+```
274+
275+Below a description of available export filters.
276+
277+#### TfrExportFilter
278+
279+TfrExportFilter is the base class of all export filters, and defines two properties that can be modified by the developer in the OnExportFilterSetup event:
280+
281+- **BandTypes**: TfrBandTypes. this is a set of band types, only bands included in this set are actually exported; the other bands on the report will be ignored. Using this property a developer could for example export only the master band content, leaving titles, headers and footers out of exported output by doing:
282+
283+``` pascal
284+sender.BandTypes := [btMasterData];
285+```
286+
287+By default, all bands are processed but TfrCSVExportFilter changes this property to process only master header, column header and master data bands.
288+
289+- **UseProgressbar**: boolean. This property enables or disables showing the progress bar while processing the export filter. This property is false by default.
290+
291+#### TfrTextExportFilter
292+
293+inheritance: TfrExportFilter \<- TfrTextExportFilter\
294+located in: components/lazreport/source/**lr_e_txt.pas** file included with the LazReport package.
295+
296+TfrTextExportFilter is the base class of text based export filters. This export filter tries to make a text representation of a graphical report by fitting the original graphical coordinates into a more coarse grid where each unit is of "UsedFont" pixels, depending on the value of UsedFont value, the exported output may more or less represent the layout of objects in graphical report. Beside the properties inherited from TfrExportFilter class, TfrTextExportFilter define two more properties.
297+
298+- **UsedFont**:integer. this property define the pixel dimensions on the output grid, objects on report are fitted into this grid by reclaculating each x and y position. The default value is 10, if user changes this value to 0 or less, LazReport will show automatically a dialog asking for the UsedFont value, if user enter a invalid value, a 10 value will be used. The 10 value is used because is the value that better fits the usual reports which are made with fonts 10-13 points.
299+- **UseBOM**:boolean. This property enable the inclusion of UTF-8 Byte Order Mark character at the start of text output (see [BOM](http://en.wikipedia.org/wiki/Byte_order_mark)) needed by some editors/viewers, by default no BOM is used in exported output.
300+
301+#### TfrCSVExportFilter
302+
303+inheritance: TfrExportFilter \<- TfrTextExportFilter \<- TfrCSVExportFilter\
304+located in: components/lazreport/source/**lr_e_csv.pas** file included in LazReport package.
305+
306+This special text export filter produces Comma Separated Value output (actually any character can be used as separator), it differs from its ancestor in that it doesn't try to create text layout representation of graphical report, instead, for each record output it tries to guess the fields order from the source report, it then produce a list of fields using a separator defined by the user. Beside the properties inherited from its ancestor classes it defines some properties to customize the generated output.
307+
308+- **QuoteType**:TfrQuoteType. This property controls whether the generated field value should be wrapped using specified quote char or not. Possible values are *qtNone*, *qtQuoteChar* and *qtAutoQuote*. With *qtNone* the field value is never wrapped, *qtQuoteChar* will use the character specified by property **QuoteChar**, any instance of **QuoteChar** already present in field value is duplicated. *qtAutoQuote* first try to find if any instance of **separator** or **QuoteChar** is already present in field value, if affirmative it behaves as if *qtQuoteChar* has been specified, on the contrary case, the field value is not wrapped just as if *qtNone* has been specified. **QuoteType** property is set to *qtQuoteChar* by default.
309+- **QuoteChar**:TUTF8Char. This holds the character to be used to wrap the field value in case of **QuoteType** of value *qtQuoteChar* has been specified or deduced if *qtAutoQuote* is set. Any instance of this character in the field value will be duplicated. by default **QuoteChar** is set to the " character.
310+- **Separator**:TUTF8Char. This is the character used to separate the fields in each record, by default is set to the ',' (COMMA) character but any UTF-8 valid character could be used. Some CSV files are actually TAB separated files, to get this, set Separator:=#9;
311+
312+The CSV exporter do not use the inherited UsedFont property value so any value set will be ignored. With default property values, TfrCSVExportFilter will produce Excel compatible files, the only caveat is that Excel will not recognize the file as UTF-8 encoded. To force Excel to recognize the encoding automatically, set the property **UseBOM** to true.
313+
314+#### TfrHTMExportFilter
315+
316+inheritance: TfrExportFilter \<- TfrTextExportFilter \<- TfrHTMExportFilter.\
317+Located in: components/lazreport/source/**lr_e_htm.pas** in LazReport package.
318+
319+This special text export filter produces valid "HTML 4.01 Transitional" output. Currently it defines only one additional property.
320+
321+- **UseCSS**:boolean. This property controls whether or not the produced output include CSS information, this property is set to true by default.
322+
323+### Scripting Language
324+
325+A simple scripting language (interpreter) for a Pascal-like language is included in LazReport.
326+
327+Some notes:
328+
329+- the interpreter seems to expect semicolons after each line
330+- the interpreter cannot handle nested if statements
331+
332+Example:
333+
334+``` pascal
335+ if [[PAY]>0] then begin
336+ if[[LATE_FEE]=1] then Text:=[MSGL2];
337+ else Text:= [MSGL1];
338+ end;
339+```
340+
341+The above won't compile, while this will:
342+
343+``` pascal
344+ if[[LATE_FEE]=1] then Text:=[MSGL2]
345+ else Text:= [MSGL1];
346+```
347+
348+Source: [forum thread](http://forum.lazarus.freepascal.org/index.php/topic,22902.0.html).
349+
350+### Examples
351+
352+You can find some examples in your Lazarus installation under components\lazreport\samples.
353+
354+#### Running total
355+
356+To calculate the total for values of each page added to the total on the previous page, follow these steps:
357+
358+- Make sure you have a report with a master data band connected to a dataset
359+- Place a text inside that band and double click to open its editor.
360+- In the editor, check the script option on the top left.
361+- In the script window, paste the following code
362+
363+``` pascal
364+ IF [Line#] = 1 then RunTotal := [Dbf1."MTH_NO"]
365+ else RunTotal := RunTotal +[Dbf1."MTH_NO"];
366+```
367+
368+
369+of course you need to replace \[dbf1."MTH_NO"\] with your field, the one you want a running total.
370+
371+- Click OK to save the changes.
372+- Reopen the editor by double clicking on the text object
373+- Click the "Variable" button.
374+- Select the fr variables category in the drop down combo.
375+- Double click the RunTotal variable to be placed on Memo.
376+- Click OK and then press Preview.
377+
378+Source: [Issue \#17198](https://bugs.freepascal.org/view.php?id=17198)
379+
380+### Tutorial
381+
382+Please see [[LazReport Tutorial]].
383+
384+### See also
385+
386+- [Creating and printing a report](http://www.pp4s.co.uk/main/tu-db-firebird-demo3.html)
387+
388+## Report Designer
389+
390+- Menu: File-\>Save As ...
391+ - Templates Notice:
392+ - While saving the current report, the user can select an arbitrary directory in the Save As dialog, in the case of templates (.lrt extension), the selected directory is ignored. Templates are always stored in the **Templates Directory**. The **Templates Directory** value can be set by either changing the frTemplateDir global variable (lr_desgn.pas unit) or by setting the TemplateDir property in a TfrDesigner component. The TemplateDir property will overwrite the frTemplateDir global variable if both are used. The **Templates Directory** can be an absolute path or a relative to program path directory, if this directory do not exists, LazReport will try to create it on the first use. If the TemplateDir property or the frTempateDir variable are not set, LazReport will try to save the templates in the program directory.
393+ - Binary templates format (.frt) is not recommended, as binary formats are hard to keep compatible with newer (or older) LazReport versions. If possible use .lrt file extension (XML file format).
394+
395+## Quick Start
396+
397+### Report Records from a Dataset
398+
399+In this sample we will design a report to print records from a dataset derived component (TDbf, TSQLQuery, TZTable, TZQuery, Etc.). First it's assumed that LazReport is already installed and the dataset component, called "Dbf1" here, is already configured and active.
400+
401+1. From LazReport tab, select TfrReport component and drop it in Form Designer, it will be named "frReport1".
402+2. Drop a TfrDbDataset component, it will be named "frDbDataset1"
403+3. Drop a TButton, it will be named Button1, change its name to "btnShowReport"
404+4. With frDbDataset1 selected in Object Inspector select "Dbf1" for the "Dataset" property
405+5. Now select frReport1 and using the same procedure link its "Dataset" property to "frDbDataset1":
406+ <center>
407+
408+ <figure>
409+ <img src="lrformsetup.png" title="lrformsetup.png" />
410+ <figcaption>lrformsetup.png</figcaption>
411+ </figure>
412+
413+ </center>
414+6. Right Click frReport1 and select "Design Report" from the menu, LazReport report designer will appear.
415+7. From Designer window, select menu Tools-\>Tools-\>Insert DB Fields, Insert fields Dialog will appear.
416+8. In field list select the fields you want in the report
417+9. Check "Include Headers" and "Include Bands" options, the result should be something like this:
418+ <center>
419+
420+ <figure>
421+ <img src="lrinsertfieldsdialog.png" title="lrinsertfieldsdialog.png" />
422+ <figcaption>lrinsertfieldsdialog.png</figcaption>
423+ </figure>
424+
425+ </center>
426+10. Press the "Ok" button, LazReport will arrange bands and fields like this:
427+ <center>
428+
429+ <figure>
430+ <img src="lrreportwithfields.png" title="lrreportwithfields.png" />
431+ <figcaption>lrreportwithfields.png</figcaption>
432+ </figure>
433+
434+ </center>
435+11. Press the preview button ![](/wiki-images/b/bb/Lrprevieweye.png), LazReport will then arrange things and show:
436+ <center>
437+
438+ <figure>
439+ <img src="lrpreviewreport.png" title="lrpreviewreport.png" />
440+ <figcaption>lrpreviewreport.png</figcaption>
441+ </figure>
442+
443+ </center>
444+12. Save the report with Menu File-\>Save, select the same directory as the current project and name it as listing1.lrf
445+13. Close the report designer.
446+14. Double click btnShowReport and type the following code:
447+
448+``` pascal
449+ frReport1.LoadFromFile('listing1.lrf');
450+ frReport1.ShowReport;
451+```
452+
453+## Bug reports
454+
455+Please report problems using the [lazarus/freepascal bugtracker](http://www.freepascal.org/mantis/main_page.php), project: "Lazarus Packages", Category "LazReport".
456+
457+For patches, please submit a bug report and attach the patch to it.
458+
459+---
460+*From the Free Pascal wiki, page "LazReport Documentation", as it stood on 2022-02-05 after a change by Slawek, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history