Manually duplicating report inside OnPrintReport event

Hello,

I need to print multiple copies of a single report however each copy needs to be a little different. After preparing the report and showing it in preview window, the user selects how many copies to print inside the print dialog and starts printing. Then FastReport fires OnPrintReport event which needs to build the whole report again, once for each copy.
procedure TForm1.frxReportPrintReport(Sender: TObject);
var
  copyNumber: Integer;
begin
  frxReport.PreviewPages.Clear;
  for copyNumber := 1 to frxReport.PrintOptions.Copies do
  begin
    UpdateReportDataForSpecificCopy(copyNumber);
    frxReport.PrepareReport(False);
  end;
  //reset copies because report was duplicated manually
  frxReport.PrintOptions.Copies := 1;
end;
The problem is that each PrepareReport(False) overwrites the previous one and in the end prints only the last copy.

However this only happens if I show the preview window (with frxReport.ShowPreparedReport) before printing.
If I print directly (with frxReport.Print) then everything works fine.

Any ideas how to solve this? I'm using FastReport 5.4 Standard so I can't check the source code.

Thanks! >

Comments

  • edited 2:42PM
    If I understand correctly, you prepare only one generic copy of the report that you show to the user using FastReport default preview form. Then, the user use the print button on that form and select a number of copy on the dialog that open. Then, you want to intercept the print and generate a batch of personalized report.

    I don't see the advantage of using the same TfrxReport class to print that batch of report. Could you simply use another class to print that batch and stop the current printing of the generic report?

    I'm not sure if it is possible without the source code, but you could also inherit the TfrxPreviewForm and modify the behavior of the print button for that particular report(and to link that PreviewForm to your TfrxReport). What I do is that I use my own PreviewForm (created from scratch) that I can customize to my liking.

  • edited 2:42PM
    Solved it by rebuilding preview pages inside OnPrintReport:
    frxReport.PreviewPages.Clear;
    for copy := 1 to frxReport.PrintOptions.Copies do 
    begin
      //... modify source data for current copy;
      frxReport.PrepareReport(False);
    end;
    //override copies because they were already prepared
    frxReport.PrintOptions.Copies := 1;
    
    Used in production for about a month and it works great. Although it's rarely used to print more than 3 pages in total.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.