Page 1 of 1

Print html/txt files using Javascript from a webgadget

Posted: Mon Nov 27, 2006 11:35 pm
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

Posted: Mon Nov 27, 2006 11:41 pm
by rsts
pretty nice for a quck and easy way to print something.

thanks

Posted: Tue Nov 28, 2006 2:10 am
by JCV
Doesnt work on IE7 here. It says "This program cannot display the webpage "
How to make it work?

Posted: Tue Nov 28, 2006 11:18 am
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.

Posted: Tue Nov 28, 2006 1:17 pm
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.

Posted: Fri Dec 01, 2006 1:09 pm
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..

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

Posted: Wed Oct 17, 2012 11:48 am
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?

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

Posted: Wed Oct 17, 2012 12:01 pm
by Kiffi
@SeregaZ:
SetGadgetItemText(#Gadget, Item, Text$ [, Column])

Change the html code in the gadget with #PB_Web_HtmlCode as 'Item'.
Greetings ... Kiffi

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

Posted: Wed Oct 17, 2012 12:10 pm
by SeregaZ
thanks :)

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

Posted: Wed Oct 17, 2012 1:02 pm
by Kukulkan
Does not work on Mac. Possible reason: http://www.purebasic.fr/english/viewtop ... 19&t=51564

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

Posted: Wed Oct 17, 2012 1:17 pm
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