Ignore:
Timestamp:
Jan 19, 2010, 1:26:10 PM (14 years ago)
Author:
Kevin Toppenberg
Message:

Fixed HTML Note Printing Error

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cprs/branches/tmg-cprs/CPRS-Chart/rHTMLTools.pas

    r541 r671  
    1616    DesiredHTMLFontSize : byte;       
    1717     
    18   procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string; PtName:string;
    19                             DOB:string; Location:string; Application : TApplication);  //kt added 5-2-05
     18  procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string;
     19                            PtName, DOB, VisitDate, Location:string; Application : TApplication);  //kt added 5-2-05
    2020  function  IsHTML(Lines : TStrings): boolean; overload;
    2121  function  IsHTML(Line : String): boolean; overload;
     
    3434  procedure SetRegHTMLFontSize(Size: byte);
    3535  procedure RestoreRegHTMLFontSize;
    36   procedure SetupHTMLPrinting(Name,DOB,Location,Institution : string);
     36  procedure SetupHTMLPrinting(Name,DOB,VisitDate,Location,Institution : string);
    3737  procedure RestoreIEPrinting;
    38    
     38  function ExtractDateOfNote(Lines : TStringList) : string;
     39
    3940implementation
    4041
     
    4445       Graphics, //For color constants
    4546       fTMGPrintingAnimation,
     47       ExtCtrls,
    4648       StrUtils;
     49
     50  type
     51    TPrinterEvents = class
     52    public
     53      SavedDefaultPrinter : string;
     54      LastChosenPrinterName : string;
     55      RestorePrinterTimer : TTimer;       
     56      PrintingNow : boolean;
     57      procedure HandleRestorePrinting (Sender: TObject);
     58      Constructor Create;
     59      Destructor Destroy; override;
     60    end; 
     61   
     62  var
     63    PrinterEvents : TPrinterEvents;
    4764
    4865  const CRLF = #$0D#$0A;
     
    100117    until (GetTime-StartTime) > (OneSec*Sec);
    101118  end;
    102  
     119
     120 
     121  procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string;
     122                            PtName, DOB, VisitDate, Location:string;
     123                            Application : TApplication);
     124  //      Web browser printing options:
     125  //        OLECMDEXECOPT_DODEFAULT       Use the default behavior, whether prompting the user for input or not.
     126  //        OLECMDEXECOPT_PROMPTUSER      Execute the command after obtaining user input.
     127  //        OLECMDEXECOPT_DONTPROMPTUSER  Execute the command without prompting the user.
     128
     129  {Notice:  When IE is asked to print, it immediately returns from the function,
     130           but the printing has not yet occured.  If UI is requested, then the
     131          printing will not start until after the user selects a printer and
     132         presses [OK].  I could not find any reliable way to determine when the
     133        print job had been created.  I had to know this event because I need to
     134       restore some IE settings AFTER the printing has finished.  I even tried to
     135      get the active windows and see if it was a print dialog.  But IE print dlg
     136     apparently is owned by another thread than CPRS, because GetActiveWindow would
     137     not bring back a handle to the printer dialog window.  I therefore told IE
     138     to print WITHOUT asking which printer via UI.  In that case it prints to the
     139     system wide default printer.  So I have to set the default printer to the
     140     user's choice, and then change it back again.  This is bit of a kludge,
     141     but I couldn't figure out any other way after hours of trial and error.
     142     NOTE: I tried to query IE to see if it was able to print, thinking that it
     143     would return NO if in the process of currently printing.  It didn't work,
     144     and would return OK immediately.     
     145     
     146     ADDENDUM:  I was getting errors and inconsistent behavior with this, so I
     147       have decided to try to let the user click a button when the printer has
     148       been selected.                                         }   
     149 
     150  var
     151    UseUI          : OleVariant;   
     152    //NewPrinterName : string;   
     153    //dlgWinPrinter  : TPrintDialog;
     154  begin
     155    //if PrinterEvents.RestorePrinterTimer.Enabled = false then begin
     156    //  PrinterEvents.SavedDefaultPrinter := GetCurrentPrinterName;
     157    //end; 
     158    if PrinterEvents.PrintingNow then exit; // prevent double printing (it has happened)
     159
     160    try
     161      frmNotes.SetDisplayToHTMLvsText([vmView,vmHTML],Lines);  //ActivateHtmlViewer(Lines);
     162      if frmNotes.HtmlViewer.WaitForDocComplete = false then begin
     163        ErrMsg := 'The web browser timed out trying to set up document.';
     164        exit;
     165      end;
     166      PrinterEvents.PrintingNow := true;
     167      SetupHTMLPrinting(PtName,DOB,VisitDate,Location,' ');  {elh 6/19/09} //kt
     168      frmNotes.HtmlViewer.PrintFinished := false;               
     169      UseUI := true;
     170      frmNotes.HtmlViewer.PrintDocument(UseUI);   //Returns immediately, not after printing done.
     171      frmTMGPrinting.ShowModal;    // Let user show when print job has been launched.
     172      PrinterEvents.RestorePrinterTimer.Enabled := true; //launch a restore event in 30 seconds
     173      //RestoreIEPrinting;  //elh - This was omitted from below. Not sure why.  11/10/09
     174    finally 
     175      PrinterEvents.PrintingNow := false;
     176    end;
     177  end;
     178
     179(*
     180  Safe copy of above.  Delete later...
     181   
    103182  procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string;
    104183                            PtName, DOB, Location:string;
     
    126205     and would return OK immediately.                                               }   
    127206 
     207
     208
    128209  var
    129     UseUI : OleVariant;   
    130     NewPrinterName,DefaultPrinter: string;   
    131     dlgWinPrinter: TPrintDialog;
    132   begin
    133     DefaultPrinter := GetCurrentPrinterName;
     210    UseUI          : OleVariant;   
     211    NewPrinterName : string;   
     212    dlgWinPrinter  : TPrintDialog;
     213  begin
     214    if PrinterEvents.RestorePrinterTimer.Enabled = false then begin
     215      PrinterEvents.SavedDefaultPrinter := GetCurrentPrinterName;
     216    end; 
    134217    dlgWinPrinter := TPrintDialog.Create(nil);
    135218    frmTMGPrinting.Show;
     219    //FIX: get printer name for the one used last time somehow...
    136220    if dlgWinPrinter.Execute then begin  //only sets a local printer
    137221      NewPrinterName := GetCurrentPrinterName; 
    138222      SetDefaultPrinter(NewPrinterName); //Set global setting that IE will use.
     223      PrinterEvents.LastChosenPrinterName := NewPrinterName;
    139224      try
    140         //frmNotes.SetHTMLorTextViewer(True,Lines);  //ActivateHtmlViewer(Lines);
    141225        frmNotes.SetDisplayToHTMLvsText([vmView,vmHTML],Lines);  //ActivateHtmlViewer(Lines);
    142226        if frmNotes.HtmlViewer.WaitForDocComplete = false then begin
     
    148232        UseUI := false;  //UseUI := true;
    149233        frmNotes.HtmlViewer.PrintDocument(UseUI);   //Returns immediately, not after printing done.
    150         Wait(4,Application); //give IE x sec to complete print document.  Is this always enough?
     234        PrinterEvents.RestorePrinterTimer.Enabled := true; //launch a restore event in 30 seconds
     235        Wait(4,Application);
    151236        //WaitForBrowserOK(10, Application); //wait up to 10 seconds  //Note: this doesn't do what I want.  Status is immediately OK.
    152         RestoreIEPrinting;   {elh 6/19/09}  //kt
     237        //RestoreIEPrinting;   {elh 6/19/09}  //kt
    153238      finally   //any needed final code goes here.
    154         SetDefaultPrinter(DefaultPrinter);
     239        //SetDefaultPrinter(DefaultPrinter);
    155240        //beep;
    156241      end;
     
    159244    frmTMGPrinting.Hide;       
    160245  end;
    161 
     246*) 
    162247  (*
    163248  function WaitForBrowserOK(MaxSecDelay: integer; Application : TApplication) : boolean;
     
    738823    Reg : TRegistry;
    739824   
    740   procedure SetupHTMLPrinting(Name,DOB,Location,Institution : string);
     825  procedure SetupHTMLPrinting(Name,DOB,VisitDate,Location,Institution : string);
    741826  //Purpose: To open the IE header and footer registry keys, save the
    742827  //current value and then replace with passed patient data.   elh 6/19/09
     
    748833    end; 
    749834    NewHeader := Location + ' &b ' + Institution + ' &b Printed: &d &t';
    750     NewFooter := Name + ' &b DOB: ' + DOB + ' &b &p of &P';
     835    NewFooter := Name + ' (' + DOB + ') &b Note: ' + VisitDate + ' &b &p of &P';
    751836    Reg := TRegistry.Create;  //to be freed in RestoreIEPrinting
    752837    try
     
    764849  procedure RestoreIEPrinting;
    765850  //Purpose: To restore the IE header and footer registry with the initial value
     851  //NOTE: The below function was used to restore the previous value to the registry
     852  //       but got commented above, so the registry retained the patient's data
     853  //       to resolve this, we are now setting this to a default value.
    766854  //elh 6/19/09
    767855  begin
    768856    if not assigned(Reg) then Reg := TRegistry.Create;
    769857    try
     858      StoredIEFooters := '&u&b&d';          //Comment this line to restore previous value
     859      StoredIEHeader := '&d&b&t&bPage &p of &P';  //Comment this line to restore previous value
    770860      Reg.WriteString('footer',StoredIEFooters);
    771861      Reg.WriteString('header',StoredIEHeader);
     
    776866  end;
    777867
    778 begin
     868  function ExtractDateOfNote(Lines : TStringList) : string;
     869  //Scan note and return date found after 'DATE OF NOTE:', if present.
     870  var i,p : integer;
     871      s : string;
     872  begin
     873    Result := '';
     874    if Lines = nil then exit;
     875    for i := 0 to Lines.Count-1 do begin
     876      p := Pos('DATE OF NOTE:',Lines.Strings[i]);
     877      if p<1 then continue;
     878      s := Piece(Lines.Strings[i],'DATE OF NOTE:',2);
     879      s := Piece(s,'@',1);
     880      Result := Trim(s);     
     881    end;
     882  end;
     883
     884  //===============================================================
     885
     886  Constructor TPrinterEvents.Create;
     887  begin
     888    RestorePrinterTimer := TTimer.Create(frmNotes);
     889    RestorePrinterTimer.Enabled := false;
     890    RestorePrinterTimer.Interval := 30000; //30 seconds to complete print job.
     891    RestorePrinterTimer.OnTimer := HandleRestorePrinting;
     892    PrintingNow := false;
     893  end;
     894 
     895  Destructor TPrinterEvents.Destroy;
     896  begin
     897    RestorePrinterTimer.Free;   
     898    inherited Destroy;
     899  end;
     900
     901
     902  procedure TPrinterEvents.HandleRestorePrinting (Sender: TObject);
     903  begin
     904    if PrinterEvents.PrintingNow then begin
     905      RestorePrinterTimer.Enabled := true; // reset timer for later.
     906      exit;
     907    end;
     908    RestorePrinterTimer.Enabled := false;
     909    RestoreIEPrinting;   {elh 6/19/09}  //kt
     910    //kt SetDefaultPrinter(SavedDefaultPrinter);   
     911    //beep;   
     912  end; 
     913
     914  //===============================================================
     915 
     916
     917initialization
    779918  DesiredHTMLFontSize := 2; //probably overwritten in fNotes initialization
     919  PrinterEvents := TPrinterEvents.Create; 
     920
     921finalization
     922  PrinterEvents.Free;   
    780923end.
Note: See TracChangeset for help on using the changeset viewer.