Wicked-PDF not showing images, 'wicked_pdf_image_tag' undefined
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..
Wicked PDF - images are not showing
After trying all kinds of solutions, here is what worked for me:
<%= wicked_pdf_image_tag(polymorphic_url(@certificate.certificate_template.brand_logo), width: "400px") %>
Wicked pdf not showing images in action mailer
I'm curious what the HTML for this outputs for you in the final PDF if you render from a controller vs an email:
<pre>
<%= image_tag(wicked_pdf_asset_pack_path("media/images/header_lighuen.png"), width: "600", height: "120") %>
</pre>
The <pre>
tag is added on purpose, so you can see the markup, instead of it being rendered as an image.
I'm also curious if you have a different result if you do this, to pre-render the PDF before passing it to your mailer, like so:
pdf = WickedPdf.new.pdf_from_string(
render_to_string(
:template => 'shared/estimation_pdf.html.erb',
page_size: 'A4',
layout: 'light_pdf.html',
encoding: 'UTF-8',
disable_smart_shrinking: true,
viewport_size: '1920x1080',
margin: {
top: 10, # default 10 (mm)
bottom: 10,
left: 5,
right: 5
}
)
)
EstimationMailer.with(estimation: estimation, pdf: pdf).new_estimation_email.deliver_now
class EstimationMailer < ApplicationMailer
helper ApplicationHelper
def new_estimation_email
@estimation = params[:estimation]
@pdf = params[:pdf]
attachments["Presupuesto.pdf"] = @pdf
mail(to: "#{@estimation.client_email}", subject: "Presupuesto "+@estimation.code+" "+@estimation.created_at.strftime("%d/%m/%Y"))
end
end
Of course, that means preparing the PDF before getting to the mailer, so you might want to put that in a separate job if it ends up working for you.
Let me know how it goes, or post an update with the additional information, and I'll try to help.
WickedPDF template image_tag / wicked_pdf_image_tag not working on Rails API-only
I finally got the thing to work. There's no need to change the application_controller.rb
class. All I had to do is to specify the host of the image and with that construct the rest of the URL like this:
wicked_pdf_image_tag("localhost:3000#{image_url("foo.png")}")
Of course, the host will change once the application gets on productive. So for that, I created a helper that manages the image URL for me:
def manage_image_url
return case Rails.env
when 'development' then 'http://localhost:3000'
when 'develop' then 'https://yourpagehere.com'
else 'https://yourotherpagehere.com'
end
end
In the controller that generates the PDF, I pass the result of that method as variable, and the final code should look like this:
wicked_pdf_image_tag("#{manage_image_url}#{image_url("foo.png")}")
The image_tag
method also works. Note that if your Rails app is not API-only you probably won't need to do this. I'm still not sure of what causes the images to not show if you try to load them from the assets or public folders, but my guess is that it has to do with how API-only apps work. Have a nice day!
How display image during pdf generation using wicked_pdf
Use the built-in helper wicked_pdf_image_tag
<%= wicked_pdf_image_tag(@image.snap.url(:thumb)) unless @image.blank? %>
Rails Wicked PDF error log (image not rendering)
Wicked PDF can have issues rendering images from the asset pipeline. Try using the wicked_pdf_asset_base64
helper method i.e.
<%= image_tag wicked_pdf_asset_base64('logo-invoice.jpg'), class: 'logo' %>
Related Topics
Retrieving Image Height with Carrierwave
Use Separate Authentication Model with Devise on Rails
Ruby, Tor and Net::Http::Proxy
Ruby - Difference Between :Variable and @Variable
Why Doesn't Module.Method_Defined(:Method) Work Correctly
"Gem Update --System Is Disabled on Debian" Error
Including a Virtual Attribute in the Respond_With Hash
Ruby: Remove Whitespace Chars at the Beginning of a String
How to Embed Ruby in JavaScript (Rails + .Html.Erb File)
Rspec View Undefined Method Stub_Model
Memorable Name Generator Gem for Ruby
Convert 12 Hr Time to 24 Hr Format in Ruby
Ruby on Rails - Paperclip and Dynamic Parameters
Ruby Escape Argv Argument or String as Argument to Shell Command
Should I Deploy My Ruby on Rails Application on Heroku
Rails 4 Strong Parameters Without Required Parameters