Wicked_pdf: Issues rending images from HTTPS

Created on 16 Oct 2014  ·  57Comments  ·  Source: mileszs/wicked_pdf

Hi All,

A couple days ago I realized that images were missing from the PDF's I'm generating ... it wasn't an issue prior to that and I'm still trying to track down what "changed" (no code changes on my end). Anyways ... I noticed that images sourced from HTTPS are not rendering in the generated PDF's but they do render when show_as_html is enabled.

Anybody have any insight into this issue?

I've setup a working example of the issue @ http://dev.e-snapapps.com/welcome/test.pdf (for show_as_html use http://dev.e-snapapps.com/welcome/test.pdf?debug=true).

Thanks =~ J

wkhtmltopdf

Most helpful comment

Also, just a note to @lalitlogical, @bcackerman, and @Epigene:

If you guys are on heroku, there's a heroku specific gem that works and is "more reliable" than the -binary-edge gem. There was some notice that you shouldn't "use it in production" (though we had it there for a bit and it wasn't an issue, but we have an offline generator for pdfs).

In our gemfile we have:

group :development, :test do
  gem 'wkhtmltopdf-binary-edge', '~> 0.12.2.1'
group :staging, :production do
  gem 'wkhtmltopdf-heroku'

All 57 comments

Hi, I have the same problem. It was working perfectly in the past and now just blank squares in the pdf.. Although when you use <%= wicked_pdf_image_tag 'image' %> helper, it works fine as well as using just links with http.

I've tried using wicked_pdf_image_tag but it appears that that helper ONLY works for local resources. This isn't the issue at hand but just wanted to be thorough.

When I try to use it to render a remote image using wicked_pdf_image_tag (either HTTP or HTTPS) it fails with the following error.

F, [2014-10-17T11:00:58.321786 #7285] FATAL -- :                                                                                                                                                               
ActionView::Template::Error (undefined method `pathname' for nil:NilClass):                                                                                                              
    11:                 <%= wicked_pdf_image_tag 'http://rubyonrails.org/images/rails.png' %>
    12:         </body>                                                                                                                                                                                        
    13: </html>                                                                                                                                                                                                
  app/views/welcome/test.pdf.erb:11:in `_app_views_welcome_test_pdf_erb__928987179_86697920'                                                                                                                   
  app/controllers/welcome_controller.rb:12:in `block (2 levels) in test'                                                                                                                                       
  app/controllers/welcome_controller.rb:9:in `test'        

@jmesserer Remote images that aren't part of the asset pipeline or in /public can just use regular image_tag.

@unixmonkey - Thanks ... I was aware of that but I'm still seeing an issue with remote images sourced from HTTPS locations as seen in my example @ http://dev.e-snapapps.com/welcome/test.pdf

Doesn't work -->
<%= image_tag 'https://www.etsy.com/assets/dist/images/etsylogo.0000.png' %>

Works -->
<%= image_tag 'http://rubyonrails.org/images/rails.png' %>

Thanks =~ J

Also seeing this issue — is there anything I can do to get more verbose debug output to help narrow down the cause?

I am also experiencing this issue. Local images render just fine, but remote images (S3, Rackspace Cloudfiles) served over SSL fail to render.

I'm also experiencing this issue. Any ideas? It really has to do with HTTPS.

This one works : http://www.etsy.com/assets/dist/images/etsylogo.0000.png
While this one does not : https://www.etsy.com/assets/dist/images/etsylogo.0000.png

I haven't figured anything out yet but in the meantime I was able to circumvent the issue by downloading the images I need (using httparty gem) and rendering them locally. I'm sure there's room for improvement but it works and gets my users up & running again.

# download image
remote_image = 'https://www.google.com/images/srpr/logo11w.png'
local_image = "#{Rails.root}/public/images/#{File.basename remote_image}"

unless File.exists? local_image
  File.open(local_image, 'wb') do |f| 
    f.write HTTParty.get(remote_image).parsed_response
  end       
end

# render in PDF
<% local_image = File.basename remote_image %>
<%= wicked_pdf_image_tag(image_path(local_image)) %>

It appears that the server will need openssl-devel packages installed for this to work, even with a statically compiled binary.

See https://code.google.com/p/wkhtmltopdf/issues/detail?id=17

Any clue how to fix this on Heroku?

We've encountered the same issue. Since last week assets fetched over HTTPS were no longer working. As it turns out the latest stable relase of wkhtmltopdf uses SSLv3 for its HTTPS connections.
Since the SSLv3 POODLE attack a lot of web hosts have disabled SSLv3.
The best solution right now is to install the wkhtmltopdf v1.2.2 testing release.

I've also created an issue in the wkthmltopdf binary gem project. Once there is a (pre-)release of the gem it should be possible to use it on heroku too.

@joelcogen Thanks for pointing out that this is related to Heroku and others disabling SSLv3. I was wondering how so many people were just now starting to have issues when things have been working so well for years.

The issue with missing images (on sites that disable SSLv3) seems fixed if you use wkhtmltopdf-binary binaries from the master branch. Hopefully, we'll see a release soon: https://github.com/zakird/wkhtmltopdf_binary_gem/issues/17.

Im having this issue too. No solution yet.

+1

This is weird. if i set direct path of image it shows image but when i use dynamic data to show path it dosent work. here is an example.
<% barcode_name = "barcode_"+@my_ticket[:barcode_random]+'_'+@my_ticket[:id].to_s+'.png' %>
<% full_br_path = 'http://abc.com/eventdemo/public/data/barcode/'+barcode_name %>

This doesn't work but when i write below code it works.

how do i get dynamic data for img tag, i'm getting dynamic data for other parts like ticket price ,name and order number.
* note *
I have change img to imgs becase i don't know how to use code formatting here and that img tags are showing broken image because of fake domain name.

Hi @mileszs!
Any news on this issue?
Thanks.

I had this same issue on Heroku serving images from CloudFlare.

We'd been serving them successfully using CloudFare Pro but when we downgraded to the Free version the images stopped working when using SSL. (Seems the Pro vs Free version has some difference regarding SSL).

Upgrading to the latest WKTHMLtoPDF version seemed to fix it using:

https://github.com/dscout/wkhtmltopdf-buildpack

I'm using wkhtmltopdf 0.9.9 in local dev and production. Latest version of wkhtmltopdf loads really slow on OS X for some reason so I've chosen not to go above 0.9.9. Guess I'm going to have to use local http:// for my images. Thanks for the update.

Hi all,

I had this issue on version: 0.12.1 (with patched qt).

I was able to resolve it by upgrading to version: 0.12.2.1 (with patched qt)

Has anyone found a solution to this other than copying images to a non-https location? I've tried using the wkhtmltopdf-binary gem, but it has no effect.

Strangely this only stopped working recently, as it has been working for the past few months. I don't check the PDFs every day, but we just started noticing them not having images. We are using S3, and I suspect it's something along the lines of what @jeroenj was saying regard SSL.

Is there a way to using the testing version of wkhtmltopdf with wicked_pdf, specifically on Heroku? Or is there another solution?

Yes, all I did was a gsub on the image tags and it worked perfectly, as follows:

<%= image_tag(image_url.gsub('https', 'http') %>

@ryanwjackson Yes, you can install the newest wkhtmltopdf and copy it to your bin directory in Rails, and point :exe_path at it in config/initializers/wicked_pdf.rb, or use https://github.com/rposborne/wkhtmltopdf-heroku

I ended up going with installing wkhtmltopdf directly on my computer and then using the suggestion above of a buildpack: https://github.com/paidapi/wkhtmltopdf-buildpack.

And it's notable that @nfriend21's solution did work, but I see that as a bandaid rather than a fix. Hopefully someone will update the binary gem and all will be right in the world.

FYI That Amazon discontinued support for SSLv3 in S3 on May 20th 2015. Just around that time we started having this problem with images no longer appearing in our pdf output.

Yes, the last AWS extension was until June 8 2015 and then we faced this issue as well...

<%= image_tag image_url.gsub('https', 'http') %> worked after many failed attempts at fixing the issue other ways.

To add a bit of insight, I found out today that the underlying gem, wkhtmltopdf is what handles the pulling in of images from s3.

Using rails on heroku, we were able to simply switch over gem 'wkhtmltopdf-binary, which is just out of date and uses the old (now deprecated as of June 8) SSLv3 module.

The new gem, -edge with a different maintainer seems to fix the issue!

gem 'wkhtmltopdf-binary-edge', '~> 0.12.2.1'

Though beware, we did have some minor styling issues that needed correction with some html5 elements, such as an unwanted border around <header> tags. Dunno what else.

It seems like the bottom line is that if you’re experiencing this issue then you probably have some dependency in the pipeline that is not updated to the most recent version.

On Wed, Jun 17, 2015 at 1:45 PM, Eric Garside [email protected]
wrote:

To add a bit of insight, I found out today that the underlying gem, wkhtmltopdf is what handles the pulling in of images from s3.
Using rails on heroku, we were able to simply switch over gem 'wkhtmltopdf-binary, which is just out of date and uses the old (now deprecated as of June 8) SSLv3 module.
The new gem, -edge with a different maintainer seems to fix the issue!
gem 'wkhtmltopdf-binary-edge', '~> 0.12.2.1'

Though beware, we did have some minor styling issues that needed correction with some html5 elements, such as an unwanted border around <header> tags. Dunno what else.

Reply to this email directly or view it on GitHub:
https://github.com/mileszs/wicked_pdf/issues/344#issuecomment-112911197

@garside solved the issue for me. Just make sure, like he said, to check formatting again. Some things shifted.

Seconding what @jimthedev said.

For anyone using wkhtmltopdf-heroku gem:

Updating wkhtmltopdf-heroku gem (from 1.0.0 to 2.12.2.1, :smile: ) solved it for me, albeit now I have some formatting issues to fix. Key thing seems to be to make sure that the wkhtmltopdf your server is sourcing is 0.12.2.1+. If you're on Heroku:

$ heroku run console

WickedPdf.config

then you should get this:

{:exe_path=>"/app/vendor/bundle/ruby/2.2.0/gems/wkhtmltopdf-heroku-2.12.2.1/bin/wkhtmltopdf-linux-amd64"}

or something similar if you're using a buildpack instead. Just make sure it's sourcing correctly.

@garside, you're a lifesaver!

Confirmed, switching to edge binary fixes asset accessability over HTTPS.

gem 'wkhtmltopdf-binary-edge', '~> 0.12.2.1'

allows disabling SSLv3 in nginx, leaving

ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;

One thing to note is that when using a newer wkhtmltopdf binary you may need to use wicked_pdf's master branch rather than the latest gem release for the moment to get correct support for items like TOC.

Thanks @garside.

I have this problem from a long time. I have tried the 'wkhtmltopdf-binary-edge' and now it is working fine.

Also, just a note to @lalitlogical, @bcackerman, and @Epigene:

If you guys are on heroku, there's a heroku specific gem that works and is "more reliable" than the -binary-edge gem. There was some notice that you shouldn't "use it in production" (though we had it there for a bit and it wasn't an issue, but we have an offline generator for pdfs).

In our gemfile we have:

group :development, :test do
  gem 'wkhtmltopdf-binary-edge', '~> 0.12.2.1'
group :staging, :production do
  gem 'wkhtmltopdf-heroku'

Thanks @garside
I have do a lot of research to try to fix this problem, I use the 'wkhtmltopdf-binary-edge' and now it is working fine.
And @nfriend21 , your method is also very smart😄

+1 @garside

I'm having issues getting my images to render too. I have a logo in my app/assets/images/logo.png as well as public/images/logo.png. I'm using wicked_pdf_image_tag('logo.png', size: '240x66'), and I'm on a SSL domain. When I generate a pdf, it just says "Logo", but doesn't actually render the image in production. In development it works just fine.

@jwoertink What version of wkhtmltopdf are you using?

Does it work with the wkhtmltopdf-binary-edge gem?

I'm using

gem 'wkhtmltopdf-binary-edge', '~> 0.12.2.1'
gem 'wicked_pdf'

@jwoertink

Does it work without the size option?

Try adding the option debug: true to your render, then right-clicking and viewing the source, to see if it looks like it's doing the right thing. It should be a <img src="file:///... with a complete path to the asset.

If that doesn't help you track down the issue, I might suggest you embed a base64 encoded image as demonstrated in #257

Ok, so I changed it up a bit. I did image_tag("file://#{Rails.root.join('public', 'images', 'logo-full.png')}"), and it works. When I add the size option back in, I got this error.

PDF could not be generated! Command Error: /srv/namechk/shared/bundle/ruby/2.1.0/gems/wkhtmltopdf-binary-edge-0.12.2.1/libexec/wkhtmltopdf-linux-amd64: error while loading shared libraries: libXrender.so.1: cannot open shared object file: No such file or directory 

@jwoertink Ah, that helps.

The wkhtmltopdf-binary-edge gem is statically compiled, but still has some dynamic dependencies.

If you can control the server, try this: http://stackoverflow.com/a/5036301/23915

Otherwise, I'd either try an older version of the wkhtmltopdf-binary gem, or compile your own binary.

oh nice! Yup, that worked! Thanks so much.

+1 @garside :+1:

@garside :+1:

@garside 👍

If it wasn't mentioned before... if you were using 'wkhtmltopdf-binary' and are switching to 'wkthmltopdf-binary-edge' it seems you need to uninstall 'wkhtmltopdf-binary' removing it and rebundling didn't work for me but it did work once I ran gem uninstall 'wkhtmltopdf-binary'

I am having an issue that I hope you may have knowledge about.The images were missing from the PDF's I'm generating(wicked_pdf). There is an issue with HTTPS request for wicked PDF, when i replace HTTPS with HTTP it worked but i have so many PDFs and don't want to change HTTPS to HTTP. I tried updating gem to this (gem 'wkhtmltopdf-binary-edge', '~> 0.12.2.1') but this too did not work for me. I am wandering here and there for the reason why this is happening.
I have following configurations.
Configuration of nginx(1.14) and OpenSSL 1.1.0g 2 Nov 2017 with ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Please help me to sort out this issue, If possible please elaborate the reason too.

@skyach-001 Please update to the latest gem 'wkhtmltopdf-binary-edge', '~> 0.12.5.0' and report back.

Hello @unixmonkey , as per suggested i update the gem but still got error "Error message
RuntimeError: PDF could not be generated! Command Error: */vendor/bundle/ruby/2.1.0/gems/wkhtmltopdf-binary-edge-0.12.5.0/libexec/wkhtmltopdf-linux-amd64: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory" will you please help me to figure out the problem.

@skyach-001 I suggest you post this here, but it looks like your system needs libpng and likely some other libraries installed for wkhtmltopdf to work properly.

Make sure to provide some info about your operating system and platform.

As a solution, I used the system's packaged wkhtmltopdf in the containers, and it works fine. Only drawback : it downloads a hell lot of dependencies.

Version 0.12.5.0 appears to be incompatible with the Heroku-18 stack. The only way I could get it to work on Heroku was to load S3 images over http rather than https.

This is a better substitution than the ones that have been mentioned above — it's nil-safe and anchored to the start of the string:

image_url&.gsub(/^https/, 'http')

@McGeekiest This solution should not have anything to do with the Heroku 18 stack, but loading HTTPS resources over many networks (including AWS). The workaround of loading assets over HTTP isn't always available (like for domains that force SSL), so you may need to do something like request the content with open-uri, read it in and output a local file or a base64 representation inline.

@unixmonkey Understood, but I was responding to "Please update to the latest gem 'wkhtmltopdf-binary-edge', '~> 0.12.5.0' and report back." That is not an option on Heroku-18, at least out-of-the-box, due to a libjpeg incompatibility.

Also, certainly, this does not present a general solution for all HTTPS resources, but for the specific combination of Heroku and AWS the gsub solution resolves the issue.

@McGeekiest Gotcha. Thanks for clarifying.

It appears that the latest https://github.com/rposborne/wkhtmltopdf-heroku works on Heroku-18 based on this

Was this page helpful?
0 / 5 - 0 ratings