WebGadgetPrint()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

WebGadgetPrint()

Post 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)
Ta - N
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post 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...
Ta - N
okasvi
Enthusiast
Enthusiast
Posts: 150
Joined: Wed Apr 27, 2005 9:41 pm
Location: Finland

Post 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...)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

@Fang

You have examples of the WebGadget Print or the Copy?

Either would be useful
Ta - N
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

The webGadget Print

Post 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

Last edited by Fangbeast on Tue Dec 20, 2005 2:29 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post 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. 
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post 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
Ta - N
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post 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.
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post 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.
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post 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!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply