wickedpdf not rendering images? - ruby-on-rails

i am using wickedpdf gem to generate pdf invoice from the html code.
gems:
gem 'wicked_pdf'
gem "wkhtmltopdf-binary"
gemfile.lock
wicked_pdf (1.0.6)
wkhtmltopdf-binary (0.9.9.3)
in controller:
def show_pdf_invoice
respond_to do |format|
format.html { render :layout => "pdf.pdf.erb" }
format.pdf do
render pdf: "show_pdf_invoice", :layout => 'pdf.pdf.erb'
#render :pdf => "pdf"#, :layout => 'pdf.html.erb'
end
end
end
in views/invoices/show_pdf_invoice.pdf.erb
<img id="image" src="https://www.google.co.in/logos/doodles/2016/holidays-2016-day-2-6356741311692800-scta.png" alt="logo" />
<%= wicked_pdf_image_tag 'https://www.google.co.in/logos/doodles/2016/holidays-2016-day-2-6356741311692800-scta.png' %>
pdf is getting generated. But the images are not showing. in the place of images empty boxes are coming. unable to find the issue.

I've had the same problem, mine was fixed by removing https for http. Have you tried this? and for the Amazon S3 part: You could use gsub for that as in: gsub("https", "http")

Using Rails 5.2 with Active Storage in combination with Amazon S3 storage I had the same problem.
In development on my local machine the images rendered perfectly, but on Heroku
they were presented as small empty rectangles.
To get the url from the logo uploaded to Active Storage I used: #my_object.logo.service_url.
Which used the standard url with https. As mentioned before, replacing this with http resolved the issue.
Full code used in my pdf generator view:
<%= wicked_pdf_image_tag #my_object.logo.service_url.gsub("https", "http") %>

Two Options
1. Upgrade to wkhtmltopdf 0.12.5.
-or-
2. Install libssl1.0-dev with apt-get install libssl1.0-dev.
See this issue for more information: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/3001

Related

Problem with generate pdf and display images

