lib/action_printer.rb in oxen_printer-0.4.5 vs lib/action_printer.rb in oxen_printer-0.4.7

- old
+ new

@@ -1,8 +1,8 @@ #encoding: utf-8 class ActionPrinter < ActionView::Base - + include Exceptions attr_accessor :printer, :paper def initialize(printer,paper) @@ -13,20 +13,24 @@ def command @printer.command end + def command=val + @printer.command=val + end + # # the default rendering is from a string of chars to an html file - statically placed # for who ever knows the exact path to the file, to see # def do_render(print_job,*args) path = print_job.view_template_path # TODO vi får en fejl her når vi begynder at mixe modellerne f.eks. Employee.find_by_sql('select * from products') rc = print_job.printing_class.constantize coll = rc.find_by_sql( print_job.print_sql) - locals = { resource_class: rc, collection: coll, resource: coll.first } + locals = { resource_class: rc, collection: coll, resource: coll.first, resources: coll } # # TODO args must be mergeable ;) # if args.flatten.compact.any? # locals.merge! args # end @@ -45,18 +49,22 @@ of.path end # the default print is sending a link to the user who ordered the print - def do_print copies, usr=nil - # move pdf_file_path to a static place where it will not be accidentally deleted from - # static_path = File.join(Rails.root,'public','files','1.pdf') - # var = %x[ mv #{pdf_file_path} #{static_path} 2>&1 ] - # send email to usr with link - # raise MovingFileFailedError.new(var) unless var.blank? - PrinterMailer.send_print(usr,file_path).deliver_now if usr - true + # + def do_print print_job, args + result = nil + prn = printer + if prn.nil? or prn.command.blank? or prn.command=="email" + mail_print print_job, args + else + print_print prn, print_job, args + end + rescue => e + logit :error, "Printing failed! The error was #{e.message}" + false end # # this is WorkInProgress !! # @@ -94,13 +102,52 @@ raise "you must implement this on the driver!!" end def send_print_file context context.send_file pdf_file_path, filename: 'oxen_file', type: get_file_type, disposition: "attachment" + # send_file path, type: asset_content_type, disposition: 'inline', filename: filename # context.cookies['fileDownload'] = 'true' # File.open(pdf_file_path, 'r') do |fp| # context.send_data fp.read.force_encoding('BINARY'), filename: 'oxen_file', type: "application/pdf", disposition: "attachment" # end + end + + def mail_print print_job, args + # move pdf_file_path to a static place where it will not be accidentally deleted from + # static_path = File.join(Rails.root,'public','files','1.pdf') + # var = %x[ mv #{pdf_file_path} #{static_path} 2>&1 ] + # send email to usr with link + # raise MovingFileFailedError.new(var) unless var.blank? + msg = args[:message] || "Der er ikke defineret nogen printere til dette job, for dig - så jeg er nødt til at sende dig filen vedhæftet denne email! ;)Du bør kontakte din system administrator og bede om at der bliver oprettet en printer definition til denne printer jobtype, for dig!" + if print_job.printed_by.nil? + raise PrintJobPrintingError.new t('action_printer.user_not_found') + else + PrinterMailer.send_print(print_job.printed_by,file_path,msg,t('print_delivered_by_email')).deliver_now + end + end + + def print_print prn, print_job, args + args[:copies] ||= 1 + cmd = build_print_cmd prn, args + if args[:copies].to_i > 1 + cmdargs = cmd.split(" ") + f = cmdargs.pop + cmdargs.push "-n", args[:copies].to_i, f + cmd = cmdargs.join(" ") + end + # Here we start actually printing + # $1 is the CUPS printer + # $2 is the PDF file_path_and_name + # $3 is the PDF filename + # the command being something along the lines of: lp -d $1 -o media=a4 $2 + return `#{cmd}` + end + + def build_print_cmd prn, args + cups = !prn.printserver ? prn.cups_printer : "#{prn.cups_printer} -h 10.0.0.26:4#{prn.printserver.port}" + pdf_path = pdf_file_path.split("/")[-1] + paper = args.delete(:paper) || prn.paper + prn.command.gsub( /\$1/, cups ).gsub( /\$2/, paper ).gsub( /\$3/, pdf_file_path ).gsub( /\$4/, pdf_path ) end def logit( log_type, msg ) Rails.logger.send(log_type, "[OXEN] #{Time.now} [#{log_type.to_s}] #{msg}") end