module PrintDrivers

Public Class Methods

included(base) click to toggle source
# File lib/print_drivers.rb, line 15
def self.included(base)

  # build PDF or somehow prepare printer output
  def rendered_on(*args)
    update_column(:state, "rendering")

    # TODO 2013-06-08 set the action_printer with respect to the selected output & printer
    action_printer = case print_driver
    when :pdf, "pdf"
      require 'oxen_printer/pdf_printer'
      PdfPrinter.new(printer, paper)
    when :cab, "cab"
      require 'oxen_printer/cab_printer'
      CabPrinter.new(printer, paper)
    when :label, "label"
      require 'oxen_printer/label_printer'
      LabelPrinter.new(printer, paper)
    when :html, "html"
      require 'oxen_printer/html_printer'
      HtmlPrinter.new(printer, paper)
    when :csv, "csv"
      require 'oxen_printer/csv_printer'
      CsvPrinter.new(printer, paper)
    end

    # print_job.action_printer.printer = print_job.printer
    # print_job.cycle if
    return action_printer if action_printer.do_render(self,args)
    update_column(:state, 'error rendering')
    raise PrintJobRenderingError
  end

  #
  # called from the perform on the print_job
  # print_with will use the action_printer to build the print
  # and return the resulting filepath
  #
  def print_with action_printer, args
    update_column(:state, "printing")
    args = set_default_args args
    status = case args[:medium]
    when "email"
      if action_printer
        printed_by.email = args[:email_to]
        action_printer.printer.command="email"
        action_printer.do_print(self,args)
        update_column(:state, 'done')
        action_printer.file_path
      else
        update_column(:state, 'error printing')
        raise PrintJobPrintingError.new t('print_drivers.print_with.email_error')
      end
    when "printer"
      if action_printer && action_printer.do_print(self,args)
        update_column(:state, 'done')
        action_printer.file_path
      else
        update_column(:state, 'error printing')
        raise PrintJobPrintingError.new I18n.t('print_drivers.print_with.print_error')
      end
    else
      raise PrintJobPrintingError.new I18n.t('print_drivers.print_with.case_error');
    end
  end

  def send_with action_printer, context
    update_column(:state, 'sending')
    if action_printer && action_printer.send_print_file( context)
      update_column(:state, 'done')
      action_printer.file_path
    else
      update_column(:state, 'error sending')
      raise PrintJobPrintingError
    end
  end

  def set_default_args args
    args[0][:print] ||= {}
    args[0][:print][:copies] ||= 1
    args[0][:print][:medium] ||= "printer"
    args[0][:print]
  rescue
    Rails.logger.error "#{args}"
    { copies: 1, medium: "printer" }
  end

  # q%{
  #   Problem with CUPS printer definitions - losing knowledge of attached printers
  #
  #   D [08/Dec/2008:16:20:25 +0100] cupsdAuthorize: No authentication data provided.
  #   D [08/Dec/2008:16:20:25 +0100] Print-Job ipp://localhost/printers/HP_LaserJet_M3035_MPF_192.168.105.78
  #   D [08/Dec/2008:16:20:25 +0100] print_job: auto-typing file...
  #   D [08/Dec/2008:16:20:25 +0100] print_job: request file type is application/pdf.
  #   D [08/Dec/2008:16:20:25 +0100] Print-Job client-error-not-found: The printer or class was not found.
  #   D [08/Dec/2008:16:20:25 +0100] cupsdProcessIPPRequest: 8 status_code=406 (client-error-not-found)
  #   D [08/Dec/2008:16:20:25 +0100] cupsdCloseClient: 8
  #
  # }
end

Public Instance Methods

print_with(action_printer, args) click to toggle source

called from the perform on the print_job print_with will use the action_printer to build the print and return the resulting filepath

rendered_on(*args) click to toggle source

build PDF or somehow prepare printer output

# File lib/print_drivers.rb, line 18
def rendered_on(*args)
  update_column(:state, "rendering")

  # TODO 2013-06-08 set the action_printer with respect to the selected output & printer
  action_printer = case print_driver
  when :pdf, "pdf"
    require 'oxen_printer/pdf_printer'
    PdfPrinter.new(printer, paper)
  when :cab, "cab"
    require 'oxen_printer/cab_printer'
    CabPrinter.new(printer, paper)
  when :label, "label"
    require 'oxen_printer/label_printer'
    LabelPrinter.new(printer, paper)
  when :html, "html"
    require 'oxen_printer/html_printer'
    HtmlPrinter.new(printer, paper)
  when :csv, "csv"
    require 'oxen_printer/csv_printer'
    CsvPrinter.new(printer, paper)
  end

  # print_job.action_printer.printer = print_job.printer
  # print_job.cycle if
  return action_printer if action_printer.do_render(self,args)
  update_column(:state, 'error rendering')
  raise PrintJobRenderingError
end
send_with(action_printer, context) click to toggle source
# File lib/print_drivers.rb, line 80
def send_with action_printer, context
  update_column(:state, 'sending')
  if action_printer && action_printer.send_print_file( context)
    update_column(:state, 'done')
    action_printer.file_path
  else
    update_column(:state, 'error sending')
    raise PrintJobPrintingError
  end
end
set_default_args(args) click to toggle source
# File lib/print_drivers.rb, line 91
def set_default_args args
  args[0][:print] ||= {}
  args[0][:print][:copies] ||= 1
  args[0][:print][:medium] ||= "printer"
  args[0][:print]
rescue
  Rails.logger.error "#{args}"
  { copies: 1, medium: "printer" }
end