我正在尝试使用wkhtmltopdf渲染我的模型的pdf,其中包含使用dragonfly存储的图像,但我无法使其工作。当我使用wkhtmltopdf的调试选项时,html页面呈现正常,但pdf本身只是得到一个空白框,图像应该在那里。 到目前为止,我只找到了两个提出这个问题的网站(a german blog和a google forum thread),但这两个解决方案都没有为我解决。 我怀疑我应该使用blog中建议的图像文件的绝对路径,但是当我尝试该解决方案时,服务器卡在:
a = [{"name" => "name1", "folder" => "folder1", 'id' => 1 },
{"name" => "name1", "folder" => "folder1", 'id' => 2 },
{"name" => "name1", "folder" => "folder2", 'id' => 3},
{"name" => "name2", "folder" => "folder1", 'id' => 4}]
a.group_by { |c| c['name'] }.map do |key, value|
[key, [value.group_by { |c| c['folder']}]]
end.to_h
=> {"name1"=>
[{"folder1"=>[
{"name"=>"name1", "folder"=>"folder1", "id"=>1},
{"name"=>"name1", "folder"=>"folder1", "id"=>2}],
"folder2"=>[
{"name"=>"name1", "folder"=>"folder2", "id"=>3}]}],
"name2"=>
[{"folder1"=>[
{"name"=>"name2", "folder"=>"folder1", "id"=>4}]}]
}
我的代码如下所示:
image.rb
Rendered images/show.pdf.erb (19.9ms)
"***************[\"/home/oskar/.rbenv/versions/2.2.3/bin/wkhtmltopdf\",
\"-q\", \"--encoding\", \"utf8\", \"--orientation\", \"Landscape\",
\"file:////tmp/wicked_pdf20161007-8095-1nlruhe.html\", \"/tmp
/wicked_pdf_generated_file20161007-8095-1f6vr27.pdf\"]***************"
images_controller.rb:
class Image < ActiveRecord::Base
dragonfly_accessor :image
end
show.pdf.erb:
def show
respond_to do |format|
format.html
format.pdf do
render pdf: 'name',
background: true,
encoding: 'utf8',
orientation: 'Landscape',
:show_as_html => params[:debug].present?
end
end
end
image.url = <% if params[:debug].present? %>
<%= stylesheet_link_tag 'document' %>
<%= stylesheet_link_tag 'pdf' %>
<% else %>
<%= wicked_pdf_stylesheet_link_tag 'document' %>
<%= wicked_pdf_stylesheet_link_tag 'pdf' %>
<% end %>
...
<%= image_tag image.url if image.image_stored? %>
...
任何建议都将不胜感激!
答案 0 :(得分:1)
我知道,我的解决方案很难看。但它有效:)
totalValue = 20 # of 'a'
#OR
totalValue = 50 # of 'b'
#OR
totalValue = 20 # of 'c'
现在我渲染这样的图像:
myFile = open("file.txt", "r")
while True:
theline = myFile.readline()
if "a" in theline: #Just used 'a' here as an example.
for char in theline:
...
myFile.close()
现在,您甚至可以附加def self.export_to(path)
all.each do |image_model|
image_model.image.to_file(Rails.root.join("tmp","image", image.id.to_s))
end
end
回调来删除图片。
答案 1 :(得分:1)
诀窍是使用:image_tag(logo.path)
请注意image_tag
和path
代替wicked_pdf_image_tag
和url
的使用情况。
答案 2 :(得分:0)
您需要将image_tag
替换为wicked_pdf_image_tag
<%= wicked_pdf_image_tag image.url if image.image_stored? %>
答案 3 :(得分:0)
这适用于我使用路径
<img src="<%=Rails.root.join(image.path)%>" >
如果图像对象是孩子
<img src="<%=Rails.root.join(parentObject.image.path)%>" >