I have a problem with displaying the images in pdf. 5 out of 100 attempts work. After saving all data to database, there is opened a new site with pdf generated by gem wkhtmltopdf. During getting the image from path /rails/active_storage/blobs/... service is stopped and report isn't generated. There are no errors on consoles .Only solution is stopped the server ( ctrl+c ) and start again the server.
The server is stopped on request :
Started GET "/rails/active_storage/blobs/...
I display image by line :
<%= image_tag(#object.image, class: '') if #object.image.attached? %>
There is a method for generate report (above this code there are the varialbes to display in report )
respond_to do |format|
format.html do
redirect_to index
end
format.pdf do
render pdf: "Raport",
template: "myFolder/report.html.erb",
:footer => {
:font_size => '8'
}
end
end
I'm using rails 5.2.3, ruby 2.5.3
I'm expect to generate the pdf with images.

Rails app hangs when using Wicked_pdf gem and wkhtmltopdf-binary-edge

I'm trying to print a PDF of my view by using the `'wicked_pdf' gem. This is the first time I use this gem. I've read the the documentation and googled around and I also found some simular questions here on Stack Overflow But nothing seems to do the trick.
I've validated the binaries ( see edit below) they are working fine, so the problem isn´t there.
I found two strange lines in the terminal when running rails s( See edit at the bottom)
I've been searching for a solution to this for three straight days, and can't figure this out.
Am I missing something here?
the server constantly hangs with that output in the terminal
Rendered pages/partials/_travel_part.html.erb (36.6ms)
Rendered users/show.html.erb within layouts/application (531.7ms)
Rendered layouts/_navbar.html.erb (2.6ms)
Rendered shared/_footer.html.erb (0.8ms)
Completed 200 OK in 1640ms (Views: 1115.4ms | ActiveRecord: 136.0ms)
"***************[\"/usr/local/bin/wkhtmltopdf\", \"-q\", \"file:////var/folders/sm/zwm8cy0x73qb6q1pq22r4bjh0000gn/T/wicked_pdf20160913-35465-hkw9b8.html\", \"/var/folders/sm/zwm8cy0x73qb6q1pq22r4bjh0000gn/T/wicked_pdf_generated_file20160913-35465-10go9dt.pdf\"]***************"
Below is what I have so far.
I want to print views/users/show.html.erb so in the users_controller.rb I have this code in the showmethod
def show
#user = User.find(params[:id])
#users = User.order('created_at DESC').paginate(page: params[:page], per_page: 30)
#paper = current_user.papers.build
#electro = current_user.electros.build
#hwater = current_user.hwaters.build
#cleaning = current_user.cleanings.build
#transport = current_user.transports.build
#papers = current_user.papers
respond_to do |format|
format.html {render layout:'application'}
format.pdf {render pdf:"test",javascript_delay:2000,
layout:'application',template:'users/show.pdf.erb'}
end
end
In the application.html.erbI have this in the head using the wicked pdf helper
<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= wicked_pdf_javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag "http://www.google.com/jsapi"%>
<%= wicked_pdf_javascript_include_tag 'chartkick'%>
<%= csrf_meta_tags %>
The button in the views/users/show.html.erb is
<%= link_to 'Download Pdf', user_path(current_user, format: :pdf) %>
in the config/initializers/mime_types.rb I have this code
Mime::Type.register "application/pdf", :pdf
in the config/initializers/wicked_pdf.rb I have this code
WickedPdf.config = {
#:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf',
#:layout => "pdf.html",
:exe_path => '/usr/local/bin/wkhtmltopdf'
}
And in the gemfile.rb I have those two gems:
gem 'wicked_pdf', '~> 1.0', '>= 1.0.6'
gem 'wkhtmltopdf-binary-edge'
EDIT*
Ok I've Validated the binaries are working fine by typing this wkhtmltopdf google.com google in the terminal. So my conclusion is that the problem lies somewhere else than in the wkhtmltopdf. But where? I still can't find out...
(AddingWickedPdf) $ wkhtmltopdf google.com google
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
(AddingWickedPdf) $
ANOTHER EDIT
I noticed this lines in the terminal when I started my server rails s earlier to night, I don´t know what it means, but I'm sure it has something to do with the App hanging when rendering pdf:
/Users/dadi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/http/mime_type.rb:163: warning: already initialized constant Mime::PDF
/Users/dadi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack- 4.2.5/lib/action_dispatch/http/mime_type.rb:163: warning: previous definition of PDF was here
It would be so great if someone could take a look at this and guide me to the right path here.
thanks in advance
DH
The answer was originally posted here:
How rails resolve multi-requests at the same time?
Subsequently adapted for this question here (Last answer):
PDFkit hangs when generating a pdf with an image

html5 video tag not working in firefox for my rails app

For my Rails 4.2 app, my video_tag for HTML5 is working fine for Safari & Chrome, but I get No video with supported format and MIME type found in Firefox.
Here is my view:
<%= video_tag("preroll.webm", "preroll.ogv", "preroll.mp4", controls: true, size: "480x360", poster: "prerollposter.png") %>
Here is my controller show action:
def show
respond_to do |format|
format.any(:html, :m4v, :ogv, :webm) {
}
end
end
Here is my mime_types.rb file:
Mime::Type.register "video/webm", :webm
Mime::Type.register "video/ogg", :ogv
Mime::Type.register "video/mp4", :m4v
In application.html.haml, I have:
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
In my terminal, I've done...
rake assets:precompile
...and I've continually refreshed my firefox browser and cntl-c'ed out of the local server every time.
I'm new at embedding video into a Rails app, so I'm wondering what the Rails equivalent of .htaccess might be (Is it actually mime_types.rb?).
Any help on this would be much appreciated. I think I'd read somewhere previously that the Rails video_tag has some problems, but I'm not sure if that is the cause of Firefox not working here. Thanks so much!

google_visualr chart rendering error

i am trying Google-Visualr gem and got the above error.
As described here.
i installed the gem and in MyModel (we can use controller/model) i copied the demo code for line-chart from here.
i have a UserMailer,where i wanted to render html page along with my chart and using pdfkit i am converting rendered html to pdf.
class UserMailer < ActionMailer::Base
def my_report
#chart =MyModel.line_chart
html_data = render :template => "user_mailer/my_report"
kit = PDFKit.new(html_data,:page_size => 'Letter')
pdf = kit.to_pdf
end
in my my_report view i included
<script src='https://www.google.com/jsapi'></script>
in <head>
included this in <body> part
<div id='chart'></div>
<%= render_chart(#chart, 'chart') %>
But it is leading to me to undefined method "render_chart" error.
Any suggestion that how can i render chart with my html data?
where i am going wrong?
i am using rails 4
and 'google_visualr', '>= 2.1'

Rails 3.1 asset pipeline with PDFKit

I am using PDFkit with rails 3.1. In the past I was able to use the render_to_string function and create a pdf from that string. I then add the stylesheets as follows. My issue is that I have no idea how to access them from within the asset pipeline. (This is how I did it in rails 3.0)
html_string = render_to_string(:template => "/faxes/show.html.erb", :layout => 'trade_request')
kit = PDFKit.new(html_string, :page_size => 'Letter')
kit.stylesheets << "#{Rails.root.to_s}/public/stylesheets/trade_request.css"
So my question in how do i get direct access from my controller to my css file through the asset pipline?
I know I can use the Rack Middleware with PDFkit to render the pdf to the browser, but in this case i need to send the pdf off to a third party fax service.
Thanks for your help.
Ryan
Just ran into this issue as well, and I got past it without using the asset pipeline, but accessing the file directly as I would have previously in /public. Don't know what the possible cons are to using this approach.
I guess LESS and SCSS files won't be processed as they would have if accessed through the asset pipeline.
html = render_to_string(:layout => false , :action => 'documents/invoice.html.haml')
kit = PDFKit.new(html, :encoding=>"UTF-8")
kit.stylesheets << "#{Rails.root.to_s}/app/assets/stylesheets/pdf_styles.css"
send_data(kit.to_pdf, :filename => "test_invoice", :type => 'application/pdf')
A little late, but better late than never, eh.
I'd do it like this:
found_asset = Rails.application.assets.find_asset( "trade_request.css" ).digest_path
kit.stylesheets << File.join( Rails.root, "public", "assets", found_asset )
In Rails 3.1.1 stylesheets are written to /public/assets with and without the digest fingerprint.
This means that you should be able to reference these files just by changing the path in your code.
One gotcha though: if the PDF sheet is not referenced in a CSS manifest you'll have to add it to the precompile config:
config.assets.precompile += ['trade_request.css']
This tells sprockets to compile that file on its own.
As a (better) alternative, see if the asset_path helper works in your code. This will reference the correct file in dev and production.
I ended up copying the css file to my public directory and referring to it in the same way i did before with rails 3. For more information check out this question: Access stylesheet_link_tag from controller
I landed here trying to solve this problem and none of the answers seemed to solve the issue for me. I found the accepted answer of this stack overflow post worked for me:
How does one reference compiled assets from the controller in Rails 3.1?
I was even able to serve .css.erb files through this method.
Try
= stylesheet_link_tag "application", 'data-turbolinks-track': 'reload'
You should be able to access the stylesheet using this method:
ActionController::Base.helpers.asset_path("trade_request.css")
Making your code:
html_string = render_to_string(:template => "/faxes/show.html.erb", :layout => 'trade_request')
kit = PDFKit.new(html_string, :page_size => 'Letter')
kit.stylesheets = ActionController::Base.helpers.asset_path("trade_request.css")

Resources