Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
Anim-FX Flash intros & Flash banners builder
Vector Shape - Rounded Rectangle
CD-ROM Drive Letter Finder-Using a File
DM XTreme Transitions
Light Up
Icon Shop
SetMouseXtra
Home, End, Page Up, Page Down with Text Members
DmySQL - mySQL backend for Director 'baby' version
DirectCommunication
 

 

 

FAQ Print Formatted and HTML Text Using a Browser

Added on 3/26/2004

 

Compatibilities:
D7 D8 D8_5 D9 Mac Script

Required Xtras:
Buddy API

This item has not yet been rated

Author: itsolutionz

How can I print formatted text without PrintOMatic?

Whenever the discussion moves towards printing cast members with text in html format, PrintOMatic gets suggested as the print engine. However, I avoid this method and easily create formatted html files which print silently, using BuddiApi.

Perhaps this is old news to a majority of lingoists, but the process is effective if you haven't seen it yet.

In my commercial applications, I need to create and print spreadsheet-like reports. While there are many ways to set this up, I typically display and print columnar text from a number of side-by-side fields, scrolled as a group with scripted bitmap scroll bars.

I generally use netLingo to query a remote mysql database, return the data as vertical bar delimited text--using a simple php script to pull the data from the mysql server--and place each database field into the on-screen field members.

With FileIO and BuddiApi methods, I then read the field rows in a repeat loop and generate a temporary file, which is then printed by the computer-resident browser (almost any browser), without even displaying the browser on screen, rather then with PrintOMatic.

The file is created on the fly to reflect the query results, is overwritten silently for subsequent print files, and is automatically deleted when the movie quits.

In the following example script, I use eight fields as columns to display the quasi-spreadsheet on screen.

However, a single field/text member could as easily used to hold the results—or to format any rich text for printing—and the script could then be revised to loop on the "item" rather than field. If you simply want to print the formatted text of one or more text members, the script can be rewritten to just write the html of the member to the print file.

This routine runs very fast, and can produce a 50 page report in mini-seconds. However, BuddiApi is required to Open, Print, and Delete the html file, and FileIO is used to write the temporary print file.

on mouseUp
  -- if you need to save a permanent file, hold down the ctrl key
  if the commandDown then
    tCd=1
  end if
  --create a temporary or permanent print file
  set tExportFile = new(xtra "FileIO")
  if tCd=1 then
    tFilePath = displaySave(tExportFile,"Save data to...","Report.html")
  else
    tFilePath=the moviePath&"Prnttemp.html"
  end if
  if tFilePath = "" then
    set tExportFile = 0
    exit
  else
    -- standard FileIO stuff...
    createFile(tExportFile,tFilePath)
    set tErr = status(tExportFile)
    if tErr = -122 then
      openfile(tExportFile,tFilePath,2)
      delete(tExportFile)
      createFile(tExportFile,tFilePath)
    end if
    openfile(tExportFile,tFilePath,2)
    
    -- prepare report data
    tNumRows=the number of lines of field "RptFld1" -- the first "spreadsheet" field
    
    -- store all the heading stuff first
    put "<" & "!DOCTYPE HTML PUBLIC"&RETURN into tConvertToTable
    put QUOTE&"-//W3C//DTD HTML 4.01 Transitional//EN""E&RETURN after tConvertToTable
    put QUOTE&"http://www.w3.org/TR/html4/loose.dtd""E&">"&Return after tConvertToTable
    put "<" & "html>"&Return after tConvertToTable
    put "<" & "head>"&Return after tConvertToTable
    put "<" & "title>Converted data<" & "/title>"&Return after tConvertToTable
    
    -- create an internal cascading style sheet for the html elements
    put "<" & "style type=""E&"text/css""E&">"&Return after tConvertToTable
    put "p {font-family: ""E&"Arial""E&"; font-size: 8pt; line-height: 115%}"&Return after tConvertToTable
    put "td {font-family: ""E&"Arial""E&"; font-size: 8pt; line-height: 115%; border-bottom:1pt solid #ECECEC}"&Return after tConvertToTable
    --if you have IE6 or a browser that recognizes dotted borders, use this instead...
    --put "td {font-family: ""E&"Arial""E&"; font-size: 8pt; line-height: 115%; border-bottom:1pt dotted black}"&Return after tConvertToTable
    
    --the next style includes a "page break", and is placed at the bottom of each page
    put "h1 {font-family: ""E&"Arial""E&"; font-size: 8pt; font-weight: normal; line-height: 115%; page-break-after: always}"&Return after tConvertToTable
    put "<" & "/style>"&Return after tConvertToTable
    put "<" & "/head>"&Return after tConvertToTable
    put "<" & "body bgcolor=""E&"#ffffff""E&">"&Return after tConvertToTable
    
    -- set up bold headers to repeat on each page
    tHeaders=EMPTY
    put "<" & "tr>"&RETURN after tHeaders
    tHoldHeader=EMPTY
    repeat with x=1 to 8
      tLabelName=line 1 of field ("RptFld"&x)
      put "<" & "td width=104pt align=left><" & "b>"&(tLabelName)&"<" & "/b><" & "/td>" after tHeaders
      delete line 1 of field ("RptFld"&x)
    end repeat
    put RETURN after tHeaders
    put "<" & "/tr>"&RETURN after tHeaders
    
    --set up variables for the number of rows per page and number of pages to print
    tHowManyPages=tNumRows/52
    tLastTime=tNumRows mod 52
    
    -- write row data to holding variable
    if tHowManyPages=0 then --less than 52 rows, or 1 page
      -- add the report title, copied from a user field
      put "<" & "p><" & "b>"&(member("rpttitle").text)&"<" & "/b><" & "/p>" after tConvertToTable
      put "<" & "table>"&RETURN after tConvertToTable
      put tHeaders after tConvertToTable
      repeat with x=2 to tNumRows
        tRowHtml=EMPTY
        put "<" & "tr>"&RETURN into tRowHtml
        repeat with z=1 to 8
          tColName="RptFld"&z
          tHoldText=line x of field tColName
          if tHoldText=EMPTY then
            --write non-breaking space for empty cell
            tHoldText=" "
          end if
          put "<" & "td width=104pt align=left>"&tHoldText&"<" & "/td>" after tRowHtml
        end repeat
        put RETURN after tRowHtml
        put "<" & "/tr>"&RETURN after tRowHtml
        put tRowHtml after tConvertToTable
      end repeat
    else -- if more than 1 page, do this instead
      tStart=1
      tEnd=52
      repeat with t=1 to tHowManyPages
        put "<" & "p><" & "b>"&(member("rpttitle").text)&":  Page "&t&"<" & "/b><" & "/p>" after tConvertToTable
        put "<" & "table>"&RETURN after tConvertToTable
        put tHeaders after tConvertToTable
        repeat with x=tStart to tEnd
          tRowHtml=EMPTY
          put "<" & "tr>"&RETURN into tRowHtml
          repeat with z=1 to 8
            tColName="RptFld"&z
            tHoldText=line x of field tColName
            if tHoldText=EMPTY then
              tHoldText=" "
            end if
            put "<" & "td width=104pt align=left>"&tHoldText&"<" & "/td>" after tRowHtml
          end repeat
          put RETURN after tRowHtml
          put "<" & "/tr>"&RETURN after tRowHtml
          put tRowHtml after tConvertToTable
        end repeat
        tStart=tStart+52
        tEnd=tEnd+52
        put "<" & "/table>"&RETURN after tConvertToTable
        -- add an element containing a page break--see the style sheet above
        put "<" & "h1> <" & "/h1>"&RETURN after tConvertToTable
      end repeat
      -- if a part page is left over
      if tLastTime>0 then
        put "<" & "p><" & "b>"&(member("rpttitle").text)&":  Page "&t&"<" & "/b><" & "/p>" after tConvertToTable
        put "<" & "table>"&RETURN after tConvertToTable
        put tHeaders after tConvertToTable
        repeat with x=tStart to tEnd
          tRowHtml=EMPTY
          put "<" & "tr>"&RETURN into tRowHtml
          repeat with z=1 to 8
            tColName="RptFld"&z
            tHoldText=line x of field tColName
            if tHoldText=EMPTY then
              tHoldText=" "
            end if
            put "<" & "td width=104pt align=left>"&tHoldText&"<" & "/td>" after tRowHtml
          end repeat
          put RETURN after tRowHtml
          put "<" & "/tr>"&RETURN after tRowHtml
          put tRowHtml after tConvertToTable
        end repeat
      end if
      put "<" & "/table>"&Return after tConvertToTable
      put "<" & "table>"&RETURN after tConvertToTable
    end if
    
    -- add the html ending tags
    put "<" & "/table>"&Return after tConvertToTable
    put "<" & "/body>"&Return after tConvertToTable
    put "<" & "/html>"&Return after tConvertToTable
    -- write the assembled html to a file
    writeString(tExportFile,tConvertToTable)
    closeFile(tExportFile)
    set tExportFile = 0
    set tConvertToTable=EMPTY
    -- if the command key was down, open the file, else just print the file
    if tCd=1 then
      tCheck=baOpenFile(tFilePath,"Normal")
    else
      tCheck=baPrintFile(tFilePath)
    end if
    if tCheck<" & "32 then
      alert "There is a problem opening the application."
    end if
  end if
end

To remove the temporary print file from the user computer add something like this to your "Quit" routine…

on Quit_Movie
  -- delete temporary print file if it exists
  tFilePath=the moviePath&"Prnttemp.html"
  baDeleteFile(tFilePath)
  quit
end

And if you just need to print a text member, use this instead of the repeat loop...

on mouseUp
  --create a temporary print file
  set tExportFile = new(xtra "FileIO")
  tFilePath=the moviePath&"Prnttemp.html"
  -- standard FileIO stuff...
  createFile(tExportFile,tFilePath)
  set tErr = status(tExportFile)
  if tErr = -122 then
    openfile(tExportFile,tFilePath,2)
    delete(tExportFile)
    createFile(tExportFile,tFilePath)
  end if
  openfile(tExportFile,tFilePath,2)
  -- store the heading stuff first to be html conforming!
  put "<" & "!DOCTYPE HTML PUBLIC"&RETURN into tConvertToFile
  put QUOTE&"-//W3C//DTD HTML 4.01 Transitional//EN""E&RETURN after tConvertToFile
  put QUOTE&"http://www.w3.org/TR/html4/loose.dtd""E&">"&Return after tConvertToFile

  -- include the html of the formatted text member
  put member("my_html_text_member").html after tConvertToFile

  -- write the assembled html to a file
  writeString(tExportFile,tConvertToFile)
  closeFile(tExportFile)
  set tExportFile = 0
  set tConvertToTable=EMPTY
  tCheck=baPrintFile(tFilePath)
  if tCheck<" & "32 then
    alert "There is a problem printing the file."
  end if
end

Using routines like the above scripts, any text/field members can easily be printed. And, I can quickly design complex page layouts with exact positioning; specific fonts; complete border, spacing, and color control, with just a bit of research on Cascading Style Sheets and some thought to html tables, which are then embedded in the script.

In Fact, anything that can be viewed in a browser can be designed into your script, with much more speed and  ease that programming with PrintOMatic.

Saul Krimsly
Intelligent Solutionz, Inc.
saul@itsolutionz.com  

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail