printing on linux

Linux specific questions.
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

printing on linux

Post by stevie1401 »

Under windows it gives api. can I a printer under linux program, so that I can print also pictures and format text?
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Post by yetifoot »

I haven't ever tried to program for it, but I think one thing you should look into is CUPS (common unix printing system)
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Post by sir_mud »

kde and gnome both provide a common interface for printing, for gnome's version it's probaly closely related to gimp-print
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

Post by stevie1401 »

and how can i use this interface?
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

I read a little about CUPS a while back: CUPS provides printer management and spooling as well as rasterization (filters and drivers) for a wide range printers. It has no API for creating documents. But it can read PostScript files and apply many print related options before actually printing.

On linux, it would seem the way to make an application print enabled is by first being able to generate PostScript output. Then the PostScript could be piped to CUPS using OPEN PIPE or a SHELL command, or fed directly to the printer (via 'lp') using OPEN LPT.
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

Post by stevie1401 »

Can you give me an example for a script for CUPS?
i.e. print <bold>"This is a bold Text"
<loadpic> 100,100,"mypic.bmp"
text 100,200,<green>"This is a green Text"
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

CUPS doesn't have scripts. It reads PostScript files. There are many examples, tutorials, and complete PostScript (.ps) files available on the web. For a start, see http://en.wikipedia.org/wiki/PostScript

I haven't programmed in PostScript much (it is its own language), so sorry the following example is rough.

Code: Select all

#include once "fbgfx.bi"

'' The ps_* subs generate PostScript
'' - replace these with whatever works better
'' - check the PostScript specs for more information

dim shared ps_buffer as string

'':::::
sub ps_begin()
  ps_buffer = !"%!PS\n"
end sub

'':::::
sub ps_write_nl()
  ps_buffer &= !"\n"
end sub

'':::::
sub ps_write( byref x as string )
  dim i as integer = any
  if( len( x ) > 72 ) then
    for i = 1 to len( x ) step 72
      ps_buffer &= mid( x, i, 72 )
      if( i <= len(x) - 72 ) then
        ps_write_nl()
      end if
    next i
  else
    ps_buffer &= x
  end if
end sub

'':::::
sub ps_write_image _
  ( _
    byval img as fb.IMAGE ptr, _
    byval px as integer, _
    byval py as integer, _
    byval sx as integer, _
    byval sy as integer _
  )

  dim as integer x = any, y = any
  dim as uinteger c = any
  dim s as string = ""

  for y = 0 to img->height - 1
    for x = 0 to img->width - 1
      s &= hex( point( x, y, img ), 6 )
    next x
  next y

  ps_write( "" & px & " " & py & " translate" )
  ps_write_nl()
  ps_write( "" & sx & " " & sy & " scale" )
  ps_write_nl()
  ps_write( "" & img->width & " " & img->height & " 8 " )
  ps_write( " [" & img->width & " 0 0 " & -cint(img->height) & " 0 " & img->height & "]" )
  ps_write_nl()
  ps_write( "{<" )
  ps_write_nl()
  ps_write( s )
  ps_write_nl()
  ps_write( ">}" )
  ps_write_nl()
  ps_write( "false 3 colorimage" )
  ps_write_nl()

end sub

'':::::
sub ps_write_text _
  ( _
    byval px as integer, _
    byval py as integer, _
    byval sx as integer, _
    byref txt as string _
  )

  ps_write( "/Times-Roman findfont" )
  ps_write_nl()
  ps_write( "" & sx & " scalefont" )
  ps_write_nl()
  ps_write( "setfont" )
  ps_write_nl()
  ps_write( "newpath" )
  ps_write_nl()
  ps_write( "" & px & " " & py & " moveto" )
  ps_write_nl()
  ps_write( "(" & txt & ") show" )
  ps_write_nl()

end sub

'':::::
sub ps_end()
  ps_buffer &= "%%EOF" & !"\n"
end sub

'' ============
'' This creates a sample image to put on the page

'':::::
function CreateSampleImage _
  ( _
    byval w as integer, _
    byval h as integer _
  ) as fb.IMAGE ptr

  dim as integer i = any, x = any, y = any
  dim as fb.IMAGE ptr img = any

  img = ImageCreate( w, h, rgb(255,255,255) )

  for y = 0 to img->height - 1
    for x = 0 to img->width - 1
      pset img, ( x, y ), _
        rgb( _
          x / ( img->width - 1 ) * 127 + 128, _
          y / ( img->height - 1 ) * 127 + 128, _
          0 _
        )
    next x
  next y

  for i = 0 to 15 step 3
    line img, ( i, i ) - ( w - i - 1, h - i - 1 ), 0, b
  next i

  circle img, (w / 2, h / 4), h / 8, 0
  circle img, (w / 4, h / 2), w / 8, 0

  line img, ( w/2, h/2 ) - ( w, h ), 0

  draw string img, ( 20, 20 ), "Top", rgb(255,255,255)
  draw string img, ( 20, 32 ), "Left", rgb(255,255,255)

  function = img

end function

''=================
'' Example

dim img as any ptr = any

'' Use a NULL gfx mode to make the gfx image
ScreenRes 100, 100, 32, , fb.GFX_NULL

img = CreateSampleImage( 150, 100 )

'' Generate the post script
ps_begin()
ps_write_text( 100, 500, 50, "Hello, World!" )
ps_write_image( img, 100, 200, 300, 200 )
ps_end()

ImageDestroy img


'' This is the part of the program that actually does 
'' the printing using the data saved in ps_buffer
'' The following shows three methods for getting
'' the PostScript to a printer

'' Method #1: Send to printer (via lp) using OPEN LPT
open lpt "lpt:" for output as #1
put #1,, ps_buffer
close #1

'' Method #2: Send to 'lp' using OPEN PIPE
open pipe "lp" for output as #1
put #1,, ps_buffer
close #1

'' Write the PostScript to a file
open "sample.ps" for output as #1
close #1
open "sample.ps" for binary as #1
put #1,, ps_buffer
close #1

'' Method #3: print the file we just made using shell
shell "lp sample.ps"
The example above makes a "sample.ps" file, and shows three ways to send it to the print spooler.

You should notice that more than 80% of the program is just to generate the sample image and PostScript output. However, with that done, actual printing to the printer is only a few lines of code.

Requires a CVS version of FreeBASIC to compile.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

coderJeff wrote:Requires a CVS version of FreeBASIC to compile.
hehe not anymore! (unless you just did that >.>)
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

Oh yeah... that should be: requires a 2007-05-01 build.

The newer printer stuff was added just after the 2006-07-30 build, so anything to do with printing was CVS only for last 8 months.
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

Post by stevie1401 »

Your example doesn't work with an picture.
Can anyone give an example with an loaded picture?

What are the parameter in the sub ps_write_image?

px= x-koordinate of the picture?
py= y-koordinate of the picture?
sx=the width of the picture?
sy=the height of the picture?

Thank you

Stevie1401
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

Post by stevie1401 »

That is the error message, I get, if I try to print an loaded picture:

%%[ Error: limitcheck; OffendingCommand: --nostringval-- ]%%


It works with pictures which are 100*100 or smaler.
Why?

Stevie1401
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Post by marcov »

What I've done in the past is simply generating LaTeX code, running it through LaTeX, and then printing the resulting .ps via CUPS or lpr.
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

Post by stevie1401 »

Can you please give an example?

Thank you.

Stevie1401
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

For small images I used the information in this tutorial:
http://local.wasp.uwa.edu.au/~pbourke/d ... ostscript/

There is also some information there about printing large images.
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

Post by stevie1401 »

Unfortunately I don't understand this.
I have tried a lot of possibilities, but nothing works.
Can you please give an example.
I thing, an example is ever the best way.

Thank you

Stevie1401
Post Reply