瀏覽代碼

TESTING pdf.js printing in cash sale

Jose Miguel Ledon Nieblas 8 年之前
父節點
當前提交
4a84105058

+ 5 - 5
Gemfile

@@ -1,7 +1,7 @@
+# rubocop:disable Bundler/OrderedGems
 # frozen_string_literal: true
 source 'https://rubygems.org'
 
-
 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '4.2.4'
 # Use PostgreSQL as the database for Active Record
@@ -17,7 +17,6 @@ gem 'cancan'
 # Activity user logs / auditabled
 gem 'audited-activerecord', '~> 4.0'
 
-
 # Use SCSS for stylesheets
 gem 'sass-rails', '~> 5.0'
 # Use Uglifier as compressor for JavaScript assets
@@ -43,7 +42,7 @@ gem 'nokogiri'
 
 ### javascripts y css
 # Use jquery as the JavaScript library
-gem 'sprockets-rails', :require => 'sprockets/railtie'
+gem 'sprockets-rails', require: 'sprockets/railtie'
 gem 'jquery-rails'
 gem 'jquery-ui-rails'
 gem 'bootstrap-sass'
@@ -76,6 +75,8 @@ gem 'will_paginate'
 gem "font-awesome-rails"
 gem 'carrierwave', '>= 1.0.0.rc', '< 2.0'
 gem 'mini_magick'
+gem 'pdfjs_viewer-rails'
+
 # gem 'ajax-datatables-rails', git: 'git://github.com/antillas21/ajax-datatables-rails.git', branch: 'master'
 # gem 'amcharts.rb'
 # Use ActiveModel has_secure_password
@@ -101,9 +102,8 @@ end
 group :development do
   # Access an IRB console on exception pages or by using <%= console %> in views
   gem 'web-console', '~> 2.0'
-
   # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
   gem 'spring'
   gem 'pry-rails'
 end
-
+# rubocop:enable Bundler/OrderedGems

+ 10 - 4
app/controllers/sales_controller.rb

@@ -295,12 +295,18 @@ class SalesController < ApplicationController
   end
 
   def print_receipt
-    # ticket para la venta
     respond_to do |format|
-      sale = Sale.find(params[:sale_id])
-      format.pdf do
-        render pdf: "ticket_venta_#{sale.id}", template: "sales/receipt.pdf.erb", layout: 'receipt.html.erb', locals: { sale: sale }, show_as_html: params.key?('debug'), page_width: '80mm', page_height: '300mm'
+      @sale = Sale.find(params[:sale_id])
+
+      unless File.exist?(Rails.public_path.join('pdfs', "ticket_venta_#{@sale.id}.pdf"))
+        pdf = render_to_string pdf: "ticket_venta_#{@sale.id}", template: "sales/receipt.pdf.erb", layout: 'receipt.html.erb', locals: { sale: @sale }, show_as_html: params.key?('debug'), page_width: '80mm', page_height: '300mm'
+        save_path = Rails.public_path.join('pdfs', "ticket_venta_#{@sale.id}.pdf")
+
+        File.open(save_path, 'wb') do |file|
+          file << pdf
+        end
       end
+      format.js
     end
   end
 

+ 5 - 2
app/views/cash_registers_moves/confirm_payments.js.erb

@@ -1,8 +1,11 @@
 <% if @sale.present? %>
 	window.location = "<%= new_sale_path %>";
 	$("#notice").html("<%= flash[:notice] %>");
-	var ticket_window = window.open("<%= print_receipt_path(@sale.id, format: 'pdf') %>", 'New tab', '' );
-	ticket_window.print();
+  $.ajax({
+    type: "get",
+    url: '<%= print_receipt_path(@sale.id) %>',
+    dataType: 'script'
+  });
 
 <% elsif @products_return.present? %>
 	window.location = "<%= products_returns_path %>";

+ 1 - 1
app/views/sales/index.html.erb

@@ -144,7 +144,7 @@
                             <i class="fa fa-search"></i>
                           <% end %>
 
-                          <%= link_to print_receipt_path(sale.id, format: 'pdf'), {:class=>"btn btn-icon-only default", :target => "blank"} do %>
+                          <%= link_to print_receipt_path(sale_id: sale.id), remote: true, class:"btn btn-icon-only default", target: "blank", :title=>"Imprimir ticket" do %>
                             <i class="fa fa-print"></i>
                           <% end %>
 

+ 24 - 0
app/views/sales/print_receipt.js.erb

@@ -0,0 +1,24 @@
+var ticket_window = window.open('<%= pdfjs.reduced_path(file: "ticket_venta_#{@sale.id}.pdf") %>', '_blank', '');
+
+// ticket_window.onload = function() {
+//   ticket_window.print();
+// };
+
+
+// function ready(fn) {
+//   if (document.readyState != 'loading'){
+//     fn();
+//   } else if (document.addEventListener) {
+//     document.addEventListener('DOMContentLoaded', fn);
+//   } else {
+//     document.attachEvent('onreadystatechange', function() {
+//       if (document.readyState != 'loading')
+//         fn();
+//     });
+//   }
+// }
+
+
+// ready(function() {
+//   ticket_window.print();
+// });

+ 2 - 0
config/routes.rb

@@ -14,6 +14,8 @@ Rails.application.routes.draw do
     sign_up: 'cmon_let_me_in'
   }
 
+  mount PdfjsViewer::Rails::Engine => "/pdfs", as: 'pdfjs'
+
   devise_scope :user do
     get "login", to: "devise/sessions#new"
     get "logout", to: "devise/sessions#destroy"