mileszs / wicked_pdf

PDF generator (from HTML) plugin for Ruby on Rails
http://www.mileszs.com/wicked-pdf-plugin
MIT License
3.54k stars 646 forks source link

Image not render #940

Open carlosmedrano97 opened 4 years ago

carlosmedrano97 commented 4 years ago

Issue description

I'm using a simple


pdf = WickedPdf.new.pdf_from_string("<img alt='logo' style='width: 10%; height: 100%;' src='https://collegeengine.blob.core.windows.net/media/images/logos/CE-Icon_4C.png'>")
save_path = Rails.root.join('filename.pdf')
File.open(save_path, 'wb') do |file|
  file << pdf
end

Added the two gems

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'

Expected or desired behavior

The image should be included in the pdf

System specifications

wicked_pdf gem version (output of cat Gemfile.lock | grep wicked_pdf):

wicked_pdf (2.1.0)

wkhtmltopdf version (output of wkhtmltopdf --version):

wkhtmltopdf 0.12.3.2

belarba commented 4 years ago

change the https to http to test

unixmonkey commented 4 years ago

@carlosMedrano27 I did a quick test with this on the command-line, and think I discovered what's going on.

echo "<img alt='logo' style='width: 10%; height: 100%;' src='https://collegeengine.blob.core.windows.net/media/images/logos/CE-Icon_4C.png'>" > test.html
wkhtmltopdf test.html out.pdf

will not output your image either, telling me this is not a problem with wicked_pdf, but with the underlying wkhtmltopdf.

Trying to reduce the variables involved, when I removed the style attributes, it started working:

echo "<img alt='logo' src='https://collegeengine.blob.core.windows.net/media/images/logos/CE-Icon_4C.png'>" > test.html
wkhtmltopdf test.html out.pdf

I was able to add back in the width style with style='width: 10%', and that worked too. However setting the height style, or setting with the height attribute like height='100%', causes the image to not show, where setting height='100' does (which is pixels instead of percent). I even tried setting it in a style tag like <style>img { height: 100%; }</style>, and this also causes the image to not show.

It doesn't even seem to matter if it's in a proper HTML document with doctype & body within a div with a fixed height; it just seems to not like setting a height of 100% on an image in any circumstance I've tried.

It seems to behave the same with wkhtmltopdf version 0.12.3 as well as the newer 0.12.6.

I would suggest you report this to the wkhtmltopdf issue tracker as this could be a legitimate issue, and I'm stumped to why this might be happening. Please link the issue here, so I can keep track of it, too.

Of course you can work around it by omitting that height style in the meantime, as images should be 100% height in relation to the width anyway.

carlosmedrano97 commented 4 years ago

Thanks for your help @unixmonkey, I submitted the issue on the link you provide me, the issue I created is the #4832