Wicked-PDF not showing images, 'wicked_pdf_image_tag' undefined
Asked Answered
P

4

14

I want to generate a PDF with our department logo in it. When I try to use the WickedPdf class in my controller (using the method described at https://github.com/mileszs/wicked_pdf):

def some_action
  image_tag_string = image_tag('logo.jpg')
  pdf = WickedPdf.new.pdf_from_string(image_tag_string)

  save_path = Rails.root.join('testpdfs','logotest.pdf')
  File.open(save_path, 'wb') do |file|
    file << pdf
  end
end

...the application saves the PDF to the target directory, but it has a blue-and-white '?' mark where the image should be.

If I do this instead:

  image_tag_string = wicked_pdf_image_tag('logo.jpg')
  pdf = WickedPdf.new.pdf_from_string(image_tag_string)

I get the following error:

NoMethodError:
   undefined method `wicked_pdf_image_tag' for #<...

It would appear that my Rails app is also missing / not linking to a helper file belonging to the wicked-pdf gem.

Answers to similar questions on StackOverflow recommend writing a custom "image-tag" helper to locate the image or installing wkhtmltopdf. For me, image-tag shows the logo just fine when placed in a View (whatever.html.erb). "logo.jpg" is already located in both the asset pipeline and #{RailsRoot}/public/images. Finally, I am using wkhtmltopdf 0.9.9, wicked-pdf 0.11.0, and rails 4 on Ubuntu 14.04.

In a nutshell - what am I doing wrong that causes WickedPDF to fail to render the image?

Preamble answered 7/5, 2015 at 19:46 Comment(7)
wicked_pdf_image_tag is a helper method, and as such it should be used in views, or other helpers. Post some more information about your implementation.Interstice
What information would be useful? Currently, I trigger the above method controller#some_action with a button on a test page. I have written a test containing the same code in Rspec3 and it does the same thing when I run it - builds the PDF, saves it, puts a question mark where the image should be.Preamble
As I have said, the wicked_pdf_image_tag is a helper method, so it can't be used (at least it shouldn't be) in controller action. You should make a view, where you'd put in your image, and use that view to generate pdf. But to make long story short - for wicked_pdf the image paths need to be a full, absolute paths, with full file system path or full url (with domain and protocol). I'll try to come up with some simple example and post it here (unless someone beats me to it).Interstice
You said "I want to generate a PDF with our department logo in it". Is that all you need this code for? A pdf only with an image within? That's what your code sample suggest.Interstice
We currently have a tool that renders PDFs from user-entered text and attaches them to emails. That part works very well. I'm trying to include our department logo at the top of the PDFs (before the user's text) now. But WickedPDF won't even render a PDF containing only an image_tag. I want to get that working first and build up from there.Preamble
Thanks for the info about the helper! I'll try messing around with the paths again. If I don't post a result by Tuesday, it can be safely assumed that I have not found a working path.Preamble
So maybe post some parts of your code for attaching pdf to emails and we can work out a solution. Will be a lot easier to know what the actual codebase really is. Or you can look at actual solutions here: #5422115 and here: #5763083. It looks like it's been asked and solved before.Interstice
P
16

First thing create a pdf template to render and use your wicked_pdf tags in that template.. for example-

app/views/layout/application.pdf.erb-

<!doctype html>
<html>
  <head>
    <meta charset='utf-8' />
  </head>
  <body onload='number_pages'>
    <div id="content">
      <%= yield %>
    </div>
  </body>
</html>

app/views/pdf/pdf_view.pdf.erb-

<div>
  <%= wicked_pdf_image_tag 'logo.jpg' %>
</div>

use this template instead

def save
  pdf = WickedPdf.new.pdf_from_string(
                        render_to_string(
                          template: 'example/pdf_view.pdf.erb',
                          layout: 'layouts/application.pdf.erb'))
  send_data(pdf,
            filename: 'file_name.pdf',
            type: 'application/pdf',
            disposition: 'attachment') 
end

This might help you..

Picofarad answered 9/5, 2015 at 6:48 Comment(0)
G
9

Use the wicked_pdf_image_tag helper in your view and reference the image with asset_url if your image is in public/images or use asset_pack_url if the image is in public/packs/media/images

<%= wicked_pdf_image_tag asset_url('/images/footer_logo.png') %>

or

<%= wicked_pdf_image_tag asset_pack_url('media/images/footer_logo.png') %>
Gegenschein answered 5/9, 2021 at 1:16 Comment(1)
This helped me. However, as the image in my case was in assets/images/, asset_url('image.png') was all I needed. Thanks.Pitchfork
F
0

I converted image url that 'http' from 'https'. Than worked.

Heroku-18
Rails 4.2
wicked_pdf (1.1.0)
wkhtmltopdf-binary (0.12.4)
Fiesole answered 13/2, 2019 at 19:30 Comment(1)
Install to 0.12.5 of wkhtmltopdf and see if that resolves the issue. There was a known issue with https image URLs prior to that version: github.com/wkhtmltopdf/wkhtmltopdf/issues/3001Captor
H
0

In my case, I am using carrierwave, the solution was taken from this post

<img src="<%= root_url + "/" +file.service_url %>">

This worked on rails 5.

Hall answered 3/3, 2022 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.