Tuesday, 11 May 2021

FastReports Picture Issue

 I've been using FastReports for a few months to produce PDF reports and most of the time it seems to work OK, but have one major problem with pictures. Here is what is happening

1) Multiple reports that need images on a report, these images are small icons.

2) The images are stored in the database and the dataset brings them back fine. 

3) Previewing the report in the report designer shows the images OK.

4) Exporting the report to PDF, the images no longer appear. I am exporting the report in code not directly from the preview.

If I put a picture control on the report and load an image, this works OK, it is just when I set the 'DataSet' and 'DataField' properties of the picture component they do not appear.

I think the issue might relate to when I activate the Datasets, but it could be something else like a property I am failing to set.


frxReport := TfrxReport.Create(nil);
frxPDF := TfrxPDFExport.Create(nil);
dsList := TObjectList<TfrxDBDataset>.Create();
try
    frxReport.LoadFromFile(fr3file);
    frxReport.DataSets.Clear;
    frxReport.EngineOptions.SilentMode := true;
    frxReport.EngineOptions.EnableThreadSafe := true;
    frxReport.EngineOptions.UseFileCache := False;


    frxPDF.fileName := aPDFFile;
    frxPDF.ShowDialog := False;
    frxPDF.ShowProgress := False;
    frxPDF.OpenAfterExport := False;
    frxPDF.Compressed := true;
    frxPDF.Background := true;

    // Populate datasets - cannot show the code for this
    for spBandDS in dsList do
    begin
        if not(spBandDS.DataSet as TStoredProc).Active then
        begin
            (spBandDS.DataSet as TStoredProc).Open();
            ActivateDataSource(frxReport, spBandDS);
        end;
    end;
    frxReport.PrepareReport(true);
    frxReport.Export(frxPDF);
finally
    for spBandDS in dsList do
    begin
        spBandDS.DataSet.Close;
    end;      
    dsList.Free;
    frxPDF.Free;
    frxReport.Free;
end;

Has anyone else had similar issues with the Picture component and FastReports?

Latest

I've managed to fix the problem by creating a function that does this:

var
  image: TFrxPictureView;
  m: TMemoryStream;
begin
  if (aDataset.Name = aDatasetName) then
  begin
    image := aReport.FindObject(aImageName) as TFrxPictureView;
    if Assigned(image) then
    begin
      image.DataSet := aDataset;
      if image.IsDataField and image.DataSet.IsBlobField(image.DataField) then
      begin
        m := TMemoryStream.Create;
        try
          image.DataSet.AssignBlobTo(image.DataField, m);
          image.LoadPictureFromStream(m);
        finally
          m.Free;
        end;
      end;
    end;
  end;

Then then passing in the parameters of the report, band dataset, name of the dataset and the image component name. Basically at runtime it needs to assign the dataset again.

No comments:

Post a Comment