Differenze tra le versioni di "Stampare un file con le funzioni del API di cups"

Da Gambas-it.org - Wikipedia.
 
(10 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
Per stampare un file con le funzioni esterne del API di ''CUPS'' (''Common Unix Printing System''), e dunque senza l'uso della Classe ''Printer'', bisognerà richiamare l'attuale libreria esterna: ''libcups.so.2'' .
 
Per stampare un file con le funzioni esterne del API di ''CUPS'' (''Common Unix Printing System''), e dunque senza l'uso della Classe ''Printer'', bisognerà richiamare l'attuale libreria esterna: ''libcups.so.2'' .
 
  
 
Mostriamo di seguito due esempi pratici; un semplice e breve codice per stampare un solo file:
 
Mostriamo di seguito due esempi pratici; un semplice e breve codice per stampare un solo file:
Riga 10: Riga 9:
 
  Library "libcups:2"
 
  Library "libcups:2"
 
   
 
   
  <FONT color=gray>' ''int cupsGetDests(cups_dest_t **dests)''</font>
+
  <FONT color=gray>' ''int cupsGetDests(cups_dest_t **dests)''
 +
' ''Get the list of destinations from the default server.''</font>
 
  Private Extern cupsGetDests(dests As Pointer) As Integer
 
  Private Extern cupsGetDests(dests As Pointer) As Integer
 
   
 
   
  <FONT color=gray>' ''int cupsPrintFile(const char *printer, const char *filename, const char *title, int num_options, cups_option_t *options)''</font>
+
  <FONT color=gray>' ''int cupsPrintFile(const char *printer, const char *filename, const char *title, int num_options, cups_option_t *options)''
 +
' ''Print a file to a printer or class on the default server.''</font>
 
  Private Extern cupsPrintFile(stampante As String, nomefile As String, titolo As String, num_opzioni As Integer, opzioni As Cups_option_s) As Integer
 
  Private Extern cupsPrintFile(stampante As String, nomefile As String, titolo As String, num_opzioni As Integer, opzioni As Cups_option_s) As Integer
 
   
 
   
Riga 19: Riga 20:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
   Dim stamp_Def, predef As String
+
   Dim fl, stamp_Def, predef, stampa As String
 
   Dim num_dests, err As Integer
 
   Dim num_dests, err As Integer
   Dim p, pstmp As Pointer
+
   Dim p As Pointer
 
   Dim st As Stream
 
   Dim st As Stream
 
   Dim b, j As Byte
 
   Dim b, j As Byte
 
   
 
   
 +
  fl = "<FONT color=gray>'''''/percorso/del/file/da/stampare'''''</font>"
 
   
 
   
  num_dests = cupsGetDests(VarPtr(p))
+
  num_dests = cupsGetDests(VarPtr(p))
 
   
 
   
  Print "Sono state riscontrate installate nel sistema num. "; num_dests; " stampanti:\n"
+
  Print "Sono state riscontrate installate nel sistema num. "; num_dests; " stampanti:\n"
 
   
 
   
  <FONT color=gray>' ''Dereferenziando la variabile di tipo "Puntatore" si ottengono i nomi delle stampanti installate.''
+
  <FONT color=gray>' ''Dereferenziando la variabile di tipo "Puntatore", si ottengono i nomi delle stampanti installate.''
  ' ''In particolare ciascun nome è uguale a quello del file .ppd afferente alla stampante installata,''
+
  ' ''In particolare ciascun nome è uguale a quello del file .ppd afferente alla stampante installata, presente nella cartella "/etc/cups/ppd" e privo della sua estensione.''
' ''presente nella cartella "/etc/cups/ppd" e privo della sua estensione.''
 
 
  ' ''Il nome della stampante andrà passato, come primo argomento, alla funzione "cupsPrintFile()".''</font>
 
  ' ''Il nome della stampante andrà passato, come primo argomento, alla funzione "cupsPrintFile()".''</font>
  For b = 1 To num_dests
+
  For b = 1 To num_dests
    stmp = String@(Pointer@(p))
+
    stampa = String@(Pointer@(p))
    p = p + 16
+
    p = p + 16
    If Byte@(p) = 1 Then
+
    If Byte@(p) = 1 Then
      stamp_Def = stmp
+
      stamp_Def = stampa
      predef = " - Default\n"
+
      predef = " - Default\n"
    Endif
+
    Endif
  <FONT color=gray>' ''Mostriamo in console il nome delle stampanti trovate installate nel sistema:''</font>
+
  <FONT color=gray>' ''Mostra in console il nome delle stampanti trovate installate nel sistema:''</font>
    Print b, stmp & predef
+
    Print b, stampa & predef
    predef = Null
+
    predef = Null
    p = p + 16
+
    p = p + 16
  Next
+
  Next
 
    
 
    
 
   
 
   
 
  <FONT color=gray>' ''Se, invece, volessimo usare i "Memory Stream":''
 
  <FONT color=gray>' ''Se, invece, volessimo usare i "Memory Stream":''
  ' '''st = Memory p For Read'''
+
  ' '''Dim p, pstmp As Pointer'''
 +
 +
  ' '''st = Memory p For Read'''
 +
 
 +
  ' '''For b = 0 To num_dests - 1'''
 +
    ' '''Seek #st, b * 32'''
 +
    ' '''Read #st, pstmp'''
 +
    ' '''Seek #st, Seek(st) + 8'''
 +
    ' '''Read #st, j'''
 +
    ' '''If j = 1 Then'''
 +
      ' '''stamp_Def = String@(pstmp)'''
 +
      ' '''predef = " - Default\n"'''
 +
    ' '''Endif'''
 +
    ' '''Print b + 1, String@(pstmp) & predef'''
 +
    ' '''predef = Null'''
 +
  ' '''Next'''
 +
  '
 +
  ' '''st.Close()'''</font>
 
    
 
    
  ' '''For b = 0 To num_dests - 1'''
+
  <FONT color=gray>' ''In questo esempio passa alla funzione "cupsPrintFile()" il nome della stampante 'predefinita' trovata:''</font>
    ' '''Seek #st, b * 32'''
+
  Print "\nStampa sulla stampante: "; stamp_Def
    ' '''Read #st, pstmp'''
 
    ' '''Seek #st, Seek(st) + 8'''
 
    ' '''Read #st, j'''
 
    ' '''If j = 1 Then'''
 
      ' '''stamp_Def = String@(pstmp)'''
 
      ' '''predef = " - Default\n"'''
 
    ' '''Endif'''
 
    ' '''Print b + 1, String@(pstmp) & predef'''
 
    ' '''predef = Null'''
 
  ' '''Next'''
 
  '
 
  ' '''st.Close()'''</font>
 
 
 
  <FONT color=gray>' ''In questo esempio passiamo alla funzione "cupsPrintFile()" il nome della stampante 'predefinita' trovata:''</font>
 
  Print "\nStampa sulla stampante: "; stamp_Def
 
 
    
 
    
  err = cupsPrintFile(stamp_Def, "<FONT color=gray>''/percorso/del/file/da/stampare''</font>", "Breve titolo qualsiasi", 0, Null)
+
  err = cupsPrintFile(stamp_Def, fl, "Breve titolo qualsiasi", 0, Null)
  If err = 0 then Error.Raise("Stampa del file fallita !")
+
  If err == 0 then Error.Raise("Stampa del file fallita !")
 
   
 
   
 
  '''End'''
 
  '''End'''
 
+
Un altro esempio un po' più complesso che utilizza altre funzioni esterne di ''CUPS'' rispetto al precedente esempio:
 
 
 
 
ed un altro un po' più complesso che utilizza altre funzioni esterne di ''CUPS'' rispetto al precedente esempio:
 
 
  Public Struct Cups_option_s    <FONT color=gray>' ''Opzioni di stampa''</font>
 
  Public Struct Cups_option_s    <FONT color=gray>' ''Opzioni di stampa''</font>
 
   name As Pointer              <FONT color=gray>' ''Nome dell'opzione''</font>
 
   name As Pointer              <FONT color=gray>' ''Nome dell'opzione''</font>
Riga 98: Riga 97:
 
  Private Const CUPS_FORMAT_AUTO As String = "application/octet-stream"
 
  Private Const CUPS_FORMAT_AUTO As String = "application/octet-stream"
 
   
 
   
 +
<FONT color=gray>' ''int cupsGetDests(cups_dest_t **dests)''
 +
' ''Get the list of destinations from the default server.''</font>
 +
Private Extern cupsGetDests(dests As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''cups_file_t *cupsFileOpen(const char *filename, const char *mode)''
 
  <FONT color=gray>' ''cups_file_t *cupsFileOpen(const char *filename, const char *mode)''
Riga 130: Riga 132:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
   Dim cups_file As Pointer
+
   Dim p, cups_file As Pointer
   Dim fl, stampante As String
+
  Dim num_dests As Integer
 +
  Dim b As Byte
 +
   Dim fl, stampa, predef, stampante As String
 
   Dim id, http_stato, byte As Integer
 
   Dim id, http_stato, byte As Integer
 
   Dim buffer As Byte[]                        <FONT color=gray>' ''Buffer di lettura/scrittura''</font>
 
   Dim buffer As Byte[]                        <FONT color=gray>' ''Buffer di lettura/scrittura''</font>
 
   Dim dest As New Cups_dest_s
 
   Dim dest As New Cups_dest_s
 
    
 
    
    
+
   fl = "<FONT color=gray>'''''/percorso/del/file/da/stampare'''''</font>"
  fl = "''/percorso/del/file/da/stampare''"
 
  stampante = "''nome_della_stampante''"
 
 
 
  cups_file = cupsFileOpen(fl, "r")
 
  If IsNull(cups_file) Then Error.Raise("Impossibile aprire il file !")
 
   
 
  id = cupsCreateJob(CUPS_HTTP_DEFAULT, stampante, "testo qualsiasi", 0, Null)
 
  If id <= 0 Then Error.Raise("Impossibile stampare con 'Canon-BJ-200' !")
 
  Print "Processo di stampa num. "; id
 
 
   
 
   
  http_stato = cupsStartDocument(CUPS_HTTP_DEFAULT, stampante, id, "nome documento", CUPS_FORMAT_AUTO, 1)
+
  num_dests = cupsGetDests(VarPtr(p))
  If http_stato <> HTTP_CONTINUE Then Error.Raise("Impossibile avviare la stampa del documento !")
+
  Print "Sono state riscontrate installate nel sistema num. "; num_dests; " stampanti:\n"
   
 
  buffer = New Byte[Stat(fl).Size]
 
   
 
  byte = cupsFileRead(cups_file, buffer, buffer.Count)
 
  While byte > 0
 
 
   
 
   
    If cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, byte) <> HTTP_CONTINUE Then Error.Raise("Impossibile scrivere byte !")
+
  For b = 1 To num_dests
 +
    stampa = String@(Pointer@(p))
 +
    p = p + 16
 +
    If Byte@(p) = 1 Then
 +
      stampante = stampa
 +
      predef = " - Default\n"
 +
    Endif
 +
<FONT color=gray>' ''Mostriamo in console il nome delle stampanti trovate installate nel sistema:''</font>
 +
    Print b, stampa & predef
 +
    predef = Null
 +
    p = p + 16
 +
  Next
 +
 
 +
  cups_file = cupsFileOpen(fl, "r")
 +
  If IsNull(cups_file) Then Error.Raise("Impossibile aprire il file !")
 +
 
 +
  id = cupsCreateJob(CUPS_HTTP_DEFAULT, stampante, "testo qualsiasi", 0, Null)
 +
  If id <= 0 Then Error.Raise("Impossibile stampare con '" & stampante & "' !")
 +
  Print "Processo di stampa num. "; id
 
   
 
   
    Wait 0.5
+
  http_stato = cupsStartDocument(CUPS_HTTP_DEFAULT, stampante, id, "nome documento", CUPS_FORMAT_AUTO, 1)
     
+
  If http_stato <> HTTP_CONTINUE Then Error.Raise("Impossibile avviare la stampa del documento !")
    byte = cupsFileRead(cups_file, buffer, buffer.Count)
 
     
 
  Wend
 
 
      
 
      
 +
  buffer = New Byte[Stat(fl).Size]
 
      
 
      
  cupsFinishDocument(CUPS_HTTP_DEFAULT, stampante)
+
  byte = cupsFileRead(cups_file, buffer, buffer.Count)
   
+
  While byte > 0
  dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, Null, Null)
+
    If cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, byte) <> HTTP_CONTINUE Then Error.Raise("Impossibile scrivere byte !")
  Print "Stampa del documento su: "; String@(dest.name)
+
    Wait 0.5
 
+
    byte = cupsFileRead(cups_file, buffer, buffer.Count)
 +
  Wend
 +
 +
  cupsFinishDocument(CUPS_HTTP_DEFAULT, stampante)
 +
  dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, Null, Null)
 +
  Print "Stampa del documento su: "; String@(dest.name)
 +
 
 
  '''End'''
 
  '''End'''
 
  
  
Riga 176: Riga 187:
 
=Riferimenti=
 
=Riferimenti=
 
* [http://www.cups.org/documentation.php?VERSION=2.0&INDEX=0&Q= API di cups]
 
* [http://www.cups.org/documentation.php?VERSION=2.0&INDEX=0&Q= API di cups]
* [https://refspecs.linuxfoundation.org/LSB_3.2.0/LSB-Printing/LSB-Printing/book1.html Linux Standard Base Printing Specification 3.2]
+
* [https://refspecs.linuxfoundation.org/LSB_4.0.0/LSB-Printing/LSB-Printing/book1.html Linux Standard Base Printing Specification 4.0]
 
* [http://fossies.org/dox/cups-2.0.0-source/index.html cups Documentation]
 
* [http://fossies.org/dox/cups-2.0.0-source/index.html cups Documentation]

Versione attuale delle 10:56, 22 giu 2023

Per stampare un file con le funzioni esterne del API di CUPS (Common Unix Printing System), e dunque senza l'uso della Classe Printer, bisognerà richiamare l'attuale libreria esterna: libcups.so.2 .

Mostriamo di seguito due esempi pratici; un semplice e breve codice per stampare un solo file:

Public Struct Cups_option_s     ' Opzioni di stampa
  name As Pointer               ' Nome dell'opzione
  value As Pointer	        ' Valore dell'opzione
End Struct

Library "libcups:2"

' int cupsGetDests(cups_dest_t **dests)
' Get the list of destinations from the default server.
Private Extern cupsGetDests(dests As Pointer) As Integer

' int cupsPrintFile(const char *printer, const char *filename, const char *title, int num_options, cups_option_t *options)
' Print a file to a printer or class on the default server.
Private Extern cupsPrintFile(stampante As String, nomefile As String, titolo As String, num_opzioni As Integer, opzioni As Cups_option_s) As Integer


Public Sub Main()

 Dim fl, stamp_Def, predef, stampa As String
 Dim num_dests, err As Integer
 Dim p As Pointer
 Dim st As Stream
 Dim b, j As Byte

 fl = "/percorso/del/file/da/stampare"

 num_dests = cupsGetDests(VarPtr(p))

 Print "Sono state riscontrate installate nel sistema num. "; num_dests; " stampanti:\n"

' Dereferenziando la variabile di tipo "Puntatore", si ottengono i nomi delle stampanti installate.
' In particolare ciascun nome è uguale a quello del file .ppd afferente alla stampante installata, presente nella cartella "/etc/cups/ppd" e privo della sua estensione.
' Il nome della stampante andrà passato, come primo argomento, alla funzione "cupsPrintFile()".
 For b = 1 To num_dests
   stampa = String@(Pointer@(p))
   p = p + 16
   If Byte@(p) = 1 Then
     stamp_Def = stampa
     predef = " - Default\n"
   Endif
' Mostra in console il nome delle stampanti trovate installate nel sistema:
   Print b, stampa & predef
   predef = Null
   p = p + 16
 Next
  

' Se, invece, volessimo usare i "Memory Stream":
 ' Dim p, pstmp As Pointer

 ' st = Memory p For Read
 
 ' For b = 0 To num_dests - 1
   ' Seek #st, b * 32
   ' Read #st, pstmp
   ' Seek #st, Seek(st) + 8
   ' Read #st, j
   ' If j = 1 Then
     ' stamp_Def = String@(pstmp)
     ' predef = " - Default\n"
   ' Endif
   ' Print b + 1, String@(pstmp) & predef
   ' predef = Null
 ' Next
 '
 ' st.Close()
 
' In questo esempio passa alla funzione "cupsPrintFile()" il nome della stampante 'predefinita' trovata:
 Print "\nStampa sulla stampante: "; stamp_Def
 
 err = cupsPrintFile(stamp_Def, fl, "Breve titolo qualsiasi", 0, Null)
 If err == 0 then Error.Raise("Stampa del file fallita !")

End

Un altro esempio un po' più complesso che utilizza altre funzioni esterne di CUPS rispetto al precedente esempio:

Public Struct Cups_option_s     ' Opzioni di stampa
  name As Pointer               ' Nome dell'opzione
  value As Pointer	        ' Valore dell'opzione
End Struct

Public Struct cups_dest_s
  name As Pointer
  instance As Pointer
  is_default As Integer
  num_options As Integer
  options As Cups_option_s
End Struct


Library "libcups:2"

Private Const CUPS_HTTP_DEFAULT As Integer = 0
Private Const HTTP_CONTINUE As Integer = 100
Private Const CUPS_FORMAT_AUTO As String = "application/octet-stream"

' int cupsGetDests(cups_dest_t **dests)
' Get the list of destinations from the default server.
Private Extern cupsGetDests(dests As Pointer) As Integer

' cups_file_t *cupsFileOpen(const char *filename, const char *mode)
' Open a CUPS file.
Private Extern cupsFileOpen(filename As String, mode As String) As Pointer

' int cupsCreateJob(http_t *http, const char *name, const char *title, int num_options, cups_option_t *options)
' Create an empty job for streaming.
Private Extern cupsCreateJob(http As Pointer, name As String, title As String, num_options As Integer, options As Cups_option_s) As Integer

' http_status_t cupsStartDocument(http_t *http, const char *name, int job_id, const char *docname, const char *format, int last_document)
' Add a document to a job created with cupsCreateJob().
Private Extern cupsStartDocument(http As Pointer, name As String, jobI As Integer, docname As String, format$ As String, last_document As Integer) As Integer

' ssize_t cupsFileRead(cups_file_t *fp, char *buf, size_t bytes)
' Read from a file.
Private Extern cupsFileRead(fp As Pointer, buf As Byte[], size_t As Integer) As Integer

' http_status_t cupsWriteRequestData(http_t *http, const char *buffer, size_t length)
' Write additional data after an IPP request.
Private Extern cupsWriteRequestData(http As Pointer, buf As Byte[], length As Integer) As Integer

' ipp_status_t cupsFinishDocument(http_t *http, const char *name)
' Finish sending a document.
Private Extern cupsFinishDocument(http As Pointer, name As String) As Integer

' cups_dest_t *cupsGetNamedDest(http_t *http, const char *name, const char *instance)
' Get options for the named destination.
Private Extern cupsGetNamedDest(http As Pointer, name As String, instance As String) As Cups_dest_s


Public Sub Main()

 Dim p, cups_file As Pointer
 Dim num_dests As Integer
 Dim b As Byte
 Dim fl, stampa, predef, stampante As String
 Dim id, http_stato, byte As Integer
 Dim buffer As Byte[]                        ' Buffer di lettura/scrittura
 Dim dest As New Cups_dest_s
 
 fl = "/percorso/del/file/da/stampare"

 num_dests = cupsGetDests(VarPtr(p))
 Print "Sono state riscontrate installate nel sistema num. "; num_dests; " stampanti:\n"

 For b = 1 To num_dests
   stampa = String@(Pointer@(p))
   p = p + 16
   If Byte@(p) = 1 Then
     stampante = stampa
     predef = " - Default\n"
   Endif
' Mostriamo in console il nome delle stampanti trovate installate nel sistema:
   Print b, stampa & predef
   predef = Null
   p = p + 16
 Next
  
 cups_file = cupsFileOpen(fl, "r")
 If IsNull(cups_file) Then Error.Raise("Impossibile aprire il file !")
  
 id = cupsCreateJob(CUPS_HTTP_DEFAULT, stampante, "testo qualsiasi", 0, Null)
 If id <= 0 Then Error.Raise("Impossibile stampare con '" & stampante & "' !")
 Print "Processo di stampa num. "; id

 http_stato = cupsStartDocument(CUPS_HTTP_DEFAULT, stampante, id, "nome documento", CUPS_FORMAT_AUTO, 1)
 If http_stato <> HTTP_CONTINUE Then Error.Raise("Impossibile avviare la stampa del documento !")
   
 buffer = New Byte[Stat(fl).Size]
   
 byte = cupsFileRead(cups_file, buffer, buffer.Count)
 While byte > 0
   If cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, byte) <> HTTP_CONTINUE Then Error.Raise("Impossibile scrivere byte !")
   Wait 0.5
   byte = cupsFileRead(cups_file, buffer, buffer.Count)
 Wend

 cupsFinishDocument(CUPS_HTTP_DEFAULT, stampante)
 dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, Null, Null)
 Print "Stampa del documento su: "; String@(dest.name)
 
End


Riferimenti