Search code examples
ruby-on-railsactionmailerwebpackerwicked-pdf

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
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0] Delivered mail [email protected] (3867.3ms)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0] Date: Mon, 25 Jan 2021 10:56:56 -0300
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Presupuesto PRE-AB8-26 15/01/2021
Mime-Version: 1.0
Content-Type: multipart/mixed;
 boundary="--==_mimepart_600ece2834831_61da1bb70-2ba";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_600ece2834831_61da1bb70-2ba
Content-Type: multipart/related;
 boundary="--==_mimepart_600ece2835e18_61da1bb70-1e1";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_600ece2835e18_61da1bb70-1e1
Content-Type: multipart/alternative;
 boundary="--==_mimepart_600ece28345dc_61da1bb70-346";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_600ece28345dc_61da1bb70-346
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: base64

UmVjaWJpc3RlIHVuIG51ZXZvIHByZXN1cHVlc3RvIGRlIEfDs21leiBFdWdl
bmlvIQ0KPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT0NCg0KRGV0YWxsZXMgZGVsIHByZXN1cHVlc3RvOg0KLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0NCg0KTm9tYnJlOiBHw7NtZXogRXVnZW5p
bw0KRW1haWw6IGV1Z2VzbWFAZ21haWwuY29tDQpPYnJhOiBMYWdvIEhlcm1v
c28NClRlbMOpZm9ubzogNDIyODYyDQoNCk1lbnNhamU6DQphc2Rhc2QNCg==

----==_mimepart_600ece28345dc_61da1bb70-346
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body class="noselect">
    <img src="cid:[email protected]" width="374" height="80" />
    <p class="body-mail">
        <p>Hi, i'm sending this email to test the header logo.</p>
    </p>
  </body>
</html>
  </body>
</html>

----==_mimepart_600ece28345dc_61da1bb70-346--

----==_mimepart_600ece2835e18_61da1bb70-1e1
Content-Type: image/jpeg;
 filename=encabezado_email.jpg
Content-Transfer-Encoding: base64
Content-Disposition: inline;
 filename=encabezado_email.jpg
Content-ID: <[email protected]>

Solution

  • 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.