How to set the number of copies to Print In Linux?

Linux specific forum
swhite
Enthusiast
Enthusiast
Posts: 603
Joined: Thu May 21, 2009 6:56 pm

How to set the number of copies to Print In Linux?

Post by swhite »

Hi

Is there a way to set the number of copies to print in Linux? I know I could simply run the code twice but there are some things that are changed when the job is printed and thus would not be the same for the second time round so I would prefer to just tell the printer that 2 copies are required.

Thanks,
Simon
Simon White
dCipher Computing
User avatar
Shardik
Addict
Addict
Posts: 1826
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to set the number of copies to Print In Linux?

Post by Shardik »

Normally you would use PB's PrintRequester() to let the user select one of the available printers, the range of pages to print, the number of copies and other parameters. Or are you in need to print output without any user interaction?
swhite
Enthusiast
Enthusiast
Posts: 603
Joined: Thu May 21, 2009 6:56 pm

Re: How to set the number of copies to Print In Linux?

Post by swhite »

Hi

Yes I am in need of way to do this without user interaction.

Simon
Simon White
dCipher Computing
User avatar
mk-soft
Addict
Addict
Posts: 2948
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to set the number of copies to Print In Linux?

Post by mk-soft »

We've been wanting this for a long time.
But it seems to be very complex, because every printer has its own parameters and is also available for all OS.
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
User avatar
Shardik
Addict
Addict
Posts: 1826
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to set the number of copies to Print In Linux?

Post by Shardik »

In this old example from 2007 walker already demonstrated how to utilize the GTK Print API to output a page to a printer. I have taken his example and modified it to suppress the print dialog and directly output a page with a "Hello World!" text to the default printer while setting the number of copies to 2. In order to print your text however you have to fiddle around with setting the font and its size via Pango API calls in procedure draw_page(). Instead of the native PureBasic print functions the example below only uses GTK API functions...

I have tested walker's modified example successfully on Linux Mint 19.3 x64 with PB 5.72 x64.

Code: Select all

; --------------------------------------------------------------------------------
; using the GTK print dialog
; 2007 walker
; not fully functional yet but shows how to use this dialog
; BUT with a working preview ;-)
; --------------------------------------------------------------------------------
; Modifications by Shardik:
; -------------------------
; - ImportC path to libgtk-x11-2.0.so changed
; - CreateGadgetList(WindowID(0)) and CloseGadgetList() commented out
; - gtk_print_settings_set_n_copies() added to ImportC
; - Number of copies are set to 2 in event loop 
; --------------------------------------------------------------------------------

;---------------- Preparations ---------------------------------------------------
;import needed functions
;ImportC "/usr/lib/libgtk-x11-2.0.so"
ImportC "/usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so"
  gtk_print_operation_new()
  gtk_print_operation_run(*op, action, *parent, *error);the print dialog
  gtk_print_settings_new()
  gtk_print_operation_set_print_settings(*print,*settings)
  gtk_print_context_create_pango_layout(*context)
  pango_layout_set_text(layout,txt.p-utf8,length)
  pango_font_description_from_string(font.p-utf8)
  pango_font_description_free(desc)
  pango_layout_set_font_description(layout,desc)
  cairo_move_to(cr,x,y)
  pango_cairo_show_layout(cr,layout)
  gtk_print_context_get_cairo_context(context)
  gtk_print_settings_set_n_copies(*settings, num_copies.L)
  gtk_print_operation_set_n_pages(*op,n_pages)
  gtk_print_operation_set_unit(*op,unit)
  gtk_print_operation_get_status_string (*op); a (translated) string
  gtk_print_operation_get_status (*op);the numer enumerated below
  gtk_print_operation_is_finished (*op);returns true or false
EndImport

;print actions
Enumeration
  #GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
  #GTK_PRINT_OPERATION_ACTION_PRINT
  #GTK_PRINT_OPERATION_ACTION_PREVIEW
    #GTK_PRINT_OPERATION_ACTION_EXPORT
