pdfkit not rendering images and css to the pdf - ruby-on-rails

I have generated a pdf using pdfkit but it is neither rendering images nor the css file. I don't know why this is happening. Any help will be appreciable.
Layout : (report.html.erb)
<!DOCTYPE html>
<html lang='en'>
<head>
<%= stylesheet_link_tag 'merchant_report', rel: 'stylesheet'%>
<%= csrf_meta_tags %>
<%= favicon_link_tag 'favicon.ico' %>
</head>
<body>
<%= yield %>
</body>
</html>
Action : (report.pdf.erb)
<div class="checkin_data">
<h4 style="text-align: center;">No. of Customers Vs. Date</h4>
<div>No. of Checkins during this period on a day wise basis</div>
<% image_name = Time.now.strftime("%d-%m-%Y").to_s + '_' + $merchant.id.to_s + '_check_in_visit_history_image.png' %>
<!-- <img src="http://127.0.0.1:3000/system/report/#{image_name}"/> -->
<%= image_tag("/assets/images/report/#{image_name}")%>
<br>
<h4 style="text-align: center;">No. of Customers Vs. No. of Visits</h4>
<div>No. of Visits per Customer during this period</div>
<canvas height="400" width="600" id="customer_visit"></canvas>
<br>
</div>
Controller : (merchant_report_controller)
respond_to do |format|
format.html
format.pdf do
html = render_to_string(:layout => "report.html.erb" , :action => "report.pdf.erb")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/public/system/stylesheets/merchant_report.css"
send_data(kit.to_pdf, :filename => "some_name.pdf", :type => 'application/pdf', :disposition => 'inline')
return
end
end
pdfkit.rb
PDFKit.configure do |config|
config.wkhtmltopdf = '/usr/bin/wkhtmltopdf'
config.default_options = {
:page_size => 'Legal',
:print_media_type => true,
:disable_javascript => true
}
# Use only if your external hostname is unavailable on the server.
config.root_url = "http://localhost:3000"
config.verbose = false
end

Related

Application layout not applied to form

