unit U_PrintFr3;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Data.Win.ADODB, RM_Common, RM_Class, RM_GridReport,
  RM_E_llPDF, RM_e_Xls, RM_BarCode, RM_RichEdit, RM_Dataset, frxExportXLSX, frxExportPPTX, frxClass,
  frxExportBaseDialog,Vcl.Printers, frxExportXLS, frxTableObject, frxBarcode, frxOLE, frxRich, frxDBSet;

type
  TfrmPrintFr3 = class(TForm)
    frxReport1: TfrxReport;
    frxDBDataset1: TfrxDBDataset;
    frxRichObject1: TfrxRichObject;
    frxOLEObject1: TfrxOLEObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxReportTableObject1: TfrxReportTableObject;
    frxXLSExport1: TfrxXLSExport;
    frxPPTXExport1: TfrxPPTXExport;
    frxXLSXExport1: TfrxXLSXExport;
    ADOQueryReport: TADOQuery;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    procedure getReportFile();
  public
    fFormID:integer;
    fCopyNumber:integer;
    fBLCLID:string;
    fIsPreview:Integer;
    fprinterIndex:Integer;
    constructor Create(
     AOwner: TComponent;
     ACaption: String;
     mFormID:integer;
     recid:string;
     CopyNumber:integer;IsPreview:integer;printerIndex:integer);
   end;

implementation
uses
  U_dataLink,U_CompressionFun;
{$R *.dfm}

constructor TfrmPrintFr3.Create(
  AOwner: TComponent;
  ACaption: String;
  mFormID:integer;
  recid:string;
  CopyNumber:integer;
  IsPreview:integer;printerIndex:integer);
begin
  inherited Create(AOwner);
  if ACaption<>'' then
    Caption := ACaption;
  fFormID:=mFormID;
  fBLCLID:=recid;
  if CopyNumber>0 then
    fCopyNumber:= CopyNumber
    else
      fCopyNumber:=1;
  fIsPreview:=IsPreview;
  fprinterIndex:= printerIndex ;
end;
/////////////////////////////////////////////////////////////
///
procedure TfrmPrintFr3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  action:=cafree;
end;

procedure TfrmPrintFr3.FormShow(Sender: TObject);
begin
    getReportFile();

end;

procedure TfrmPrintFr3.getReportFile();
var
       myStream: TMemoryStream;
       mReportFile:string;
      // ff: TADOBlobstream;
begin
     if fBLCLID='' then
     begin
       showMessage('!未传入打印记录id') ;
       self.Close  ;
       exit;
     end;
   try
     myStream:= TMemoryStream.Create;
     myStream.Position:=0;
     with ADOQueryReport do
     begin
       close;
       sql.Clear;
       sql.Add('select * from Bs_report_Cloud_File');
       sql.Add('where BLCLID='+quotedstr(fBLCLID));
       open;
       if  isEmpty then
       begin
         showMessage('未获取到要打印的文件!') ;
         self.Close  ;
         exit;
       end;
       TBlobField(ADOQueryReport.FieldByName('GenerateFiles')).SaveToStream(myStream);
      // ff := TADOBlobstream.create(fieldByName('GenerateFiles') as TblobField, bmRead);
       //  ff.SaveToStream(myStream);
       mReportFile:= ExtractFilePath(Application.ExeName) +'report\'+Trim(ADOQueryReport.fieldByName('GenerateFileName').AsString);

       myStream.Position:=0;
       if fprinterIndex>=0 then
         frxReport1.PrintOptions.Printer := Printer.Printers[fprinterIndex];

       frxReport1.PreviewPages.LoadFromStream(myStream);
       frxReport1.EngineOptions.IgnoreExprError  :=true;
        if fIsPreview =1 then
        begin
          frxReport1.ShowPreparedReport ;
        end
        else
        begin
          frxReport1.PrintOptions.Copies :=fCopyNumber;
          frxReport1.PrintOptions.ShowDialog:=false;
          frxReport1.Print() ;
        end;
        self.Close;

     end;

    finally
       myStream.Free;
   end;

end;


end.
