Рельсы печати чашки - PullRequest
       8

Рельсы печати чашки

0 голосов
/ 25 февраля 2019

В моем приложении rails я печатаю квитанцию, используя чашки с гемом cupsffi

Это задание, которое я установил:

class PrintReceiptJob < ActiveJob::Base
  require 'cupsffi'
  printers = CupsPrinter.get_all_printer_names
  printer = CupsPrinter.new(printers.first)
  job = printer.print_file(Rails.root.join('receipt.pdf').to_s, {'PageSize' => 'A4'})

end

В моем контроллере я генерируюквитанцию ​​в pdf с креветкой и вызовом задания выше:

pdf = ReceiptPdf.new(@receipt)
pdf.render_file "receipt.pdf"
PrintReceiptJob.perform_now

И все работает, квитанция распечатывается.

Проблема в том, что после печати я получаю эту ошибку:

[ActiveJob] [PrintReceiptJob] [044c6b97-daca-445d-9c87-631e3de3d7cd] Performing PrintReceiptJob (Job ID: 044c6b97-daca-445d-9c87-631e3de3d7cd) from Async(default)
[ActiveJob] [PrintReceiptJob] [044c6b97-daca-445d-9c87-631e3de3d7cd] Error performing PrintReceiptJob (Job ID: 044c6b97-daca-445d-9c87-631e3de3d7cd) from Async(default) in 0.52ms: NotImplementedError (NotImplementedError):

Насколько я понимаю, он пытается выполнить работу дважды, а во второй раз не удается.

application.rb

require_relative 'boot'
require 'apartment/elevators/subdomain'
require 'rails/all'

require File.expand_path('../boot', __FILE__)
ENV['RANSACK_FORM_BUILDER'] = '::SimpleForm::FormBuilder'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module CompanyManagement
  class Application < Rails::Application

    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2
    config.time_zone = "Singapore"
  end
end

Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
#gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'devise'
gem 'bootstrap'
gem 'jquery-ui-rails'
gem 'jquery-rails'
gem 'simple_form'
gem "font-awesome-rails"
gem "cocoon"
gem "select2-rails"
gem 'will_paginate'
gem 'will_paginate-bootstrap4'
gem "chartkick"
gem 'groupdate'
gem 'trix-rails', require: 'trix'
gem 'amoeba'
gem "simple_calendar", "~> 2.0"
gem 'apartment'
gem 'protokoll'
gem 'ransack'
gem "time_splitter"
gem 'working_hours'
gem 'jquery-minicolors-rails'
gem 'carrierwave', '~> 1.0'
gem 'prawn-rails'
gem 'cupsffi'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'pry'

end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15', '< 4.0'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Чего мне не хватает?Как я могу исправить?

1 Ответ

0 голосов
/ 25 февраля 2019

Вы забыли обернуть свой код методом:

require 'cupsffi'

class PrintReceiptJob < ActiveJob::Base
  def perform
    printers = CupsPrinter.get_all_printer_names
    printer = CupsPrinter.new(printers.first)
    job = printer.print_file(Rails.root.join('receipt.pdf').to_s, {'PageSize' => 'A4'})
  end    
end
...