Page 1 of 1

WebGadgetPrint()

Posted: Mon Dec 19, 2005 3:24 am
by naw
This would be soooo useful.

I use the WebGadget to create nicely formatted reports for printing, I embed some JavaScript to force a print of the WebGadget (which is easy and works fine) but it would be nice to be able to send a message to the WebGadget to force it to print.

The syntax should (ideally) be:

SetGadgetState(#GID, #PB_Web_Print)

Posted: Mon Dec 19, 2005 3:33 am
by naw
oh! And while I'm thinking about it, this would be really useful too:

SetGadgetState(#GID, #OB_Web_Copy)

To copy the contents of the specified Web Gadget to the clipboard...

Posted: Mon Dec 19, 2005 4:28 am
by okasvi
naw wrote:oh! And while I'm thinking about it, this would be really useful too:

SetGadgetState(#GID, #OB_Web_Copy)

To copy the contents of the specified Web Gadget to the clipboard...

i think that, that one isnt going to be easy as you might think that since the control over the things inside the gadget is really on atl.dll...

(im just guessing here...)

Posted: Mon Dec 19, 2005 10:03 am
by Fangbeast
There are actually examples of this i the forum and on PureArea and both Karbon and myself are using them.

Don't have time tonight to type it up but if no-one answers you, I'll upload a snippet tomorrow.

Posted: Mon Dec 19, 2005 4:22 pm
by naw
@Fang

You have examples of the WebGadget Print or the Copy?

Either would be useful

The webGadget Print

Posted: Tue Dec 20, 2005 12:40 am
by Fangbeast
See if the below makes sense to you. I found it in the form ages ago and don't remember who any more (I suspect this was RIngs's code though) and I massaged it to suit me.

Code: Select all

;==============================================================================================================================
; Print related constants found by Rings
;==============================================================================================================================

#READYSTATE_UNINITIALIZED     = 0       ; Print routine with WebGadget
#READYSTATE_LOADING           = 1
#READYSTATE_LOADED            = 2
#READYSTATE_INTERACTIVE       = 3
#READYSTATE_COMPLETE          = 4
#OLECMDID_PRINT               = 6
#OLECMDID_PRINTPREVIEW        = 7
#OLECMDEXECOPT_DONTPROMPTUSER = 2

Global WebObject.IWebbrowser2                           ; For the web page printing

;==============================================================================================================================
; Common controls structure size
;==============================================================================================================================

dt.INITCOMMONCONTROLSEX\dwSize  = SizeOf(INITCOMMONCONTROLSEX) 
dt\dwICC                        = $100 

InitCommonControlsEx_(@dt) 

; First open your window

; If OpenWindow(........etc
;
; Now change the webgadgetclass to allow printing from it
; 
WebObject = GetWindowLong_(GadgetID(#WhateverMyWebGadgetIsCalled), #GWL_USERDATA)
;
; Now i've put the routines into 3 little gosubs to make it easier for me
;
;============================================================================================================================
CheckReady:                       ; Check that the web page is ready for printing (Finished loading etc)
;============================================================================================================================

  printIt = #True
  If printit = #True                          ; Check if the printing has been forced to auto
    WebObject\get_ReadyState(@ready.l)        ; What is the document's ready state
                                              ; From what I've seen in the past, a web page may not reach
                                              ; #READYSTATE_COMPLETE = 4 if there are missing objects, such
                                              ; As images. Once the web page reaches #READYSTATE_INTERACTIVE = 3
                                              ; It seems as though print works fine. You can change the following line to
                                              ; If ready = 4 And printit = #True
                                              ; And see which works best in your situation.
    If ready > 2 And printit = #True         ; Once we have interactive page readiness we print
      If printit                             ; Does the print flag have a positive value
        Go.l = 1                             ; If printing is ready, tell the print and preview routines to go ahead
      Else 
        Go.l = 0
      EndIf
    EndIf                                   ; Otherwise end test condition
  EndIf                                     ; Otherwise end test condition
  
Return

;============================================================================================================================
ForcePrint:                       ; Print the webgadget without a prompt from the user
;============================================================================================================================

  Gosub CheckReady                          ; Check that the web page is ready for printing (Finished loading etc)
  If Go.l = 1
    printweb(0)                              ; Print the page with prompt = 0 to print and 1 to preview
    printit = #False                         ; Turn print off until we set to #True for next print job
    Go.l = 0
  EndIf
Return

;============================================================================================================================
ForcePreview:                     ; Force a preview mode of the web gadget
;============================================================================================================================

  Gosub CheckReady

  If Go.l = 1
    printweb(1)                              ; Print the page with prompt = 0 to print and 1 to preview
    printit = #False                         ; Turn print off until we set to #True for next print job
    Go.l = 0
  EndIf

Return


Posted: Tue Dec 20, 2005 1:26 am
by TerryHough
I have been using the following successfully for some time.

Found it somewhere here on the forum way back, and played with it for
my use. Can't remember who to credit with the idea.

Maybe a forum search on WebObject.IWebBrowser2 will turn it up.

Code: Select all

        WebObject.IWebBrowser2 = GetWindowLong_(GadgetID(#WebGID), #GWL_USERDATA)
        WebObject\ExecWB(#OLECMDID_PRINT, #OLECMDEXECOPT_PROMPTUSER, 0, 0)
; or to preview
        ; WebObject\ExecWB(#OLECMDID_PRINTPREVIEW, #OLECMDEXECOPT_PROMPTUSER, 0, 0) 
        ; or use #OLECMDEXECOPT_DONTPROMPTUSER as the second argument prints without the printrequester. 

Posted: Tue Jan 10, 2006 5:31 pm
by naw
@All
- Thanks very much for the examples... I'll use TerryHough's code though,
it's _very_ neat and it does _everything_ I need.

I implemented it as:

Code: Select all

  #PROMPTUSER  = 1
  #DONTPROMPTUSER = 2 
  #PRINT = 6
  #PREVIEW = 7 

Procedure PrintWebGadget (_GID, _PREVIEW)
  WebObject.IWebBrowser2 = GetWindowLong_(GadgetID(_GID), #GWL_USERDATA)
  WebObject\ExecWB(_PREVIEW, #PROMPTUSER, 0, 0)
EndProcedure

  PrintWebGadget(#GID, #PREVIEW)
- Brill - thanks very much

Posted: Fri Feb 24, 2006 3:22 pm
by Karbon
It works, but be aware I'm using this method to print HTML invoices in kBilling and it does foul up quite a bit when printing more than one document in a row.

Posted: Fri Feb 24, 2006 3:37 pm
by gnozal
I use this code to print many HTML files in a row without problems (PB 3.94).

Code: Select all

; Allows to change the header and footer (you must restore the originals)
Procedure ModifyIEHeaderFooter(State.l) ; #TRUE : modify , #FALSE : reverse to original
  Static FirstTime.l, IEHeader.s, IEFooter.s, IELeft.s, IERight.s, IEBottom.s
  If FirstTime = 0
    IEHeader = GetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "header") ; default : "&w&bPage &p de &P"
    IEFooter = GetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "footer") ; default : "&u&b&d"
    IELeft = GetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_left") ; default : "0.750000"
    IERight = GetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_right") ; default : "0.750000"
    IEBottom = GetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_bottom") ; default : "0.750000"
    FirstTime = 1
  EndIf
  If State = #True
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "header", "AQ-Viewer [Consultation de documents]&b&d")
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "footer", "&bPage &p/&P")
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_left", "0.400000")
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_right", "0.400000")
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_bottom", "0.500000")
  Else
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "header", IEHeader)
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "footer", IEFooter)
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_left", IELeft)
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_right", IERight)
    SetKeyString(#HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\PageSetup", "margin_bottom", IEBottom)
  EndIf
EndProcedure
; Set URL
Procedure WebAfficheURL(Url.s)
  WebObject.IWebBrowser2 = GetWindowLong_(GadgetID(#Web_IE), #GWL_USERDATA)
  SetGadgetText(#Web_IE, Url)
  While WindowEvent() : Wend
EndProcedure
;
; PRINTING HTML FILES (WebGadget #Web_IE may be hidden)
;
  WebObject.IWebBrowser2 = GetWindowLong_(GadgetID(#Web_IE), #GWL_USERDATA)
  ; Print many HTML files (filenames in FichiersHTML())
  ForEach FichiersHTML()
    WebAfficheURL(FichiersHTML())
    ;
    ModifyIEHeaderFooter(#True)
    WebObject\ExecWB(#OLECMDID_PRINT, #OLECMDEXECOPT_DONTPROMPTUSER, 0, 0)
    ModifyIEHeaderFooter(#False)
  Next

Posted: Fri Feb 24, 2006 3:39 pm
by Karbon
Hehe, yep, I think I wrote that, didn't I? :-)

It's almost exactly what I'm using regardless. I've had many reports of document printing out of order, not printing, and printing incorrectly.

Posted: Fri Feb 24, 2006 3:43 pm
by gnozal
Karbon wrote:Hehe, yep, I think I wrote that, didn't I?
:shock: [EDIT] After a search, I see you have some posts about it, yes. But it seems like it's mostly Freak's code.

In my experience, adding the While WindowEvent() : Wend loop after SetGadgetText(#Web_IE, Url) changes everything. It works better than the WebObject\get_ReadyState() stuff.

Posted: Fri Feb 24, 2006 3:56 pm
by Karbon
I do :

Code: Select all

Repeat
    WebObject\get_Busy(@IsBusy.l)
    Delay(1)
    While WindowEvent(): Wend
  Until IsBusy = 0
but it's been so long since I tested it I can't recall why I chose to do it that way!