WickedPdf stopped working on my local system - ruby

I'm getting this error while generating pdf using wkhtmltopdf
undefined method `pdf_from_string' for #<WickedPdf:0x7f4b82a369c8>
my wicked_pdf.rb
WickedPdf.config = {
:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf',
:layout => "pdf.html",
:margin => { :top=> 40,
:bottom => 20,
:left=> 30,
:right => 30},
:header => {:html => { :template=> 'layouts/pdf_header.html'}},
:footer => {:html => { :template=> 'layouts/pdf_footer.html'}}
# :exe_path => '/usr/bin/wkhtmltopdf'}
on command line
wkhtmltopdf google.com google.pdf
is working fine.

"pdf_from_string" means, that it makes pdf from STRING.
So to make this method work it should recieve string.
<WickedPdf:0x7f4b82a369c8> - it is an object.
It should look like this:
pdf_from_string("<p>some html code</p>")

You will get this message when calling pdf_from_string on the class itself, instead of an instance.
WickedPdf.pdf_from_string('<p>some html code</p>')
Will not work, however:
WickedPdf.new.pdf_from_string('<p>some html code</>')
will, because new returns an instance, which you could then call pdf_from_string on.
This is the same as this:
pdf_generator = WickedPdf.new
pdf = pdf_generator.pdf_from_string('<p>some html code</p>')

Related

convert embedded ruby file to PDF file instead of only HTML file using Rails 3

I want to convert my embedded ruby file to PDF file after clicking on a link using Rails 3.I became able to convert simple html file to PDF file using pdfkit gem.I am explaining my code below.
users_controller.rb:
class UsersController < ApplicationController
def index
end
def download_pdf
#html = render_to_string(:action => "/users/download_pdf.html.erb")
#kit = PDFKit.new('http://google.com')
#kit = PDFKit.new(html)
#send_data(kit.to_pdf, :filename => 'report.pdf', :type => 'application/pdf', :disposition => 'inline')
kit = PDFKit.new("<h1>Hello</h1><p>This is PDF!!!</p>", :page_size => "A4")
send_data(kit.to_pdf, :filename => 'report.pdf', :type => 'application/pdf', :disposition => 'inline')
#file = kit.to_file('my_file_name.pdf')
end
end
In this controller page i did and got success to convert from HTML to PDF.
users/index.html.erb:
<p>
<%= link_to "Download pdf",download_pdf_path(:format => 'pdf') %>
</p>
When user will click on the above "download_pdf" link the download.html.erb will convert to PDF file and it should display as well as download in specified folder.The download.html.erb file is given below.
users/download.html.erb:
<h1>Hello Rails</h1>
The above file should convert into PDF file with proper css .If i have css for this like below.
application.css:
h1{
width:100px;
height:100px;
background-color:red;
}
How can i include this CSS in that PDF file.My other files are given below.
pdfkit.rb:
PDFKit.configure do |config|
#config.wkhtmltopdf =Rails.root.join('bin', 'wkhtmltopdf-i386').to_s
config.wkhtmltopdf='C:/wkhtmltopdf/bin/wkhtmltopdf.exe'
#config.default_options[:ignore_load_errors] = true
end
Please help me to resolve this issue and make this successfully.
By using wicked_pdf gem i am getting the following error.
error:
RuntimeError in UsersController#download_pdf
Error: Failed to execute:
["C:/wkhtmltopdf/bin/wkhtmltopdf.exe", "file://C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf20150527-3204-calx6j.html", "C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf_generated_file20150527-3204-59mbli.pdf"]
Error: PDF could not be generated!
Command Error: Loading pages (1/6)
[> ] 0%
[======> ] 10%
Error: Failed loading page file://c/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf20150527-3204-calx6j.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: ContentNotFoundError
check the below code for this gem.
users/users_controller.rb:
class UsersController < ApplicationController
def index
end
def download_pdf
render pdf: 'test',
layout: '/layouts/test',
template: '/users/test',
handlers: [:erb],
formats: [:pdf],
:save_to_file => Rails.root.join('public', "test.pdf")
end
end
wicked_pdf.rb:
WickedPdf.config = {
#:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf',
#:layout => "pdf.html",
:exe_path => 'C:/wkhtmltopdf/bin/wkhtmltopdf.exe'
}
You can include your stylesheets by following:
def download_pdf
kit = PDFKit.new(File.open(Rails.root.join('app', 'views', 'users', 'download.html.erb')))
kit.stylesheets << Rails.root.join("app","assets","application.css")
send_data(kit.to_pdf, :filename => 'report.pdf', :type => 'application/pdf', :disposition => 'inline')
end

Using model attribute as :filename when using paperclip gem

I'm getting an error when attempting to change the :filename of my paperclip attachment to equal an attribute on the class I'm attaching the paperclip file to.
When I use "#{self.company_name}" it errors out. Apparently in this scope, "self" is not Company. When I wrote this line I assumed that self is the instance of Company that I'm uploading this attachment to. Any idea how I can fix this? The Paperclip docs say to use ":filename" but I'd like to use the value of Company.company_name instead.
class Company < ActiveRecord::Base
include AliasAttrs
has_attached_file :company_logo, {
:storage => :ftp,
:path => "/logos/#{self.company_name}",
:url => FTP_CONFIG[:access_host]+"logos/:filename",
:ftp_servers => [
{
:host => FTP_CONFIG[:host],
:user => FTP_CONFIG[:user],
:password => FTP_CONFIG[:pass],
:port => 21 # optional, 21 by default
}
]
}
end
Update
I tried using the advice found in this post: https://robots.thoughtbot.com/paperclip-tips-and-updates
But now I am getting the following error when starting my server:
undefined method `interpolations' for Paperclip::Attachment:Class (NoMethodError)
It looks like the syntax for interpolations has changed. Updated it and it worked. Add the following to your model or create a paperclip.rb file in config/initializers
Paperclip.interpolates :company_name do |attachment, style|
attachment.instance.company_name
end

