邪恶的 PDF - 图像不显示
Wicked PDF - images are not showing
我正在使用 ruby 3.0.0 和 rails 6.1.3.2。我研究了很多示例,但还没有找到最终在 PDF 中显示图像的解决方案。 PDF 呈现(CSS 也在工作),但图像显示为 empty/not 已找到。不过,如果我去他们的URL,我可以看到他们。
澄清一下,certificate
属于 certificate_template
和 has_one_attached :pdf
。 Acertificate_template
has_one_attached :brand_logo, :signature
.
# controllers/admin/certificates_controller.rb
def view_pdf
@certificate = Certificate.find(params[:certificate_id])
if !@certificate.pdf.attached?
pdf = WickedPdf.new.pdf_from_string(
render_to_string(template: 'admin/certificates/view_pdf.html.erb', layout: 'application.pdf.erb')
)
@certificate.pdf.attach(io: StringIO.new(pdf), filename: "#{@certificate.path.title} - #{@certificate.user.name}.pdf", content_type: 'application/pdf')
end
respond_to do |format|
format.html
format.pdf do
render pdf: url_for(@certificate.pdf), content_type: 'application/pdf', template: 'admin/certificates/view_pdf.html.erb', page_size: 'A4', layout: 'application.pdf.erb', orientation: 'Landscape', zoom: 1, dpi: 75, disposition: 'inline'
end
end
end
# views/admin/certificates/view_pdf.html.erb
<div class="template-frame">
<div class="template-text">
<br /><br />
<%# Brand logo %>
<% if @certificate.certificate_template.brand_logo.attached? %>
<%= wicked_pdf_image_tag url_for(@certificate.certificate_template.brand_logo), width: '400px' %>
<% elsif current_tenant && current_tenant.has_custom_domain? && @theme %>
<%= wicked_pdf_image_tag url_for(@theme.brand_logo), width: '400px' if @theme.brand_logo.attached? %>
<% end %>
<br /><br /><br /><br />
<%# Title and Date %>
<div class="title-date">
<h3 class="certificate-title serif">Certificate of Completion</h3>
<p id="template-date" class=<%= "my-0 #{'hide-element' unless @certificate.certificate_template.show_date}" %>>was awarded on <%= Date.today.strftime("%m/%d/%Y") %> to</p>
</div>
<%# Name of user and course %>
<div class="user-course">
<div class="user-hr">
<h3 class="serif name"><%= @certificate.user.name %></h3>
<hr>
</div>
<br />
<p class="for-completing">for completing the course</p>
<h3 class="serif course-name"><%= @certificate.path.title %></h3>
<p class=<%= "#{'hide-element' unless @certificate.certificate_template.show_course_description} course-description" %>>
<%= @certificate.certificate_template.course_description %>
</p>
</div>
<%# Signature %>
<br /><br /><br /> <br />
<div class="template-signature" class=<%= "hide-element" unless @certificate.certificate_template.show_signature %> >
<% if @certificate.certificate_template.signature.attached? %>
<%= wicked_pdf_image_tag url_for(@certificate.certificate_template.signature), width: '400px' %>
<hr>
<p class="serif"><%= @certificate.certificate_template.printed_name %></p>
<p><%= @certificate.certificate_template.printed_position %></p>
<% end %>
</div>
</div>
</div>
# views/layouts/application.pdf.erb
<!DOCTYPE html>
<html>
<head>
<title>Certificate</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= wicked_pdf_stylesheet_link_tag "pdf" %>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
在尝试了各种解决方案后,以下是对我有用的方法:
<%= wicked_pdf_image_tag(polymorphic_url(@certificate.certificate_template.brand_logo), width: "400px") %>
我正在使用 ruby 3.0.0 和 rails 6.1.3.2。我研究了很多示例,但还没有找到最终在 PDF 中显示图像的解决方案。 PDF 呈现(CSS 也在工作),但图像显示为 empty/not 已找到。不过,如果我去他们的URL,我可以看到他们。
澄清一下,certificate
属于 certificate_template
和 has_one_attached :pdf
。 Acertificate_template
has_one_attached :brand_logo, :signature
.
# controllers/admin/certificates_controller.rb
def view_pdf
@certificate = Certificate.find(params[:certificate_id])
if !@certificate.pdf.attached?
pdf = WickedPdf.new.pdf_from_string(
render_to_string(template: 'admin/certificates/view_pdf.html.erb', layout: 'application.pdf.erb')
)
@certificate.pdf.attach(io: StringIO.new(pdf), filename: "#{@certificate.path.title} - #{@certificate.user.name}.pdf", content_type: 'application/pdf')
end
respond_to do |format|
format.html
format.pdf do
render pdf: url_for(@certificate.pdf), content_type: 'application/pdf', template: 'admin/certificates/view_pdf.html.erb', page_size: 'A4', layout: 'application.pdf.erb', orientation: 'Landscape', zoom: 1, dpi: 75, disposition: 'inline'
end
end
end
# views/admin/certificates/view_pdf.html.erb
<div class="template-frame">
<div class="template-text">
<br /><br />
<%# Brand logo %>
<% if @certificate.certificate_template.brand_logo.attached? %>
<%= wicked_pdf_image_tag url_for(@certificate.certificate_template.brand_logo), width: '400px' %>
<% elsif current_tenant && current_tenant.has_custom_domain? && @theme %>
<%= wicked_pdf_image_tag url_for(@theme.brand_logo), width: '400px' if @theme.brand_logo.attached? %>
<% end %>
<br /><br /><br /><br />
<%# Title and Date %>
<div class="title-date">
<h3 class="certificate-title serif">Certificate of Completion</h3>
<p id="template-date" class=<%= "my-0 #{'hide-element' unless @certificate.certificate_template.show_date}" %>>was awarded on <%= Date.today.strftime("%m/%d/%Y") %> to</p>
</div>
<%# Name of user and course %>
<div class="user-course">
<div class="user-hr">
<h3 class="serif name"><%= @certificate.user.name %></h3>
<hr>
</div>
<br />
<p class="for-completing">for completing the course</p>
<h3 class="serif course-name"><%= @certificate.path.title %></h3>
<p class=<%= "#{'hide-element' unless @certificate.certificate_template.show_course_description} course-description" %>>
<%= @certificate.certificate_template.course_description %>
</p>
</div>
<%# Signature %>
<br /><br /><br /> <br />
<div class="template-signature" class=<%= "hide-element" unless @certificate.certificate_template.show_signature %> >
<% if @certificate.certificate_template.signature.attached? %>
<%= wicked_pdf_image_tag url_for(@certificate.certificate_template.signature), width: '400px' %>
<hr>
<p class="serif"><%= @certificate.certificate_template.printed_name %></p>
<p><%= @certificate.certificate_template.printed_position %></p>
<% end %>
</div>
</div>
</div>
# views/layouts/application.pdf.erb
<!DOCTYPE html>
<html>
<head>
<title>Certificate</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= wicked_pdf_stylesheet_link_tag "pdf" %>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
在尝试了各种解决方案后,以下是对我有用的方法:
<%= wicked_pdf_image_tag(polymorphic_url(@certificate.certificate_template.brand_logo), width: "400px") %>