Class: PdfPrinter

Inherits:
ActionPrinter show all
Defined in:
lib/oxen_printer/pdf_printer.rb

Instance Attribute Summary

Attributes inherited from ActionPrinter

#paper, #printer

Instance Method Summary collapse

Methods inherited from ActionPrinter

#command, #html_file, #initialize, #label_file_path, #logit, #pdf_file_path, #send_print_file, #text_file

Constructor Details

This class inherits a constructor from ActionPrinter

Instance Method Details

#do_print(copies) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/oxen_printer/pdf_printer.rb', line 32

def do_print copies
  result = nil
  prn = printer
  unless prn.command.blank?
    pdf_path =  pdf_file_path.split("/")[-1]
    pap = paper || prn.paper
    cmd = prn.command.gsub( /\$1/, prn.cups_printer ).gsub( /\$2/, pap ).gsub( /\$3/, pdf_file_path ).gsub( /\$4/, pdf_path )
    if copies > 1
      cmdargs = cmd.split(" ")
      f = cmdargs.pop
      cmdargs.push "-n", copies, 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
rescue
  false
end

#do_render(print_job, *args) ⇒ Object

IKKE FÆRDIG



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/oxen_printer/pdf_printer.rb', line 3

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')
  locals = { resource_class: print_job.printing_class.constantize, collection: print_job.printing_class.constantize.find_by_sql( print_job.print_sql)  }
  #
  # TODO args must be mergeable ;)
  # if args.flatten.compact.any?
  #   locals.merge! args
  # end
    # html = ActionView::Base.new(Rails.configuration.paths['app/views']).render(
    #   file: 'accounts/record.html.haml',
    #   # partial: 'test',
    #   formats: [:html],
    #   handlers: [:erb,:haml],
    #   locals: { variable: 'value' }
    # )
    #
  of = html_file render( file: path, formats: [:html], handlers: [:haml,:erb], locals: locals)
  logit :info, "created a html file: #{of.path}"
  #
  # calls java -jar with the oxen_printer gem lib/java_pdf/barcodeprinter.jar - as in java -jar /Users/walther/Projects/Gems/oxen_printer/lib/java_pdf/barcodeprinter.jar $1 $2
  var = %x[ bin/printer_cmd.sh #{of.path} #{pdf_file_path}  ]
  logit :info, "converted the html file to a PDF file: #{pdf_file_path}"
  true
rescue => e
  logit :error, "Rendering to PDF failed! The error was #{e.message}"
  false
end

#file_pathObject



56
57
58
# File 'lib/oxen_printer/pdf_printer.rb', line 56

def file_path
  pdf_file_path
end

#get_file_typeObject



60
61
62
# File 'lib/oxen_printer/pdf_printer.rb', line 60

def get_file_type
  "application/pdf"
end