Wicked pdf 不在动作邮件中显示图像

Wicked pdf not showing images in action mailer

我想在随附的 pdf 中显示 header 图片。我使用 wicked_pdf_asset_pack_path 如下:

estimation_pdf.html.erb

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

如果直接下载 pdf,这是可行的,但它不在邮件的附件中。

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 "+@estimation.created_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 "+@estimation.code+" "+@estimation.created_at.strftime("%d/%m/%Y"))
  end
end

当我发送邮件时会发生这种情况:

图片路径为:app/javascript/images/header_lighuen.png

控制台输出:

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" =  LIMIT   [["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" =  LIMIT   [["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" =  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" =  LIMIT   [["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" =  LIMIT   [["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" =  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" =  LIMIT   [["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" =   [["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" =  LIMIT   [["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" =   [["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" =  ORDER BY "variable_configurations"."id" ASC LIMIT   [["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 600ece2838d86_61da1bb700b3@bashud-PC.mail (3867.3ms)
[ActiveJob] [ActionMailer::Parameterized::DeliveryJob] [c25de849-6098-4691-a5fa-b7ec71bfa3b0] Date: Mon, 25 Jan 2021 10:56:56 -0300
From: eugesma@gmail.com
To: eugesma@gmail.com
Message-ID: <600ece2838d86_61da1bb700b3@bashud-PC.mail>
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:600ece2832a1e_61da1bb70-4a@bashud-PC.mail" 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: <600ece2832a1e_61da1bb70-4a@bashud-PC.mail>

我很好奇如果您从控制器渲染还是从电子邮件渲染,最终 PDF 中的 HTML 会为您输出什么:

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

<pre> 标签是特意添加的,因此您可以看到标记,而不是将其呈现为图像。

我也很好奇如果你这样做是否有不同的结果,在将 PDF 传递给你的邮件之前预呈现 PDF,如下所示:

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

当然,这意味着在将 PDF 发送到邮件之前准备好,因此如果它最终对您有用,您可能希望将它放在一个单独的作业中。

让我知道进展如何,或者 post 更新更多信息,我会尽力提供帮助。