Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

ruby on rails - Wicked pdf not showing images in action mailer

I want to show a header image in the attached pdf. I'm using the wicked_pdf_asset_pack_path as follows:

estimation_pdf.html.erb

   <%= image_tag(wicked_pdf_asset_pack_path("media/images/header_lighuen.png"), width: "600", height: "120") %>

enter image description here

This is working if a download the pdf directly, but it doesn't in the attached file at the mail.

estimation_mailer.rb

class EstimationMailer < ApplicationMailer
  helper ApplicationHelper

  def new_estimation_email
    @estimation = params[:estimation]
    
    # Attach the Lighuen logo
    attachments.inline["encabezado_email.jpg"] = File.read("#{Rails.root}/app/assets/images/encabezado_email.jpg")

    attachments["Presupuesto "[email protected]_at.strftime("%d/%m/%Y")+".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
        }
      )
    )

    mail(to: "#{@estimation.client_email}", subject: "Presupuesto "[email protected]+" "[email protected]_at.strftime("%d/%m/%Y"))
  end
end

This happens when i send the mail: enter image description here

The image's path is: app/javascript/images/header_lighuen.png

Console output:

Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSnJkbUZ5YVdGdWRITXZOM1kyYjNGdmJuQm5lR1YxZFdwek1tOXJZVEIxWlRscWFqUm1iaTgzTmpjd1lqQTVabVUxTkdOaFpUUXdNbVZsTm1GbVpEZG1NR1JqT1RaaE5EVmlOV013WXpjNFlqQTRaRFJrT1RsbFpUVmtOamcwTldVMU5EUmpZVEl4QmpvR1JWUTZFR1JwYzNCdmMybDBhVzl1U1NKbmFXNXNhVzVsT3lCbWFXeGxibUZ0WlQwaWQyRnNiR2hoZG1WdUxUazJOM3A1YTE4eE1qZ3dlREV3TWpRdWNHNW5JanNnWm1sc1pXNWhiV1VxUFZWVVJpMDRKeWQzWVd4c2FHRjJaVzR0T1RZM2VubHJYekV5T0RCNE1UQXlOQzV3Ym1jR093WlVPaEZqYjI1MFpXNTBYM1I1Y0dVaURtbHRZV2RsTDNCdVp6b1JjMlZ5ZG1salpWOXVZVzFsT2dwc2IyTmhiQT09IiwiZXhwIjoiMjAyMS0wMS0yNVQxNDowMTo1NS4xODNaIiwicHVyIjoiYmxvYl9rZXkifX0=--edb302b486ccd7687ec5a3f0f0611fcee054091d/wallhaven-967zyk_1280x1024.png" for ::1 at 2021-01-25 10:56:55 -0300
Processing by ActiveStorage::DiskController#show as PNG
  Parameters: {"encoded_key"=>"[FILTERED]", "filename"=>"wallhaven-967zyk_1280x1024"}
Completed 200 OK in 1ms (ActiveRecord: 0.0ms | Allocations: 460)


