Wicked-PDF 不显示图像,'wicked_pdf_image_tag' 未定义 [英] Wicked-PDF not showing images, 'wicked_pdf_image_tag' undefined
问题描述
我想生成一个带有我们部门标志的 PDF.当我尝试在控制器中使用 WickedPdf 类时(使用 https://github.com/mileszs/wicked_pdf 中描述的方法)):
def some_actionimage_tag_string = image_tag('logo.jpg')pdf = WickedPdf.new.pdf_from_string(image_tag_string)save_path = Rails.root.join('testpdfs','logotest.pdf')File.open(save_path, 'wb') do |file|文件<
...应用程序将 PDF 保存到目标目录,但它有一个蓝白色的?"标记图像的位置.
如果我这样做:
image_tag_string = wicked_pdf_image_tag('logo.jpg')pdf = WickedPdf.new.pdf_from_string(image_tag_string)
我收到以下错误:
NoMethodError:#<...的未定义方法`wicked_pdf_image_tag'
看起来我的 Rails 应用程序也丢失/没有链接到属于 wicked-pdf gem 的帮助文件.
对 StackOverflow 上类似问题的回答建议编写一个自定义的图像标签"助手来定位图像或安装 wkhtmltopdf.对我来说,图像标签在放置在视图(whatever.html.erb)中时可以很好地显示徽标.logo.jpg"已位于资产管道和#{RailsRoot}/public/images 中.最后,我在 Ubuntu 14.04 上使用 wkhtmltopdf 0.9.9、wicked-pdf 0.11.0 和 rails 4.
简而言之 - 我做错了什么导致 WickedPDF 无法渲染图像?
首先创建一个 pdf 模板以在该模板中呈现和使用您的 wicked_pdf 标签.例如-
app/views/layout/application.pdf.erb-
<头><meta charset='utf-8'/>头部><body onload='number_pages'><div id="内容"><%=产率%>
</html>
app/views/pdf/pdf_view.pdf.erb-
<%= wicked_pdf_image_tag 'logo.jpg' %>
改用这个模板
def 保存pdf = WickedPdf.new.pdf_from_string(渲染到字符串(模板:'example/pdf_view.pdf.erb',布局:'layouts/application.pdf.erb'))发送数据(pdf,文件名:'file_name.pdf',类型:'应用程序/pdf',处置:'附件')结尾
这可能对你有帮助..
I want to generate a PDF with our department logo in it. When I try to use the WickedPdf class in my controller (using the method described at https://github.com/mileszs/wicked_pdf):
def some_action
image_tag_string = image_tag('logo.jpg')
pdf = WickedPdf.new.pdf_from_string(image_tag_string)
save_path = Rails.root.join('testpdfs','logotest.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
end
...the application saves the PDF to the target directory, but it has a blue-and-white '?' mark where the image should be.
If I do this instead:
image_tag_string = wicked_pdf_image_tag('logo.jpg')
pdf = WickedPdf.new.pdf_from_string(image_tag_string)
I get the following error:
NoMethodError:
undefined method `wicked_pdf_image_tag' for #<...
It would appear that my Rails app is also missing / not linking to a helper file belonging to the wicked-pdf gem.
Answers to similar questions on StackOverflow recommend writing a custom "image-tag" helper to locate the image or installing wkhtmltopdf. For me, image-tag shows the logo just fine when placed in a View (whatever.html.erb). "logo.jpg" is already located in both the asset pipeline and #{RailsRoot}/public/images. Finally, I am using wkhtmltopdf 0.9.9, wicked-pdf 0.11.0, and rails 4 on Ubuntu 14.04.
In a nutshell - what am I doing wrong that causes WickedPDF to fail to render the image?
First thing create a pdf template to render and use your wicked_pdf tags in that template.. for example-
app/views/layout/application.pdf.erb-
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body onload='number_pages'>
<div id="content">
<%= yield %>
</div>
</body>
</html>
app/views/pdf/pdf_view.pdf.erb-
<div>
<%= wicked_pdf_image_tag 'logo.jpg' %>
</div>
use this template instead
def save
pdf = WickedPdf.new.pdf_from_string(
render_to_string(
template: 'example/pdf_view.pdf.erb',
layout: 'layouts/application.pdf.erb'))
send_data(pdf,
filename: 'file_name.pdf',
type: 'application/pdf',
disposition: 'attachment')
end
This might help you..
这篇关于Wicked-PDF 不显示图像,'wicked_pdf_image_tag' 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!