I have installed Wicked PDF on rails 4, and now I have this issue:
RuntimeError in ClientsController#show
Error: Failed to execute: ["C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe", "--footer-center", "Center", "--footer-left", "Left", "--footer-right", "Right", "file://C:/Users/Rashid/AppData/Local/Temp/wicked_pdf20141017-9664-18xoryq.html", "C:/Users/Rashid/AppData/Local/Temp/wicked_pdf_generated_file20141017-9664-zo89le.pdf"] Error: PDF could not be generated! Command Error: Loading pages (1/6) [> ] 0% [======> ] 10% Error: Failed loading page file://c/Users/Rashid/AppData/Local/Temp/wicked_pdf20141017-9664-18xoryq.html (sometimes it will work just to ignore this error with --load-error-handling ignore) Exit with code 1 due to network error: ContentNotFoundError
def show
respond_to do |format|
format.html
format.pdf do
#example_text = "some text"
render :pdf => "file_name",
:template => 'clients/show.pdf.erb',
#:layout => 'pdf',
:footer => {
:center => "Center",
:left => "Left",
:right => "Right"
}
end
end
end
I was getting the same error and fixed it by following directions here
https://github.com/mileszs/wicked_pdf/issues/157
Namely I switched to tempory fix of gem by changing gemfile entry to:
gem 'wicked_pdf', github: 'mileszs/wicked_pdf'
I believe it has to do with using file:// instead of file:///.
Find wicked_pdf.rb in path/to/gem/wkhtmltopdf/bin, on line 64 replace file:// with file:///.
Related
In rails 4, I am using wicked_pdf gem for .pdf file download. I have to add an image inside this pdf, right now image is rendering through wicked_pdf_image_tag in development but in test environment image(s3) is not rendering.
Used Gems are,
gem 'wicked_pdf', '1.0.3'
gem 'wkhtmltopdf-binary', '0.9.9.3'
In initializers,
class WickedPdf
wkhtmltopdf_path = Rails.env.production? ? "#{Rails.root}/bin/wkhtmltopdf-amd64" : "#{Rails.root}/bin/wkhtmltopdf-amd64"
WICKED_PDF = {
:exe_path => wkhtmltopdf_path,
:wkhtmltopdf => wkhtmltopdf_path
}
end
In controller,
respond_to do |format|
format.html {
render :pdf => "sample",
:margin => {:top => 10, :bottom => 10, :left => 10, :right => 10},
:orientation => 'Portrait', # default , Landscape,
:no_background => true
}
end
In views, I have tried to load through
<%= Rails.env.development? ? wicked_pdf_image_tag("img/logo.png") : wicked_pdf_image_tag("#{Rails.root}/public/assets/img/logo.png") %>
<%= Rails.env.development? ? wicked_pdf_image_tag("img/logo.png") : wicked_pdf_image_tag("#{Rails.root}/assets/img/logo.png") %>
<%= image_tag(ActionController::Base.helpers.asset_path('img/logo.png')) %>
How can I load s3 image in pdf file?
You can put the image in a s3 bucket and make it public. After that try it like below. If you use the below code no need to use separate syntax for different environments, it will works for all .Hope it works.
<%= wicked_pdf_image_tag('//s3.amazonaws.com/bucket_name/image.png') %>
I have worked on the similar functionality but using PDFKit gem. But I think rendering logic will be almost similar.
Below is the code where I rendered my partial in controller
def print_batch
batch = Batch.find(params[:id])
respond_to do |format|
format.pdf {
html = render_to_string("_batch",:formats => [:html], layout: false , locals: { batch: batch })
Rails.logger.debug(html.inspect)
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "file_name_#{batch.id}.pdf", :type => 'application/pdf') and return
}
format.html
format.json { render json: {id: batch.id, processed: batch.processed?} }
end
end
In _batch.html.haml. You can see I have used person.s3_logo for rendering the pdf image.
- logo_image_pdf = person.logo.present? ? person.s3_logo : default_credit_logo
- logo_image_html = person.logo.present? ? image_path(Person.logo.thumb('100x100').url) : image_path('default_credit_logo.png')
- logo_image = params[:format] == 'pdf' ? logo_image_pdf : logo_image_html
.bucks
%img.logo{src: logo_image }
In Person.rb model. Anyway we cannot directly render image in PDF file directly from s3. So any gem will first download it in /tmp folder and render it. This is how I have done it in my model file.
def s3_logo
file = open(self.logo.remote_url)
file.path if file
end
Using version 0.8.2 of the PDFKit gem, passing in toc as indicated in the docs throws an error:
format.pdf {
html = render_to_string(:action => 'pdf.html.haml')
kit = PDFKit.new(html, {toc: true})
send_data kit.to_pdf, :filename => "thing.pdf", :type => 'application/pdf'
}
The generic error, which I'm showing so you can see that toc is being passed as wkhtmltopdf requires, and as the specs of the PDFkit gem suggest, without the --:
command failed (exitstatus=0): /Users/Emma/.rvm/gems/ruby-2.2.1/bin/wkhtmltopdf --page-size Letter --margin-top 0.75in --margin-right 0.75in --margin-bottom 0.75in --margin-left 0.75in --encoding UTF-8 toc - -
You can try
format.pdf {
html = render_to_string(:action => '<controller_name>/action_name')
kit = PDFKit.new(html.to_s, toc: '')
send_data kit.to_pdf, :filename => "thing.pdf", :type => 'application/pdf'
}
Cheers! I'm using wicked_pdf to generate pdf docs from views:
pdf = WickedPdf.new.pdf_from_string(
render_to_string(
:layout => "pdf_report.haml",
:handlers => [:haml],
:formats => [:pdf, :haml],
:orientation => 'Landscape',
:encoding => "utf8",
:page_width => '2000',
:dpi => '300'
)
)
It's all ok, if pdf has one page:
But if pdf doc has more than one page, then width of the page is broken:
I wasn't able to reproduce your "only has one page" scenario with the code you posted, however there is a problem with the options you are passing. They aren't even getting to wkhtmltopdf, so it is probably deciding for you what kind of options to use.
render_to_string will silently discard any options it doesn't understand, but is not part of wicked_pdf.
pdf_from_string takes two params, the first being the string to pdf-ify, the second is a hash of pdf options from the README.
I've added your issue to the wicked_pdf_issues project here to reproduce and debug it:
https://github.com/unixmonkey/wicked_pdf_issues/commit/b722e8a06c42e1f2bcbb98281915d1e94b4fe2c9
You should get the results you are looking for by changing your code to something like this:
string = render_to_string(
template: 'pages/issue_330',
formats: [:pdf],
handlers: [:erb]
)
options = {
orientation: 'Landscape',
page_width: '2000',
dpi: '300'
}
pdf = WickedPdf.new.pdf_from_string(string, options)
render :pdf => "file_name",
:layout => 'pdf.html.erb',
:template => 'transactions/show.pdf.erb',
:wkhtmltopdf => WICKED_PDF_BIN,
:show_as_html => true,
:layout => 'pdf.html.erb',
:header => {:html => { :template => 'shared/header.pdf.erb'}}
PDF is generated fine, unfortunately I do not see the header. I can stick the header in the main layout and it works fine too. It seems to me that the header line above is not being processed. The filename 'header.pdf.erb' does not seem to matter. I can point it to a file that does not exist and it throws no error.
This is Mac OS, Rails 3.2.1, ruby 1.9
You may want to simply render the template as a string and assign it to the header's content. Try this out:
header: {
content: render_to_string(template: 'header.pdf.haml')
}
It works well for me.
In a ruby on rails application prawn ,prawnto is used to generate pdf raises some error..
def generate_report
generate_report = params[:report_type]
# puts(generate_report)
if generate_report == "1"
# get count of all successful downloads
#total_downloads=StatisticDownload.where("DownloadSuccess=?","1").count
#puts(#total_downloads)
# get all downloads grouped by date
#downloads = StatisticDownload.select("date(Date) as downloaded_date, count(id) as count").where("DownloadSuccess=?","1").group("date(Date)")
respond_to do |format|
format.pdf { render :layout => false }
end
end
Code in generate_report.pdf.prawn
pdf.move_down(30)
book = #downloads.map do |item|
[
item.downloaded_date,
item.count
]
end
pdf.table book, :border_style => :grid,
:row_colors => ["FFFFFF", "DDDDDD"],
:headers => ["downloaded_date", "count"],
:align => { 0 => :left, 1 => :right, 2 => :right, 3 => :right }
/admin/generate_report gives a blank page as output
/admin/generate_report.pdf gives an error
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map
Extracted source (around line #2):
1: pdf.move_down(30)
2: book = #downloads.map do |item|
3: [
4: item.downloaded_date,
5: item.count
how can i rectify this error
May be worth going to http://railscasts.com/episodes/153-pdfs-with-prawn-revised and following the tutorial, you will have to subscribe to watch if you haven't got a pro subscription though.