How to update all fields in MailChimp API batch subscribe using Ruby and Gibbon

I am using Ruby 1.9.3 without Rails and version 1.0.4 of the Gibbon gem.
I have referrals populated with my list and can send the following to MailChimp with Gibbon. However, only the email address and email type fields are populated in the list in MailChimp. What am I doing wrong that is prohibiting all the merge fields from being imported via API?
Here is the batch and map of the list.
referrals.each_slice(3) do |batch|
begin
prepared_batch = batch.map do |referral|
{
:EMAIL => {:email => referral['client_email']},
:EMAIL_TYPE => 'html',
:MMERGE6 => referral['field_1'],
:MMERGE7 => referral['field_2'],
:MMERGE8 => referral['field_3'],
:MMERGE9 => referral['field_4'],
:MMERGE11 => referral['field_5'],
:MMERGE12 => referral['field_6'],
:MMERGE13 => referral['field_7'],
:MMERGE14 => referral['field_8'],
:MMERGE15 => referral['field_9'],
:FNAME => referral['client_first_name']
}
end
#log.info("prepared_batch : #{prepared_batch}")
result = #gibbon.lists.batch_subscribe(
:id => #mc_list_id,
:batch => prepared_batch,
:double_optin => false,
:update_existing => true
)
#log.info("#{result}")
rescue Exception => e
#log.warn("Unable to load batch into mailchimp because #{e.message}")
end
end
The above executes successfully. However, only the email address and email type are populated but most of the fields should be populated.
Here is my log output for one of the prepared_batches. I replaced the real values with Value. I used my own email for testing.
I, [2013-11-11T09:01:14.778907 #70827] INFO -- : prepared_batch : [{:EMAIL=>
{:email=>"jason+6#marketingscience.co"}, :EMAIL_TYPE=>"html", :MMERGE6=>"Value",
:MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/6/13 0:00",
:MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>"10/18/13 19:09", :MMERGE15=>"Value",
:FNAME=>"Value"}, {:EMAIL=>{:email=>"jason+7#marketingscience.co"}, :EMAIL_TYPE=>"html",
:MMERGE6=>"Value", :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/6/13
0:00", :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>nil, :MMERGE15=>"Value",
:FNAME=>"Value"}, {:EMAIL=>{:email=>"jason+8#marketingscience.co"}, :EMAIL_TYPE=>"html",
:MMERGE6=>"Value", :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/7/13
0:00", :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>nil, :MMERGE15=>"Value",
:FNAME=>"Value"}]
Here is the log output of result from the MailChimp call.
I, [2013-11-11T09:01:14.778691 #70827] INFO -- : {"add_count"=>3, "adds"=>
[{"email"=>"jason+3#marketingscience.co", "euid"=>"ab512177b4", "leid"=>"54637465"},
{"email"=>"jason+4#marketingscience.co", "euid"=>"eeb8388524", "leid"=>"54637469"},
{"email"=>"jason+5#marketingscience.co", "euid"=>"7dbc84cb75", "leid"=>"54637473"}],
"update_count"=>0, "updates"=>[], "error_count"=>0, "errors"=>[]}
Any advice on how to get all the fields to update in MailChimp is appreciated. Thanks.
Turns out the documentation for using the Gibbon gem to batch subscribe is not correct. You need to add the :merge_vars struct to contain the fields other than email and email type. My final code looks like the following. I'm also going to update this code in its entirety at: https://gist.github.com/analyticsPierce/7434085.
referrals.each_slice(3) do |batch|
begin
prepared_batch = batch.map do |referral|
{
:EMAIL => {:email => referral['email']},
:EMAIL_TYPE => 'html',
:merge_vars => {
:MMERGE6 => referral['field_1'],
:MMERGE7 => referral['field_2'],
:MMERGE8 => referral['field_3'],
:MMERGE9 => referral['field_4'],
:MMERGE11 => referral['field_5'],
:MMERGE12 => referral['field_6'],
:MMERGE13 => referral['field_7'],
:MMERGE14 => referral['field_8'],
:MMERGE15 => referral['field_9'],
:FNAME => referral['first_name']
}
}
end
#log.info("prepared_batch : #{prepared_batch}")
result = #gibbon.lists.batch_subscribe(
:id => #mc_list_id,
:batch => prepared_batch,
:double_optin => false,
:update_existing => true
)
#log.info("#{result}")
rescue Exception => e
#log.warn("Unable to load batch into mailchimp because #{e.message}")
end
end