EndEnumeration
;return values from printdialog
Enumeration
  #GTK_PRINT_OPERATION_RESULT_ERROR
  #GTK_PRINT_OPERATION_RESULT_APPLY
  #GTK_PRINT_OPERATION_RESULT_CANCEL
  #GTK_PRINT_OPERATION_RESULT_IN_PROGRESS
EndEnumeration
;status of printing
Enumeration
  #GTK_PRINT_STATUS_INITIAL
  #GTK_PRINT_STATUS_PREPARING
  #GTK_PRINT_STATUS_GENERATING_DATA
  #GTK_PRINT_STATUS_SENDING_DATA
  #GTK_PRINT_STATUS_PENDING
  #GTK_PRINT_STATUS_PENDING_ISSUE
  #GTK_PRINT_STATUS_PRINTING
  #GTK_PRINT_STATUS_FINISHED
  #GTK_PRINT_STATUS_FINISHED_ABORTED
EndEnumeration 
;layout units
Enumeration
  #GTK_UNIT_PIXEL
  #GTK_UNIT_POINTS
  #GTK_UNIT_INCH
  #GTK_UNIT_MM
EndEnumeration

ProcedureCDLL begin_print(*op,context, user_data)
;this procedure is called before rendering the pages...

;set number of pages.. ESSENTIAL!!!! never forget this! (or you'll wait forever)
gtk_print_operation_set_n_pages(*op,1);

EndProcedure

ProcedureCDLL  draw_page(*op,context,page_nr,user_data)
;this Procedure is called for each page to print.... so a fixed text like this example isn't a good idea
gtk_print_operation_set_unit (*op, #GTK_UNIT_MM);
cr= gtk_print_context_get_cairo_context(context)
layout = gtk_print_context_create_pango_layout (context);
pango_layout_set_text (layout, "Hello World!", -1);
desc = pango_font_description_from_string ("sans 28");
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);
pango_cairo_show_layout (cr, layout)
g_object_unref_(layout)

EndProcedure

ProcedureCDLL  print_preview(*op,*preview, *context, *parent, user_data)
;for your own preview routine...

EndProcedure

ProcedureCDLL end_print(*op, *context, user_data)
;if printig is finished, you can add code here for some cleanups etc...

EndProcedure                                                       

;-------------------- DEMO -------------------------
OpenWindow(0,0,0,400,400,"printtest",#PB_Window_SystemMenu)
;CreateGadgetList(WindowID(0))
ButtonGadget(0,10,50,100,30,"Print")
;CloseGadgetList()

Repeat 
event=WaitWindowEvent()

    If event=#PB_Event_Gadget
        If EventGadget()=0
            ;print button
            ;create a new printoperation
            print=gtk_print_operation_new()

            ; ----- Set number of copies to 2
            *settings = gtk_print_settings_new()
            gtk_print_settings_set_n_copies(*settings, 2)
            gtk_print_operation_set_print_settings(print, *settings)

            ;connect the actions to a procedure
            g_signal_connect_(print, "begin-print", @begin_print(), user_data);
            g_signal_connect_(print, "draw-page", @draw_page(), user_data);
            ;If you activate the following line, the built in preview is DISABLED
            ; g_signal_connect_(print, "preview", @print_preview(), d);
            g_signal_connect_(print, "end-print", @end_print(), user_data);
            ;finally calling the GTK+ printrequester
            ;res=gtk_print_operation_run(print,#GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,WindowID(0),*error)
            res=gtk_print_operation_run(print,#GTK_PRINT_OPERATION_ACTION_PRINT,WindowID(0),*error)
            ;If res returns  #GTK_PRINT_OPERATION_RESULT_CANCEL the user had clicked on cancel - else one of the callbacks is called
        EndIf   
    EndIf   

Until event=#PB_Event_CloseWindow
Post Reply