Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Intraweb and fast report
#4
function TrDMRepTOR.fxProcess_Report(aProgramSwitch: string;
  aDataModule: TDataModule;
  aDatasetLst: TObjectList;
  aReportObjID: string;
  aRunDate: TDateTime;
  aExportFileExtType: string;
  aRenderType: EN_RenderType;
  aRptParamList: DataParameterArray;
  var aPdfStream: TROBinaryMemoryStream;
  aFileName: string = '';
  aPdfPassword: string = '';
  aShareName: string = ''): boolean;

var
  lAvailDS: TEdDataSets;
  lAvailQrys: TEdDataQueries;
  lAvailDBCxn: TEdDatabase;
  lFrxReport: TfrxReport;
  lTempFileName: AnsiString;
  lURL: string;
  {$IFDEF IS_WIN32}
    lErrorCode: integer;
  {$ENDIF}

  {$IFDEF  USE_GNOSTICE}
    lgtFRExportIntf: TgtFRExportInterface;
    lgtPDFEngine: TgtPDFEngine;
  {$ELSE}
    lFrxPDFExport: TfrxPDFExport;
  {$ENDIF}
  lRptVars: U_ReportVars;
  lVer, lTitle : string;
  lReportID: string;
  lReportOrigin: EN_ReportOrigin;
  lReportType: EN_ReportType;
  lUAAdhocSQL: UA_AdhocQryParam;
  lTemplateID: string;

  procedure fxProcessAsRptDB;
  begin
    fxRetrieveAdhoc(lReportID, aRptParamList, lUAAdhocSQL);
    fxLoadDBReport(lReportID, lFrxReport, lRptVars);
    lFrxReport.CheckDataPage;
    // BIND DBCXN TO REPORT
    lAvailDBCxn := TEdDatabase.Create(lFrxReport, 'DBCxn');
    lAvailQrys  := TEdDataQueries.Create;
    fxRetrieveDBCxnInfo(lReportID, lAvailDBCxn);
    // SET PARAMS PER IS_RUNTIME FLAG
    // fxRetrieveRunTimeParams( lUAAdhocSQL );
    // SET QRY
    lAvailQrys.fxPopulateWithAdhocSQL(lFrxReport, lAvailDBCxn, lUAAdhocSQL);
    lAvailQrys.fxRegisterWithReport(lFrxReport);
    lAvailQrys.fxActivateNeededQueries(lFrxReport, False, lUAAdhocSQL, aRptParamList);
  end;

  procedure fxProcessAsRptFile;
  begin
    lAvailDS := TEdDataSets.Create;
    // Make sure that this is set to false so that the frxdb data sources will not be freed; otherwise on the 2nd pass
    // the prepare report will fail.
    aDatasetLst.OwnsObjects := False;
    lAvailDS.fxPopulateWithTDataset(aDataModule, aDatasetLst);
    lAvailDS.fxRegisterWithReport(lFrxReport);
    // Loading the report afterward because in this case the report has the definition and the data is in the datamodule.
    fxLoadFileReport(lReportID, lFrxReport, lVer, lTitle);
    lAvailDS.fxActivateNeededDatasets(lFrxReport, True);
  end;


begin
  Result := False;
  // WE SET lAvailDS = NIL.. because for some reason it appears as being assigned.  NO IDEA WHY
  lAvailDS := nil;
  // Trap for some unforescene error
  try
    // SET SOURCE OF REPORT (IE FILE OR DB)
    if ((aDataModule <> nil) and (aDatasetLst.Count <> 0)) then
    begin
      lReportOrigin := roFile;
      lReportID := aReportObjID;
    end else
    begin
      lReportOrigin := roDB;
      //get the information that is needed to process the report
