Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images not appearing when rendering a file from model #398

Closed
numsweb opened this issue Mar 31, 2015 · 15 comments
Closed

Images not appearing when rendering a file from model #398

numsweb opened this issue Mar 31, 2015 · 15 comments

Comments

@numsweb
Copy link

numsweb commented Mar 31, 2015

I am using this code in my model to generate a PDF

  view = ActionView::Base.new(ActionController::Base.view_paths.first, {})
       view.extend(ApplicationHelper)
       view.extend(Rails.application.routes.url_helpers)
       pdf = WickedPdf.new.pdf_from_string(
       view.render(
        template: 'alerts/show_print.pdf.erb',
        locals: { :@alert => self }),
        :layout => 'layouts/pdf.html.erb',
        :print_media_type => false,
        :page_size => "A4",
        :show_as_html => true,
        :disable_smart_shrinking => false, 
      )
  save_path = Rails.root.join('public', filename)
   File.open(save_path, 'wb') do |file|
     file.write pdf
     file.close
   end

When I save the file, the images do not appear. I am debugging the code to show the URL for the image.

screen shot 2015-03-31 at 2 22 58 pm

@unixmonkey
Copy link
Collaborator

I don't see an image when I go to https://dx4d070bb134o.cloudfront.net/uploads/amber-dev/vehicle/avatar2/21/flyer_images__19_.jpeg, so neither can wkhtmltopdf.

Is that path correct? I get a message that the DNS failed to find the server dx4d070bb134o.cloudfront.net.

@numsweb
Copy link
Author

numsweb commented Mar 31, 2015

typo in host name...
bbl34

was typed as
bb134

DUH!

@waysidekoi
Copy link

Looks like it works with http://, but not https

@numsweb
Copy link
Author

numsweb commented Mar 31, 2015

that link I pasted works and it is https

@unixmonkey
Copy link
Collaborator

Great! 👍

Closing this issue. If something else comes up, feel free to reopen or comment.

@waysidekoi
Copy link

I was able to replicate your problem (rendering in the controller) with the https link. When i changed the link to using http, it rendered correctly

@numsweb
Copy link
Author

numsweb commented Mar 31, 2015

yeah, the typo was not the issue, let me try http

@numsweb
Copy link
Author

numsweb commented Mar 31, 2015

Using http works for me also Thanks, guys.

@unixmonkey
Copy link
Collaborator

FWIW, I can see the car at both http and https urls, if the https version isn't rendering for you through wicked_pdf, it might be because you need to update wkhtmltopdf (the binary). Some recent versions used SSLv3 for HTTPS, which was quickly disabled by many hosts due to the POODLE attack, so newer versions have been released to work around that.

@numsweb
Copy link
Author

numsweb commented Mar 31, 2015

Yeah, I updated it last week to the current version after getting that same
suggestion from here.


John Kropka
Software Developer at CabForward
http://www.linkedin.com/in/kropka
(512) 461-6914 => Mobile
Skype => s2000.coder

On Tue, Mar 31, 2015 at 3:01 PM, David Jones notifications@github.com
wrote:

FWIW, I can see the car at both http and https urls, if the https version
isn't rendering for you through wicked_pdf, it might be because you need to
update wkhtmltopdf (the binary). Some recent versions used SSLv3 for
HTTPS, which was quickly disabled by many hosts due to the POODLE attack,
so newer versions have been released to work around that.


Reply to this email directly or view it on GitHub
#398 (comment).

@numsweb
Copy link
Author

numsweb commented Mar 31, 2015

switching to http in the carrierwave asset.host isn't an option for production because the server is serving https.

but why would https image links work when rendering to html and not when rendering to a file?

@unixmonkey
Copy link
Collaborator

Because they need to be downloaded and rendered through the virtual web browser that wkhtmltopdf uses to generate the PDF.

What version of wkhtmltopdf is it using? The newer versions are 0.12.x and should work fine. If they don't, the workaround could be downloading the files with open-uri, then base64 embedding them into the html something like this:

require 'open-uri'
def embed_remote_image(url, content_type)
  asset = open(url, "r:UTF-8") { |f| f.read }
  base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")
  "data:#{content_type};base64,#{Rack::Utils.escape(base64)}"
end
<%= image_tag(
  embed_remote_image(
    'https://dx4d070bbl34o.cloudfront.net/uploads/amber-dev/vehicle/avatar2/21/flyer_images__19_.jpeg',
    'image/jpeg'
  )
) %>

@numsweb
Copy link
Author

numsweb commented Mar 31, 2015

I am using 0.12.2.1_osx-cocoa-x86-64 pkg, downloaded 3/26/2015

wkhtmltopdf 0.12.2.1 (with patched qt)

@numsweb
Copy link
Author

numsweb commented Apr 1, 2015

This worked for me (embeded_remote_image suggestion)

Thanks!


John Kropka
Software Developer at CabForward
http://www.linkedin.com/in/kropka
(512) 461-6914 => Mobile
Skype => s2000.coder

On Tue, Mar 31, 2015 at 3:31 PM, David Jones notifications@github.com
wrote:

Because they need to be downloaded and rendered through the virtual web
browser that wkhtmltopdf uses to generate the PDF.

What version of wkhtmltopdf is it using? The newer versions are 0.12.x
and should work fine. If they don't, the workaround could be downloading
the files with open-uri, then base64 embedding them into the html
something like this:

require 'open-uri'def embed_remote_image(url, content_type)
asset = open(url, "r:UTF-8") { |f| f.read }
base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")
"data:#{content_type};base64,#{Rack::Utils.escape(base64)}"end

<%= image_tag( embed_remote_image( 'https://dx4d070bbl34o.cloudfront.net/uploads/amber-dev/vehicle/avatar2/21/flyer_images__19_.jpeg', 'image/jpeg' )) %>


Reply to this email directly or view it on GitHub
#398 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants