Recent

Author Topic: Problem with LazReport and printing - index out of range  (Read 6677 times)

AKCarlow

  • New Member
  • *
  • Posts: 45
Problem with LazReport and printing - index out of range
« on: July 16, 2014, 06:57:53 pm »
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
Code: [Select]

  frReport1.LoadFromFile('ArrearsTest.lrf');
  frReport1.ShowReport;

For printing, I've copied the code from the tutorial, changing only the report filename:

Code: [Select]
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.
Code: [Select]
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?
Using: OS-X 10.6.8, Lazarus 1.2.4, FPC 2.6.4,  MySQL 5.6.19 (32-bit)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Problem with LazReport and printing - index out of range
« Reply #1 on: July 17, 2014, 12:18:45 pm »
With the following code (from the tutorial I suspect, but I have modified it working on it since):
Code: [Select]
  // Load report definition from application directory
  AppDirectory:=ExtractFilePath(ParamStr(0));
  frReport1.LoadFromFile(AppDirectory+'lazreporttutorial.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;
on Windows, x86 Lazarus trunk (so some differences to your system), it works. By default it shows the printer selection with all pages selected; printing works fine.

There may have been fixes in Lazreport and or/OSX printer handling since 1.2.4 though...

What about other Lazarus-based printing e.g. as described in
http://wiki.lazarus.freepascal.org/Using_the_printer
Does that work?

PS: There's a separate Lazreport subforum you could use for posting Lazreport-related issues.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

AKCarlow

  • New Member
  • *
  • Posts: 45
Re: Problem with LazReport and printing - index out of range
« Reply #2 on: July 17, 2014, 07:18:06 pm »
Thanks, BigChimp.

The direct printing in the other tutorial works without problems. Your version of the code from the Lazreport tutorial crashes in the same way, at the same point (ie at the with EMFPages[0]^ in LR_Class.

I'll try hunting in the LazReport subforum - missed that one - and also I'll try printing a different report from LR.
Using: OS-X 10.6.8, Lazarus 1.2.4, FPC 2.6.4,  MySQL 5.6.19 (32-bit)

AKCarlow

  • New Member
  • *
  • Posts: 45
[SOLVED] Re: Problem with LazReport and printing - index out of range
« Reply #3 on: July 18, 2014, 02:00:16 pm »
It's the little things that trip us up.  8)

In the tutorial the code for the preview button has "frReport1.LoadFromFile(...);" while the code for the print button has "frReport1.LoadFromFile(AppDirectory+...);". Preview worked fine, print crashed with index out of range - rather than the maybe more helpful "can't find file" message. Removing the AppDirectory+ fixed it.

If I set a breakpoint there the debugger tells me that AppDirectory is "Users/austink/Desktop/Tryit/project1.app/Content/MacOS/" - the report file is actually in the Tryit folder.

I can get back to learning how to use LazReport.
Using: OS-X 10.6.8, Lazarus 1.2.4, FPC 2.6.4,  MySQL 5.6.19 (32-bit)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Problem with LazReport and printing - index out of range
« Reply #4 on: July 18, 2014, 02:56:02 pm »
Ah yes... I updated the wiki article - please feel free to do so yourself on this and other pages - contributions are more than welcome!

Thanks for figuring this out and getting back... sorry you hit this problem.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018