How can I upload files to Redmine via ActiveResource / REST API?

I am trying to batch-upload images to Redmine and link them each to a certain wiki pages.
The docs (Rest_api, Using the REST API with Ruby) mention some aspects, but the examples fail in various ways. I also tried to derive ideas from the source - without success.
Can anyone provide a short example that shows how to upload and link an image from within Ruby?
This is a bit tricky as both attachments and wiki APIs are relatively new, but I have done something similar in the past. Here is a minimal working example using rest-client:
require 'rest_client'
require 'json'
key = '5daf2e447336bad7ed3993a6ebde8310ffa263bf'
upload_url = "http://localhost:3000/uploads.json?key=#{key}"
wiki_url = "http://localhost:3000/projects/some_project/wiki/some_wiki.json?key=#{key}"
img = File.new('/some/image.png')
# First we upload the image to get attachment token
response = RestClient.post(upload_url, img, {
:multipart => true,
:content_type => 'application/octet-stream'
})
token = JSON.parse(response)['upload']['token']
# Redmine will throw validation errors if you do not
# send a wiki content when attaching the image. So
# we just get the current content and send that
wiki_text = JSON.parse(RestClient.get(wiki_url))['wiki_page']['text']
response = RestClient.put(wiki_url, {
:attachments => {
:attachment1 => { # the hash key gets thrown away - name doesn't matter
:token => token,
:filename => 'image.png',
:description => 'Awesome!' # optional
}
},
:wiki_page => {
:text => wiki_text # original wiki text
}
})

Rails3 mailer with image_tag ignoring host

I've got a Rails3 mailer layout that include images.
This ones are used like :
image_tag("emails/top.gif", :width => "700", :height => "10", :alt => "")
As of Rails 2, this images included the host and produced the expected result. However, since Rails3 the config.action_mailer.default_url_options seems to be ignored.
Is there anything I'm missing?
Update
my config/environment/development.rb include:
config.action_mailer.default_url_options = { :host => 'mydomain.tld' }
Needs to use config.action_mailer.asset_host = 'http://mysite.com' in your environment config file
Credits: wmoxam in #rubyonrails

Resources