[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0] DEPRECATION WARNING: Rendering actions with '.' in the name is deprecated: shared/estimation_pdf.html.erb (called from new_estimation_email at /home/bashud/Documentos/APPS/vitromanage/app/mailers/estimation_mailer.rb:12)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0] DEPRECATION WARNING: Rendering actions with '.' in the name is deprecated: layouts/light_pdf.html (called from new_estimation_email at /home/bashud/Documentos/APPS/vitromanage/app/mailers/estimation_mailer.rb:12)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendering layout layouts/light_pdf.html.erb
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendering shared/estimation_pdf.html.erb within layouts/light_pdf.html
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Client Load (0.8ms)  SELECT "clients".* FROM "clients" WHERE "clients"."deleted_at" IS NULL AND "clients"."id" = $1 LIMIT $2  [["id", 8], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/estimation.rb:52:in `client_full_name'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Location Load (0.4ms)  SELECT "locations".* FROM "locations" WHERE "locations"."id" = $1 LIMIT $2  [["id", 9], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/estimation.rb:53:in `location_name'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0] [Webpacker] Everything's up-to-date. Nothing to do
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   EstimationSupply Load (0.9ms)  SELECT "estimation_supplies".* FROM "estimation_supplies" WHERE "estimation_supplies"."deleted_at" IS NULL AND "estimation_supplies"."estimation_id" = $1 ORDER BY "estimation_supplies"."position" DESC  [["estimation_id", 108]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/estimation.rb:173:in `has_tvh?'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Supply Load (0.4ms)  SELECT "supplies".* FROM "supplies" WHERE "supplies"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/estimation_supply.rb:62:in `is_tvh?'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Supply Load (0.3ms)  SELECT "supplies".* FROM "supplies" WHERE "supplies"."id" = $1 LIMIT $2  [["id", 11], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/estimation_supply.rb:62:in `is_tvh?'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   EstimationSupply Load (0.5ms)  SELECT "estimation_supplies".* FROM "estimation_supplies" WHERE "estimation_supplies"."deleted_at" IS NULL AND "estimation_supplies"."estimation_id" = $1 ORDER BY "estimation_supplies"."position" ASC  [["estimation_id", 108]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/views/shared/estimation_pdf.html.erb:51
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   CACHE Supply Load (0.0ms)  SELECT "supplies".* FROM "supplies" WHERE "supplies"."id" = $1 LIMIT $2  [["id", 11], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/estimation_supply.rb:42:in `supply_name'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Category Load (0.6ms)  SELECT "categories".* FROM "categories" INNER JOIN "categories_supplies" ON "categories"."id" = "categories_supplies"."category_id" WHERE "categories_supplies"."supply_id" = $1  [["supply_id", 11]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/supply.rb:106:in `is_special?'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   CACHE Supply Load (0.0ms)  SELECT "supplies".* FROM "supplies" WHERE "supplies"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/estimation_supply.rb:42:in `supply_name'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Category Load (0.5ms)  SELECT "categories".* FROM "categories" INNER JOIN "categories_supplies" ON "categories"."id" = "categories_supplies"."category_id" WHERE "categories_supplies"."supply_id" = $1  [["supply_id", 5]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/models/supply.rb:106:in `is_special?'
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   VariableConfiguration Load (0.6ms)  SELECT "variable_configurations".* FROM "variable_configurations" WHERE "variable_configurations"."status" = $1 ORDER BY "variable_configurations"."id" ASC LIMIT $2  [["status", 1], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   ? app/views/shared/estimation_pdf.html.erb:110
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendered shared/estimation_pdf.html.erb within layouts/light_pdf.html (Duration: 34.4ms | Allocations: 13643)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendered layout layouts/light_pdf.html.erb (Duration: 39.4ms | Allocations: 16971)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0] [wicked_pdf]: ["/home/bashud/.rvm/gems/ruby-3.0.0@vitromanage/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf", "file:////tmp/wicked_pdf20210125-25050-pk6m75.html", "/tmp/wicked_pdf_generated_file20210125-25050-8bz04n.pdf"]
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendering layout layouts/mailer.html.erb
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendering estimation_mailer/new_estimation_email.html.erb within layouts/mailer
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendered estimation_mailer/new_estimation_email.html.erb within layouts/mailer (Duration: 8.3ms | Allocations: 7391)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendered layout layouts/mailer.html.erb (Duration: 9.0ms | Allocations: 7585)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendering layout layouts/mailer.text.erb
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendering estimation_mailer/new_estimation_email.text.erb within layouts/mailer
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendered estimation_mailer/new_estimation_email.text.erb within layouts/mailer (Duration: 0.5ms | Allocations: 183)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0]   Rendered layout layouts/mailer.text.erb (Duration: 0.9ms | Allocations: 356)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0] EstimationMailer#new_estimation_email: processed outbound mail in 1566.9ms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "[email protected]+" "[email protected]_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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...