In app/controllers/fw_exports_controller.rb, I have:
def import_spreadsheet
#followeds = Followed.all.order(:screen_name)
#import_spreadsheet = FwExport.new
render partial: 'fw_exports/import_spreadsheet'
end
In app/views/layouts/application.html.slim, I have:
doctype html
html
head
meta content=("text/html; charset=UTF-8") http-equiv="Content-Type" /
meta content="width=device-width, initial-scale=1.0" name="viewport" /
title= content_for?(:title) ? yield(:title) : t('project_name')
meta content=("#{content_for?(:description) ? yield(:description) : t('project_name')}") name="description" /
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag 'application', 'data-turbolinks-track' => true
= csrf_meta_tags
body
header
= render 'layouts/navigation'
main role="main"
= render 'layouts/messages'
= yield
= render "layouts/footer"
In app/views/layouts/_navigation.html.slim, I have:
nav.navbar.navbar-default role="navigation"
.container
.navbar-header
a.navbar-brand href="#"
= t('parnassus_tools_home')
.navbar-left
ul.nav.navbar-nav
li
a
= link_to t('menus.upload_file'), import_spreadsheet_path
In app/views/fw_exports/_import_spreadsheet.html.slim, I have:
.container
h1
= t('fw_exports.choose_file_to_upload')
= form_tag process_imported_spreadsheet_path, html: { multipart: true }
.form-group
= file_field_tag :import_file
- if !#followeds[0].nil?
.form-group
= select_tag "followed_id", options_from_collection_for_select(#followeds, "id", "screen_name", "1")
.form_group
.sr-only
= label_tag t('fw_exports.screen_name_to_add')
= text_field_tag :screen_name_addition, placeholder: t('fw_exports.screen_name_to_add')
= button_tag(type: 'submit', class: "btn btn-primary") do
i.icon-ok.icon-white
= t('fw_exports.import')
In app/views/landings/index.html.slim, I have (this is the home page):
h1 Home Page
p test text
Here's what I see when I am on the home page:
But when I click on the Upload File menu item, from the home page, here's what I see:
Why isn't the application layout applied to the form?
You will need to render as a template and not as a partial. First, create a view template:
app/views/fw_spreadsheet/import_spreadsheet.html.slim
= render 'fw_exports/import_spreadsheet_form'
Next, rename your partial by changing the name of app/views/fw_spreadsheet/_import_spreadsheet.html.slim to app/views/fw_spreadsheet/_import_spreadsheet_form.html.slim
Finally, modify your controller as follows:
app/controllers/fw_exports_controller.rb
def import_spreadsheet
#followeds = Followed.all.order(:screen_name)
#import_spreadsheet = FwExport.new
render 'fw_exports/import_spreadsheet'
end

Extra characters rendered in view that cannot be found! A possible bug in rendering engine?

I am developing an application using rails 3.2.0 and ruby 1.9 on a Mac
I have a very strange error when rendering an index view
Rails is rendering the characters us at the bottom of the screen, below an index table, and I cannot find these characters in the view.
This part of the view looks like
<section id="main">
<section id="group"></section>
<section id="content">
<section id="data_table_section">
<script>
us
</section>
</section>
The part from id=content is yield in a layout file
<section id='content'>
<%= yield %>
</section>
When I delete the content of the view template, i.e the the us is still there
When I delete <%= yield %> in the layout, the us disappears
When I search for the us in the view code it is not found
When I add my own extra characters in the bottom of the view template code, after tags the us is displayed after these characters
When I delete the layout template the us is still there
The only thing I can come up with is that us is generated by the yield function in some mysterious way, but that seems as a very strange explanation!
Anyone that has had this problem before?
Anyone that know how to find extra characters as us in the code ?
Could it be a bug in the rendering engine ?
Any advices would be great appreciated
Here follows my view code. I use tableastic gem and some other gems
<% if !#unit.nil?
header_text="Deltagarlista för #{#unit.class.model_name.human} #{#unit.name}"
else header_text='Deltagarlista för ST-Forum'
end %>
<section id='data_table_section'>
<article id='remote_clinic_article'></article>
<article id="users_article">
<%= table_for(#users) do |t| %>
<thead ><tr ><th id='table_header' colspan=17><%=header_text%></th></tr></thead>
<thead><tr style='text-align:center;' ><th colspan=15><%= render :partial=>'users/filter'%></th></tr></thead>
<% index=(params[:page].to_i-1)*#per_page%>
<%= t.data do
t.cell(:id, :heading => "Id") {|p| index+=1}
t.cell(:portrait, :heading => "Foto") {|p| image_tag(p.portrait_image,:height=>'24px')}
t.cell(:name,:heading => sort_to("Namn",users_url(:sort_field=>'surname', :sort=>#sort),#sort_field)) {|p| link_to(mark_search_hits(p.name,#search),user_path(p.id))}
t.cell(:clinic, :heading => sort_to("Arbetsplats",users_url(:sort_field=>'clinics.name', :sort=>#sort),#sort_field)) {|p| link_to(mark_search_hits(p.clinic.name,#search),clinic_path(p.clinic.id)) unless p.clinic.nil?}
t.cell(:email,:cell_html => {:class => "address"},
:heading => sort_to("Email",users_url(:sort_field=>'email', :sort=>#sort),#sort_field)) {|p| mail_to(mark_search_hits(truncate(p.email,:length =>20),#search))}
t.cell(:user_roles,:cell_html => {:style => "width:50px"},
:heading => sort_to("Roller",users_url(:sort_field=>'user_roles.role_index', :sort=>#sort),#sort_field)) {|p| mark_search_hits(p.roles(true).to_sentence,#search)}
t.cell(:groups, :heading => sort_to("Grupper",users_url(:sort_field=>'groups.name', :sort=>#sort),#sort_field)) {|p| mark_search_hits(to_sentence(p.groups),#search)}
t.cell(:forum, :heading => sort_to("ST-forum",users_url(:sort_field=>'forums.name', :sort=>#sort),#sort_field)) {|p| mark_search_hits(link_to(p.forum.name,clinic_path(p.forum.id)),#search) unless p.forum.nil?}
t.cell(:st_starts_on, :heading => sort_to("ST-start -- slut",users_url(:sort_field=>'employments.st_starts_on', :sort=>#sort),#sort_field)){|p| mark_search_hits(between_user_dates(p.employment.st_starts_on,p.employment.st_end_on),#search) unless p.employment.nil?}
t.cell(:legitimation_on,:heading => sort_to("Legitimation",users_url(:sort_field=>'employments.legitimation_on', :sort=>#sort),#sort_field)){|p| mark_search_hits(to_user_date(p.employment.legitimation_on),#search) unless p.employment.nil?}
t.cell(:employed_on,
:heading => sort_to("ST-kontrakt",users_url(:sort_field=>'employments.employed_on', :sort=>#sort),#sort_field)){|p| mark_search_hits(to_user_date(p.employment.employed_on),#search) unless p.employment.nil?}
t.cell(:last_visit,
:heading => sort_to("Inloggad senast",users_url(:sort_field=>'last_visit_at', :sort=>#sort),#sort_field)){|p| mark_search_hits(to_user_date(p.last_visit_at),#search) }
t.cell(:mail,
:heading => "Handledare / Handledd") {|p| if !p.supervisors.blank? then mail_supervisors(p) elsif !p.supervised.blank? then mail_supervised(p) end}
t.cell(:id, :cell_html => {:style => "width:30px"},:heading=>image_to('new.png',new_user_path,:class=>'no_class')) {|p| (image_to('destroy.png',user_path(p),:class=>'none',:method=>'delete', :id=>'destroy_button', :confirm => "Vill du verkligen radera vald kurs ",:title=>'Radera kurs')+' '+image_to('map.png', map_address_path(p.address.id),:method=>:get,:class=>'none', :remote=>true, :title=>'Visa en karta över bostadsområdet')).html_safe}
end%>
<tfoot>
<tr >
<td colspan="16" class='flickr_pagination'><%= will_paginate #users, :container => true %><span class="table_filter_text">
<% if #count_users==0 %>
<span class="table_filter_alert_text">
<%=" Inga användare tillgänglig för #{put_filter(#filterparams)}".html_safe%>
</span>
<%else%>
<span class="table_footer_text">
<%="Visar användare "+((params[:page].to_i-1)*#per_page+1).to_s+" till "+([(params[:page].to_i-1)*#per_page+#per_page,#count_users].min).to_s+" av #{#count_users.to_s} användare" .html_safe%>
<br/><%= "Med #{ put_filter(#filterparams)}".html_safe %>
</span>
<%end%>
</td>
</tr>
</tfoot>
<% end %>
</article>
</section>
<script>
$(document).ready(function() {
/* $('.pagination a').attr('data-remote', 'true');*/
jQuery(".best_in_place").best_in_place();
});
// Observe forum_field and filter group_options
$(document).ready(function(){
$("#forum_id").live('change',function () {
var forum = "";
forum=$("select#forum_id :selected").val()
if (forum=='') {forum=0}
jQuery.get('/users/'+forum+'/update_group_options', function(data){
$("#group_div").html(data);
})
return false;
})
.change();
$("#county_council_id").live('change',function () {
var county_council = "";
county_council=$("select#county_council_id :selected").val()
if (county_council=='') {county_council=0}
jQuery.get('/users/'+county_council+'/update_forum_options', function(data){
$("#forum_div").html(data);
})
return false;
})
.change();
});
</script>
I think the "us" is written by js.
May be you can double check the js.

Mandrill API Templating

I'm using Mandrill's Ruby API Gem and have the follow simple template for testing:
<html>
<body>
<h1 mc:edit="header">testastic</h1>
<hr/>
<br/><br/>
<div mc:edit="main_section"></div>
<hr/>
<div mc:edit="footer"></div>
</body>
</html>
Following the example on Heroku's guide I have the follow Ruby code:
require 'mandrill'
m = Mandrill::API.new
rendered = m.templates.render 'test-template', [{:header => 'some header text', :main_section => 'The main content block', :footer => '<h3>asdf</h3>'}]
mail(:to => "Jayson Lane <jayson#domain.com>", :subject => "Test Email") do |format|
format.html { rendered['html'] }
#format.text { render "test" }
end
This works great and the email sends my template just fine, however, it doesn't replace the template mc:edit variables. Am I missing something?
You need to construct a hash for each element you're trying to replace. For instance, I have this inside of a template:
<h3 mc:edit="plan_info_name"> </h3>
<span mc:edit="plan_info_description"> </span>
<span mc:edit="plan_info_benefits"> </span>
And this on the mailer:
mandrill.messages.send_template(template,[
{
:name => 'plan_info_name',
:content => extra[:membership_info].name
},
{
:name => 'plan_info_description',
:content => extra[:membership_info].long_description
},
{
:name => 'plan_info_benefits',
:content => benefits_list
}
....

jQuery File Upload v4 for Rails 3, Routing Error

I was following this tutorial and ran into difficulties.
I get the Routing Error by calling http://localhost:3000/home
Maybe I am just calling the wrong page.
I would grateful if somebody helps me.
Hier is my routes.rb:
Try::Application.routes.draw do
resources :uploads
root :to => "home#index"
end
uploads_controller.rb:
class UploadsController < ApplicationController
def home
end
def index
end
def create
#upload = Upload.new(params[:upload])
if #upload.save
render :json => { :pic_path => #upload.picture.url.to_s , :name => #upload.picture.instance.attributes["picture_file_name"] }, :content_type => 'text/html'
else
render :json => { :result => 'error'}, :content_type => 'text/html'
end
end
end
and views/home/index.html.erb:
<%= stylesheet_link_tag('jquery.fileupload-ui') %>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<%= javascript_include_tag 'jquery.fileupload', 'jquery.fileupload-ui' %>
<script type="text/javascript" charset="utf-8">
$(function () {
$('.upload').fileUploadUI({
uploadTable: $('.upload_files'),
downloadTable: $('.download_files'),
buildUploadRow: function (files, index) {
var file = files[index];
return $('<tr><td>' + file.name + '<\/td>' +
'<td class="file_upload_progress"><div><\/div><\/td>' +
'<td class="file_upload_cancel">' +
'<button class="ui-state-default ui-corner-all" title="Cancel">' +
'<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
'<\/button><\/td><\/tr>');
},
buildDownloadRow: function (file) {
return $('<tr><td><img alt="Photo" width="40" height="40" src="' + file.pic_path + '">' + file.name + '<\/td><\/tr>');
},
});
});
</script>
<div class="files">
<%= form_for #upload, :html => { :class => "upload", :multipart => true } do |f| %>
<%= f.file_field :picture %>
<div>Upload files</div>
<% end %>
<table class="upload_files"></table>
<table class="download_files"></table>
</div>
home_controller.rb:
class HomeController < ApplicationController
def index
#upload = Upload.new
end
end
Your routes file is saying that the root path i.e. '/' is going to home#index.
There is no route for '/home' Suggest you add:
match '/home' => 'uploads#home'
when you say root :to => "home#index" in routes file it means home controller index action
also your view file structure needs to be modifies to views/controller_name/index.html.erb:

Google Maps For Rails replaceMarkers not working

Am I missing something obvious? Map keeps loading #oldjson which I set in the controller and won't be replaced when I set it in the view.
View code:
<% #json = Map.find_by_id('39').to_gmaps4rails %>
<%= gmaps("markers" => {"data" => #oldjson, "options" => { "draggable" => true } } ) %>
<script>
Gmaps.map.replaceMarkers(<%= #json %>);
</script>
Thanks.
I guess you"re facing a js error with this current code.
The reason is the following:
the js used and created by gmaps4rails is put within the yield :scripts
so your additionnal js here is called before the map is created
Solution:
<% #json = Map.find_by_id('39').to_gmaps4rails %>
<%= gmaps("markers" => {"data" => #oldjson, "options" => { "draggable" => true } } ) %>
<% content_for :scripts do %>
<script>
Gmaps.map.replaceMarkers(<%= #json %>);
</script>
<% end %>

Resources