邪恶的 pdf 没有在行动邮件中显示图像

问题描述

我想在随附的 pdf 中显示标题图像。我正在使用 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") %>

enter image description here

如果直接下载 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

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

enter image description here

图片路径为: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" = $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],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],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],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",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",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],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 = 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,所以如果它最终对您有用,您可能希望将其放在单独的工作中。

告诉我进展如何,或者发布包含其他信息的更新,我会尽力提供帮助。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...