I'm trying to learn to use LazReport and the Printing unit, following the tutorial at
http://wiki.lazarus.freepascal.org/LazReport_Tutorial. At design time I can preview the report, and it successfully retrieves the data from my MySQL database and formats it - about 20 lines. At run time I can also preview the report, using
frReport1.LoadFromFile('ArrearsTest.lrf');
frReport1.ShowReport;
For printing, I've copied the code from the tutorial, changing only the report filename:
procedure TfPrinting.Button2Click(Sender: TObject);
var
FromPage, ToPage, NumberCopies: Integer;
ind: Integer;
AppDirectory: String;
Collap: Boolean;
begin
// Load report definition from application directory
AppDirectory:=ExtractFilePath(ParamStr(0));
frReport1.LoadFromFile(AppDirectory+'ArrearsTest.lrf');
// Need to keep track of which printer was originally selected to check for user changes
ind:= Printer.PrinterIndex;
// Prepare the report and just stop if we hit an error as continuing makes no sense
if not frReport1.PrepareReport then Exit;
// Set up dialog with some sensible defaults which user can change
with PrintDialog1 do
begin
Options:=[poPageNums ]; // allows selecting pages/page numbers
Copies:=1;
Collate:=true; // ordened copies
FromPage:=1; // start page
ToPage:=frReport1.EMFPages.Count; // last page
MaxPage:=frReport1.EMFPages.Count; // maximum allowed number of pages
if Execute then // show dialog; if succesful, process user feedback
begin
if (Printer.PrinterIndex <> ind ) // verify if selected printer has changed
or frReport1.CanRebuild // ... only makes sense if we can reformat the report
or frReport1.ChangePrinter(ind, Printer.PrinterIndex) //... then change printer
then
frReport1.PrepareReport //... and reformat for new printer
else
exit; // we couldn't honour the printer change
if PrintDialog1.PrintRange = prPageNums then // user made page range selection
begin
FromPage:=PrintDialog1.FromPage; // first page
ToPage:=PrintDialog1.ToPage; // last page
end;
NumberCopies:=PrintDialog1.Copies; // number of copies
// Print the report using the supplied pages & copies
frReport1.PrintPreparedReport(inttostr(FromPage)+'-'+inttostr(ToPage), NumberCopies);
end;
end;
end;
When I try to print, after a delay the print dialog shows - when I select print I get the error message "Project raised exception class 'EListError' with message List index(0) out of bounds ...". Choosing break stops the program at this code in the LR_Class unit.
with EMFPages[0]^ do
begin
Prn.SetPrinterInfo(pgSize, pgWidth, pgHeight, pgOr);
Prn.FillPrnInfo(PrnInfo);
end;
When I preview the report at design time a message "processing page 1" appears before the preview. When I ask to print at runtime the similar message is "processing page 0", and in the printer dialog the From/To page range options are preset to 1 and 0.
I suspect I am doing something wrong - any ideas?