Print html/txt files using Javascript from a webgadget

Share your advanced PureBasic knowledge/code with the community.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Print html/txt files using Javascript from a webgadget

Post by utopiomania »

Here's a way to print files, uses html/javascript to print a document, in this case 'c:\test.txt'.

It opens a printer dialog and prints the file, which could also be a nicely formatted 'c:\test.html'
as well.

It prints headers and footers though..

(Right click int the webgadget to see the html source that does the actual work)

Code: Select all

;-program notes
;-PB4.00, 20061127, utopiomania

;-initialize
;-
enumeration
  #win1
  #web1
endEnumeration

global path.s

declare openMainWindow()
declare.s createPage(path.s)

;-program entry
openMainWindow()

;-program event handler
repeat
  event = waitWindowEvent()
  select event 
    case #PB_Event_Gadget
      select eventGadget()
      ;
      endSelect
    case #PB_Event_CloseWindow
      exit = #True
  endSelect
until exit = #True

;-program exit
;-
deleteFile(path)
end

procedure openMainWindow()
  flags = #PB_Window_ScreenCentered | #PB_Window_Systemmenu | #PB_Window_MaximizeGadget
  if openWindow(#win1, 0, 0, 800, 600, "Javascript print file", flags)
    ;Create a new gadget list for the current window
    if createGadgetList(windowID(#win1))
      ;Controls
      path = getPathPart(programFileName()) + "page.html"
      createPage(path)
      webGadget(#web1, 0, 0, 800, 600, "file://" + path)
    endIf
  endIf
endProcedure

procedure.s createPage(path.s)
  define s.s, dq.s = chr(34), crlf.s = chr(13) + chr(10)

  ;create the html string for the page:
  s = s + "<html>" + crlf
  s = s + "<head>" + crlf
  s = s + "<meta http-equiv='MSThemeCompatible' content='yes'> " + crlf
  s = s + "" + crlf
  s = s + "<script type = 'text/javascript'>" + crlf
  s = s + "function printDocument()" + crlf
  s = s + "{" + crlf
  s = s + "	frames['preview'].focus();" + crlf
  s = s + "	frames['preview'].print();" + crlf
  s = s + "}" + crlf
  s = s + "</script>" + crlf
  s = s + "</head>" + crlf
  s = s + "" + crlf
  s = s + "<body scroll = 'no'>" + crlf
  s = s + "<center>" + crlf
  s = s + "<h2>Document Preview</h2>" + crlf
  s = s + "<iframe src = 'c:\test.txt' name = 'preview' width = '750' height = '480' " + crlf
  s = s + "	style = 'border:2px solid #efefef;'>Document preview</iframe>" + crlf
  s = s + "" + crlf
  s = s + "<p><button type = 'button' onclick = 'printDocument();'>Print This Document</button></p>" + crlf
  s = s + "</body>" + crlf
  s = s + "</html>" + crlf

  if len(path)
    ;write the string to a file
    createFile(0, path)
    writeString(0, s)
    closeFile(0)
  else
    ;return the string
    procedureReturn "about:" + s
  endIf
endProcedure
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

pretty nice for a quck and easy way to print something.

thanks
JCV
Enthusiast
Enthusiast
Posts: 580
Joined: Fri Jun 30, 2006 4:30 pm
Location: Philippines

Post by JCV »

Doesnt work on IE7 here. It says "This program cannot display the webpage "
How to make it work?
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

JCV wrote:Doesnt work on IE7 here. It says "This program cannot display the webpage "
How to make it work?
That's because of the added security of IE7, you have to give each opened file that contains scripts permission to be launched locally.
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

This is the shortest way to print a website:

Insert this code somewhere in the HTML-Code:

Code: Select all

<script language="JavaScript">
document.print();
</script>
The website containing this code, will automatically open a print dialogue.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

About the problems with IE7, it's a shame the way Microsoft manages to stick their nose into absolutely everything.

In the end I guess we all have to obtain some sort of approval from them to be able to run our programs on Windows at all.. Grrrrr..
SeregaZ
Enthusiast
Enthusiast
Posts: 628
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Print html/txt files using Javascript from a webgadget

Post by SeregaZ »

how i can make page without creation temp file on hard disk drive? now i have limit to 528 symbols.

Code: Select all

If OpenWindow(0,10,10,800,600,"Test",#PB_Window_SystemMenu)

  crlf.s = Chr(13) + Chr(10)
                      
  url$ = "about:<head>" + crlf
  url$ = url$ + "<META http-equiv=Content-Type content='text/html; charset=windows-1251'>" + crlf
  url$ = url$ + "" + crlf
  url$ = url$ + "<body>" + crlf
  url$ = url$ + "<table width=680 height=1040 align=center style='border-collapse:collapse; solid windowtext;'>" + crlf
  url$ = url$ + "<tr style='border-collapse:collapse'>" + crlf
  url$ = url$ + "<td align=center valign=top>" + crlf                      
  url$ = url$ + "<table width=90% border=0 align=center style='font-size: 22px;'><tr><td width=100% align=center>" + crlf
  url$ = url$ + "<br><br><br><br><br><br><br><br><br><br><b>T E X T</b><br><br></td></tr></table><br><br>" + crlf
  url$ = url$ + "123456789012345678901234567890123456789012345678901234567890123456789012345" + crlf
  
  Debug "limit is "+Str(Len(url$))
  
  ;url$ = url$ + "<table width=90% border=0 align=center style='font-size: 18px;'><tr><td>bla</td></tr></table>" + crlf
                      
  WebGadget(0,10,10,780,580,url$)
  Repeat
    ev=WaitWindowEvent()
  Until ev=#PB_Event_CloseWindow
EndIf
uncomment last string and web page did't show any information.

Chr(13) + Chr(10) not make next string in list. probably this is problem. how to make this?
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Print html/txt files using Javascript from a webgadget

Post by Kiffi »

@SeregaZ:
SetGadgetItemText(#Gadget, Item, Text$ [, Column])

Change the html code in the gadget with #PB_Web_HtmlCode as 'Item'.
Greetings ... Kiffi
Hygge
SeregaZ
Enthusiast
Enthusiast
Posts: 628
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Print html/txt files using Javascript from a webgadget

Post by SeregaZ »

thanks :)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Print html/txt files using Javascript from a webgadget

Post by Kukulkan »

Does not work on Mac. Possible reason: http://www.purebasic.fr/english/viewtop ... 19&t=51564
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Print html/txt files using Javascript from a webgadget

Post by Kiffi »

Kukulkan wrote:Does not work on Mac.
yes. see the WebGadget()-Documentation:
- SetGadgetItemText(): With #PB_Web_HtmlCode as 'Item' html code can be streamed into the Gadget. (Windows only)
Greetings ... Kiffi
Hygge
Post Reply