wicked_pdf icon indicating copy to clipboard operation
wicked_pdf copied to clipboard

A pdf with an external image doesn't get rendered

Open GildedHonour opened this issue 6 years ago • 22 comments

While this is rendered properly:

  <img src="<%= File.join($config[:wicked_pdf_static_assets_physical_file_root], '/images/sample.png' ) %>" />

this doesn't, as if an image didn't exist:

  <img src="https://some_url_here/image.png"  %>" />

Is this an intended behaviour? What's the workaround? An image exists

GildedHonour avatar Mar 18 '18 05:03 GildedHonour

The syntax you posted above includes and unbalanced closing paren and erb tag, but that was probably a typo, so I'll assume you are having an issue with a normal image tag like this:

<img src="https://some_url_here/image.png" />

If you render with render pdf: 'mypdf', show_as_html: true, do you still have issues? Open the inspector console and look for warnings.

It is a specific HTTPS image, or several?

My thoughts are perhaps there may be server-side referrer checking that prevents hot-linking of images from other domains, some kind of cross-site request forgery, or maybe an untrusted or invalid SSL certificate.

There were some versions of wkhtmltopdf (in the earlier 12.x version range) that used SSLv3 for HTTPS. Many hosts (including Heroku) disabled SSLv3 to prevent Heartbleed attacks, so things just kind of stopped working when they did that. Newer versions of wkhtmltopdf have addressed the issue.

I recommend updating to the newest wkhtmltopdf version if you suspect this is the issue, but if you have installed a version from the past few years, I suspect this isn't your issue.

Let me know how it goes!

unixmonkey avatar Mar 18 '18 18:03 unixmonkey

Yes, that was a typo. It's a rack app, non-rails.

Is render pdf: 'mypdf', show_as_html: true a fucntion of the gem? Or Rails?

I'm using AWS S3 for storing an image. I can access it directly via a browser, in the incognito mode too.

GildedHonour avatar Mar 19 '18 02:03 GildedHonour

gemfile.lock:

wkhtmltopdf-binary (0.12.3.1)
pdf-core (0.7.0)
pdfkit (0.8.2)
wicked_pdf (1.1.0)

GildedHonour avatar Mar 19 '18 02:03 GildedHonour

They are functions of the gem, but hook into Rails’ rendering engine. I assume then that you are using WickedPdf.new.render_from_string(html, options) then? Can you share some info about how you are using it and perhaps an image link that you are having trouble with?

unixmonkey avatar Mar 19 '18 03:03 unixmonkey

Will do later An image is accessiable from wget too

GildedHonour avatar Mar 19 '18 03:03 GildedHonour

I've included 2 public images and they're still not getting rendered:

        <br />
        <br />
        <br />
        <img src="https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg" />


        <br />
        <br />
        <br />
        <img src="http://via.placeholder.com/350x150" />

I generated an html file out of my html code and the images were shown correctly.

In a pdf they're rendered as lines, 2 ones in this case

I'm creating a pdf this way:

fl = WickedPdf.new.pdf_from_string(html)
fl_nm = "/tmp/123.pdf"
File.open(fl_nm, "wb") {|x| x << fl}

GildedHonour avatar Mar 19 '18 09:03 GildedHonour

_2018-03-19_17-03-35

GildedHonour avatar Mar 19 '18 09:03 GildedHonour

when an image is located locally, it's working

GildedHonour avatar Mar 19 '18 09:03 GildedHonour

We had the same issue. We haven't tracked down yet what settings are causing this, but changing the link to http (as mentioned in this SO answer) allowed the images to render properly.

EmilyMB avatar Nov 26 '18 18:11 EmilyMB

Same issue, and thankfully the SO answer linked above works (thanks @EmilyMB!). This might not be relevant to this issue since the author used

    <img src="http://via.placeholder.com/350x150" />

but it's probably good to know regardless for those who wander here.

gytdau avatar Dec 29 '18 23:12 gytdau

Same problem

OmarHrra avatar Dec 27 '19 19:12 OmarHrra

Same problem

EsethuNenzani avatar Feb 06 '20 14:02 EsethuNenzani

