printing on linux
-
- Posts: 134
- Joined: Apr 19, 2007 16:20
- Contact:
printing on linux
Under windows it gives api. can I a printer under linux program, so that I can print also pictures and format text?
-
- Posts: 134
- Joined: Apr 19, 2007 16:20
- Contact:
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.
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.
-
- Posts: 134
- Joined: Apr 19, 2007 16:20
- Contact:
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.
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.
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"
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.
-
- Posts: 134
- Joined: Apr 19, 2007 16:20
- Contact:
-
- Posts: 134
- Joined: Apr 19, 2007 16:20
- Contact:
-
- Posts: 134
- Joined: Apr 19, 2007 16:20
- Contact:
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.
http://local.wasp.uwa.edu.au/~pbourke/d ... ostscript/
There is also some information there about printing large images.
-
- Posts: 134
- Joined: Apr 19, 2007 16:20
- Contact: