Wicked PDF
1. Add this to your Gemfile and run
bundle install
: gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
2. Then create the initializer with
rails generate wicked_pdf
3.
You may also need to add (optional)
Mime::Type.register "application/pdf", :pdf
to config/initializers/mime_types.rb
in older versions of Rails.
4. Create a PDF link
<%= link_to 'Create PDF', order_pdf_path(format: :pdf), class: "btn btn-info btn-sm float-right",
data: {toggle: "tooltip", title: "View PDF Agreement"}%>
5. Then update a code in orders_controller.rb file
def order_pdf
@orders = current_user.orders
respond_to do |format|
format.html
format.pdf do
render pdf: "order_pdf", # Excluding ".pdf" extension.
show_as_html: params.key?("debug"), # allow debugging based on url param
disable_smart_shrinking: true,
zoom: 0.75
end
end
end
6. Then create a file with Pdf extendion in orders_view_folder(order_pdf.pdf.erb)
<html>
<head>
<title>PDF</title>
<%= wicked_pdf_stylesheet_link_tag "application" %>
<%= stylesheet_link_tag "http://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"%>
<%= javascript_include_tag "http://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"%>
</head>
<body>
<p id="notice"><%= notice %></p>
<h4 class="order_header text-center">Orders History PDF</h4><br>
<div class="container">
<div class="row">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Product Name</th>
<th>Date&Time</th>
<th>Address</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
<th>Image</th>
<th>Order Rating</th>
</tr>
</thead>
<tbody>
<% if current_user.orders.present? %>
<% @orders.each do |order| %>
<tr style="background-color: cadetblue;">
<th>ORDER ID: <%= order.id %> (<%= order.user.name %>)</th>
</tr>
<% order.cart.cart_items.each do |item| %>
<% product_id = item.product_id
product = Product.unscoped.find(product_id) %>
<% if product.present? %>
<tr>
<td><%= product.name rescue nil %></td>
<td><%= Time.now.strftime "%a, %-d %b %Y %I:%M %p" %></td>
<% address_id = order.address_id %>
<% @add = Address.find_by(id: address_id)%>
<% if @add.present? %>
<td><%= @add.address %>, <%= @add.country%>, <%= @add. post_code%>, <%= @add. district%> </td>
<% end %>
<td><%= item.quantity rescue nil %></td>
<td><%= product.price || 0.0 rescue nil %></td>
<td><%= item.quantity * product.price || 0.0 rescue nil %></td>
<td><%= image_tag product.image.url(:thumb) rescue nil %></td>
<% if item.product.present? %>
<td>
<div class="btn btn-warning"><a href="/order_review/<%= product.id %>">Review</a></div>
</td>
<% end %>
</tr>
<% end %>
<% end %>
<tr>
<th></th>
<th></th>
<th></th>
<th><div class="p-2 mb-2 bg-success text-white float-left" style="margin-left: 10%" ><%= order.cart.quantity %></div></th>
<th></th>
<th><div class="p-2 mb-2 bg-success text-white float-left" style="margin-left: 10%" ><%= order.total %></div></th>
</tr>
<% end %>
<% end %>
</tbody>
</table><br><br>
</div>
</div><br>
<%= yield %>
<!-- </div> -->
</body>
</html>
Links :-1. https://medium.com/@yazed.jamal/using-wicked-pdf-with-rails-5-3d1a4b0a09ba
2. https://github.com/mileszs/wicked_pdf
No comments:
Post a Comment