//      lReportID := fxRetrieveReportID(aReportObjID, aRunDate, lReportType);
      lReportID := fxRetrieveReportID(aReportObjID, aRunDate);
    end;


    lUAAdhocSQL := UA_AdhocQryParam.Create(nil);
    lRptVars    := U_ReportVars.Create(nil);
    lFrxReport  := TfrxReport.Create(nil);
    try
      // this code is very important and needs to be called right after the report is created.
      lFrxReport.EngineOptions.EnableThreadSafe    := True;
      lFrxReport.EngineOptions.UseGlobalDataSetList := False;
      lFrxReport.ReportOptions.Compressed          := True;
      lFrxReport.EngineOptions.SilentMode          := True;
      //https://www.youtube.com/watch?v=PUUQ7LUbhD4
      lFrxReport.EngineOptions.DestroyForms        := True;
      lFrxReport.EngineOptions.UseFileCache        := False;
      lFrxReport.ShowProgress                      := False;
      lFrxReport.OnLoadTemplate                    := frxReportLoadTemplate;

      lTemplateID := fxLoadTemplate(aReportObjID);
      if lTemplateID <> '' then
      begin
        lFrxReport.InheritFromTemplate(lTemplateID, imRename);
      end;

      if lReportOrigin = roDB then
      begin
        fxProcessAsRptDB
      end else
      begin
        fxProcessAsRptFile;
      end;

      if lTemplateID <> '' then
        frxReportLoadTemplateScript(lFrxReport, lTemplateID);

      // BIND PARAMS
      {$IFDEF IS_MT}
      lRptVars.zVarLogoPath := HDM.gLogoPath;
      {$ELSE}
      lRptVars.zVarLogoPath := Controller.gLogoPath; // IW and WinApp
      {$ENDIF}
      fxAssignParams(lFrxReport, lRptVars, aRptParamList);


      // CURRENTLY NO OTHER EXPORT TYPES OTHER THAN PDF BUT COULD DO XLS, ETC
      {$REGION 'EXPORT AS PDF'}
      if (UPPERCASE(aExportFileExtType) = 'PDF') then
      begin
        {$IFDEF  USE_GNOSTICE}
        lgtFRExportIntf := TgtFRExportInterface.Create(nil);
        lgtPDFEngine    := TgtPDFEngine.Create(nil);
        {$ELSE}
        lFrxPDFExport := TfrxPDFExport.Create(nil);
        {$ENDIF}
        try
          if aFileName = '' then
            lTempFileName := fxGetSmallGUID
          else
            // GENERATE UNIQUE FILE_NAME
            lTempFileName := aFileName + '_' + uAppCommon.fxGetFileSafeDT(NOW);

          if aShareName <> '' then
            lTempFileName := aShareName + lTempFileName;

          lTempFileName := lTempFileName + '.' + aExportFileExtType;

          {$IFDEF  USE_GNOSTICE}
            lgtPDFEngine.Font.Name := 'Garamond';
            lgtPDFEngine.Font.Size := 12;
            lgtPDFEngine.FontEmbedExcludeList.Add('Arial');
            lgtPDFEngine.Preferences.EmbedTrueTypeFonts := etfNone; // etfSubset; // etfFull; etfSubset etfNone;
            { TO IMPLEMENT LATER
              if aPdfPassword <> '' then
              begin
              lgtPDFEngine.Encryption.Level        := el128Bit;
              lgtPDFEngine.Encryption.Enabled      := True;
              lgtPDFEngine.Encryption.OwnerPassword := aPdfPassword + '_owner';
              lgtPDFEngine.Encryption.UserPassword  := aPdfPassword + '_user';
              end;
            }
            lgtFRExportIntf.Engine := lgtPDFEngine;


            lgtPDFEngine.Preferences.OutputToUserStream := True;
            IgtDocumentEngine(lgtFRExportIntf.Engine).FileName := '';
            lgtPDFEngine.UserStream := aPdfStream as TMemoryStream;

            IgtDocumentEngine(lgtFRExportIntf.Engine).Preferences.ShowSetupDialog := False;
            IgtDocumentEngine(lgtFRExportIntf.Engine).Preferences.OpenAfterCreate := False;
            IgtDocumentEngine(lgtFRExportIntf.Engine).Preferences.ProcessAfterEachPage := False;
            lgtFRExportIntf.ShowSaveDialog := False;
          {$ELSE}
            lFrxPDFExport.Stream          := aPdfStream as TMemoryStream;
            lFrxPDFExport.DefaultExt      := aExportFileExtType;
            lFrxPDFExport.HideWindowUI    := True;
            lFrxPDFExport.OpenAfterExport := False;
            lFrxPDFExport.ShowDialog      := False;
            lFrxPDFExport.ShowProgress    := False;
            lFrxPDFExport.UseFileCache    := False;
            lFrxPDFExport.Compressed      := True;
            lFrxPDFExport.CenterWindow    := False;
            lFrxPDFExport.FitWindow      := True;
          {$ENDIF}
          try
            if lFrxReport.PrepareReport(True) then
            begin
              {$IFDEF  IS_IW}
                begin
                  {$IFDEF  USE_GNOSTICE}
                    lgtFRExportIntf.RenderDocument(lFrxReport, False, False);
                  {$ELSE}
                    lFrxReport.Export(lFrxPDFExport);
                  {$ENDIF}
                  aPdfStream.Position := 0;
                  lURL := TIWAppCache.StreamToCacheFile(WebApplication, aPdfStream, CGGetContentTypeForExtension(aExportFileExtType), ctSession);
                  fxRenderStaticDoc(lURL
                    , aRenderType
                    , lTempFileName
                    );
                end;
              {$ENDIF}

              {$IFDEF IS_WIN32}
                begin
                  lErrorCode := 0;
                  {$IFDEF  USE_GNOSTICE}
                    lgtFRExportIntf.RenderDocument(lFrxReport, False, False);
                  {$ELSE}
                    lFrxReport.Export(lFrxPDFExport);
                  {$ENDIF}
                  aPdfStream.Position := 0;
                  aPdfStream.SaveToFile(lTempFileName);
                end;
              {$ENDIF}

              {$IFDEF IS_MT}
                begin
                  {$IFDEF  USE_GNOSTICE}
                    if aShareName <> '' then
                    begin
                      lgtFRExportIntf.RenderDocument(lFrxReport, False, False);
                      aPdfStream.Position := 0;
                      aPdfStream.SaveToFile(lTempFileName);
                    end else
                    begin
                      lgtFRExportIntf.RenderDocument(lFrxReport, False, False);
                    end;
                  {$ELSE}
                    if aShareName <> '' then
                    begin
                      lFrxReport.Export(lFrxPDFExport);
                      aPdfStream.Position := 0;
                      aPdfStream.SaveToFile(lTempFileName);
                    end else
                    begin
                      lFrxReport.Export(lFrxPDFExport)
                    end;
                  {$ENDIF}
                end;
              {$ENDIF}
              // WE'VE GOT THIS FAR, SO WE MUST ASSUME ALL IS WELL
              Result := True;
            end
            else
            begin
              // PREPARE REPORT FAILED, SO WE MUST ASSUME THE REPORT RENDERING FAILED
              Result := False;
              raise Exception.Create(lFrxReport.Errors.Text);
            end;

          except
            Result := False;
            raise;
          end;

        finally
          {$IFDEF  USE_GNOSTICE}
            FreeAndNil(lgtFRExportIntf);
            FreeAndNil(lgtPDFEngine);
          {$ELSE}
            FreeAndNil(lFrxPDFExport);
          {$ENDIF}
        end;
      end // Export PDF
      {$ENDREGION}
      else
      begin
        if lFrxReport.PrepareReport then
          lFrxReport.ShowPreparedReport
        else
          raise Exception.Create(lFrxReport.Errors.Text);
      end;
      // WE SET lAvailDS = NIL EARLY BECAUSE FOR SOME UNKNOWN REASON IT WAS APPEARING AS
      // BEING ASSIGNED WHEN IT SHOULD NOT BE
      if Assigned(lAvailDS) then
      begin
        lAvailDS.fxResetDataSetBookMark(lFrxReport);
        FreeAndNil(lAvailDS);
      end;

    finally
      // DO NOT FREE lUAAdhoSQL (RO HANDLES THIS INTERNALLY)
      {$IFDEF IS_IW}
        FreeAndNil(lUAAdhocSQL);;
      {$ENDIF}
      lFrxReport.Free;
      lRptVars.Free;
    end;
  except
    on E: Exception do
      raise Exception.Create('ERROR: Failed to Prepare Report. ' + E.Message);
  end;

end;
Reply


Messages In This Thread
Intraweb and fast report - by softdev85 - 12-16-2020, 06:21 PM
RE: Intraweb and fast report - by softdev85 - 12-16-2020, 09:04 PM
RE: Intraweb and fast report - by zsleo - 12-16-2020, 11:02 PM
RE: Intraweb and fast report - by joel - 12-18-2020, 05:43 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)