Recent

Author Topic: New feature to export report to file handle  (Read 294 times)

tecnoxarxa

  • Newbie
  • Posts: 3
New feature to export report to file handle
« on: April 01, 2019, 02:45:07 am »
Hello everyone,

I added a new feature to export report to file handle. With this you can export to stdout without temporal files, and using ony ram.
I like to share this with community, but I don't know how.

This is a source copy:

File lr_class.pas

Add to uses: pipes

type TfrReport add to public: function ExportToFileHandle(FilterClass: TfrExportFilterClass; FileHandle :THandle):Boolean;

function source:

function TfrReport.ExportToFileHandle(FilterClass: TfrExportFilterClass; FileHandle :THandle
  ): Boolean;
var
  s: String;
  i: Integer;
  MemStream: TMemoryStream;
  Outstream: TOutputPipeStream;
begin
  // try to find a export filter from registered list
  if (FilterClass=nil) and (fDefExportFilterClass<>'') then
  begin
    for i:=0 to ExportFilters.Count - 1 do
      if (ExportFilters.FClassRef.ClassName=fDefExportFilterClass) then
      begin
        FilterClass := ExportFilters.FClassRef;
        break;
      end;
  end;

  if FilterClass=nil then
    raise Exception.Create(sNoValidFilterClassWasSupplied);

  OutStream := TOutputPipeStream.Create(FileHandle);
  MemStream := TMemoryStream.Create();

  FCurrentFilter := FilterClass.Create(MemStream);
  try
    CurReport := Self;
    MasterReport := Self;

    FCurrentFilter.OnSetup:=CurReport.OnExportFilterSetup;

    if FCurrentFilter.Setup then
    begin
      FCurrentFilter.OnBeginDoc;

      SavedAllPages := EMFPages.Count;

      if FCurrentFilter.UseProgressbar then
      with frProgressForm do
      begin
        s := sReportPreparing;
        if Title = '' then
          Caption := s
        else
          Caption := s + ' - ' + Title;
        FirstCaption := sPagePreparing;
        Label1.Caption := FirstCaption + '  1';
        OnBeforeModal := @ExportBeforeModal;
        Show_Modal(Self);
      end else
        ExportBeforeModal(nil);

      fDefExportFilterClass := FCurrentFilter.ClassName;
      Result:=true;
      MemStream.Seek(0, soBeginning);
      OutStream.CopyFrom(MemStream, MemStream.Size - 1);
    end
    else
      Result:=false;
  finally
    //is necessary to destroy the file stream before calling FCurrentFilter.AfterExport
    //to ensure the exported file is properly written to the file system
    OutStream.Free;
    MemStream.Free;
  end;
  FCurrentFilter.Stream := nil;

  if Result then
    FCurrentFilter.AfterExport;
  FreeAndNil(FCurrentFilter);
end;

Is posibble add that to official sources?
How?

Thanks!



lucamar

  • Hero Member
  • *****
  • Posts: 1555
Re: New feature to export report to file handle
« Reply #1 on: April 01, 2019, 03:26:56 am »
I like to share this with community, but I don't know how.

Next time (or even now, if you edit your post) zip the source file and add t to your post as an attachment.

Alternatively, to publish single functions or snippets of code, use the "Insert code" button (labeled as "#") to enclose your code with code tags, as in:
[code=Pascal]
...some code ...
[/code]
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.