Still not fixed?(

abdullaachilov avatar Jun 02 '20 12:06 abdullaachilov

@abdullaachilov Are you using a binary gem, or your system packages? Is your wkhtmltopdf binary compiled with SSL support? Does your hosting system need the ssl dev libraries installed? Are the image links fully-qualified with protocol and host (not relative) Does the server you are loading images from allow them to be rendered from a 3rd-party site (which is what rendering from a local tempfile is doing)? If you put a binding.pry here in wicked_pdf.rb#pdf_from_string, or here in wicked_pdf.rb#pdf_from_url, and either make a copy of the tempfile from your filesystem, or copy the contents of the string to an html file, does it work when you open that local file with Chrome?

unixmonkey avatar Jun 02 '20 14:06 unixmonkey

Quick response, cool, I will gather all info now, as Im dealing with that trouble for some time already. @unixmonkey

Does your hosting system need the ssl dev libraries installed?

  • We using our own server on ubuntu 18.04. our website works via https (nginx)

Are you using a binary gem, or your system packages?

  • I think I remember I've installed it from binary, how can I check it?

Is your wkhtmltopdf binary compiled with SSL support?

  • How I can check it?

Does the server you are loading images from allow them to be rendered from a 3rd-party site (which is what rendering from a local tempfile is doing)?

  • Yes, it is from Cloudinary. We use same images just on website.

If you put a binding.pry here in wicked_pdf.rb#pdf_from_string, or here in wicked_pdf.rb#pdf_from_url, and either make a copy of the tempfile from your filesystem, or copy the contents of the string to an html file, does it work when you open that local file with Chrome?

  • same links on localhosts works perfectly fine (MacOS) problem on both servers (ubuntu 18.04 each)

abdullaachilov avatar Jun 02 '20 14:06 abdullaachilov

We using our own server on ubuntu 18.04. our website works via https (nginx)

That doesn't answer the question. Please visit the link I posted about installing SSL libraries in your staging or production environment and retry your PDFs.

I think I remember I've installed it from binary, how can I check it?

cat Gemfile.lock | grep wkhtmltopdf will tell if you are using a wkthmltopdf binary gem. If that does not have anything, then on your Ubuntu servers, apt list --installed | grep wkhtmlto should tell you if you installed a system package (that may not be compiled with SSL or QT feature support to make more than basic uses work)

Yes, it is from Cloudinary. We use same images just on website.

But are they full "https://example.com/path/to/image.jpg" src's on the image tags, or are they relative like /path/to/image.jpg? When rendered from a tempfile, they need to be fully qualified, and include the protocol and host.

same links on localhosts works perfectly fine (MacOS) problem on both servers (ubuntu 18.04 each)

Yes, but on localhost, your images are served in development mode, which usually means relative paths. Please try what I suggested. I think it will help you determine why the images might not be showing if you open the tempfiles, and the images don't show up there either.

unixmonkey avatar Jun 02 '20 15:06 unixmonkey

That doesn't answer the question. Please visit the link I posted about installing SSL libraries in your staging or production environment and retry your PDFs.

openssl version
OpenSSL 1.1.1  11 Sep 2018

Does this it?

cat Gemfile.lock | grep wkhtmltopdf
wkhtmltopdf-binary (0.12.3.1)
  wkhtmltopdf-binary

I did use absolute path on both localhost and production, aka 'https://cloudinary.com/image.jpg' Still absolute path to extern domain works on local, but doesn't in production/staging

abdullaachilov avatar Jun 02 '20 17:06 abdullaachilov

Please upgrade your wkhtmltopdf-binary gem to the current version and report back. There are lots of changes between 0.12.3.1 and 0.12.5.4.

unixmonkey avatar Jun 02 '20 17:06 unixmonkey

updating wkhtmltopdf-binary gem to 0.12.6 fix the issue

drkmen avatar Jun 23 '20 09:06 drkmen

Oh my, Om deeply sorry - update to 0.12.5.4. solved all my troubles, I wanted to comment but apparently I forgot =(

abdullaachilov avatar Jun 23 '20 09:06 abdullaachilov

I can confirm using 0.12.6.2 with an image tag of = wicked_pdf_image_tag image_url("image_name") with an image hosted on S3 that it is properly rendering.

davidahopp avatar Jun 30 '20 14:06 davidahopp

works as expected after update version, thanks 👍🏼

ervinismu avatar Jul 04 '21 08:07 ervinismu