【发布时间】:2019-05-07 00:07:34
【问题描述】:
我已经为此工作了一段时间并做了很多谷歌搜索,但似乎无法弄清楚我哪里出错了。 (我这周接管了一个已有 2 年历史的网站,所以我仍在努力寻找解决办法,但无济于事)。
上传文件时,会创建缩略图预览图像。对于图像文件,创建此预览没有问题,但对于 PDF,我想将默认图像设置为缩略图,而不是 PDF 的预览。
在我的 DocumentUploader 类中,我有以下设置图像缩略图(我没有包含一些不相关的位):
def convert_to_image(height, width)
image = MiniMagick::Image.open(current_path)
image.resize "#{height}x#{width}"
image.write(current_path)
end
version :preview, if: :not_svg? do
process convert_to_image: [210, 297]
process convert: :jpg
def full_filename(for_file = model.file)
super.chomp(File.extname(super)) + ".jpg"
end
end
def not_svg?(new_file)
!new_file.content_type.start_with? "image/svg+xml"
!new_file.content_type.start_with? "application/pdf"
end
以此为基础,我为 PDF 编写了以下内容(同样,不相关部分除外):
def set_default_pdf(height, width)
image = MiniMagick::Image.open("app/assets/images/pdf_placeholder.svg")
image.resize "#{height}x#{width}"
image.write("app/assets/images/pdf_placeholder.svg")
end
version :preview, if: :pdf? do
process set_default_pdf: [210, 297]
process convert: :jpg
def full_filename(for_file = model.file)
super.chomp(File.extname(super)) + ".jpg"
end
end
def pdf?(new_file)
new_file.content_type.start_with? "application/pdf"
end
我的观点如下:
<%= link_to document.file.url, target: "_blank" do %>
<%= image_tag document_preview_url(document.file) if document.file? %>
<% end %>
所以我遵循了我的 DocumentPreviewHelper 模块,该模块具有以下内容:
def document_preview_url(file)
return file.preview.url(response_content_type: %( "image/jpeg" )) if file.preview.present?
file.url(response_content_type: %( "image/svg+xml"))
end
与cmets:
# Retrieve a preview thumbnail for a PDF with the correct content type set.
#
# Without overriding the content_type, the headers would return 'application/pdf'
# for the preview image, and all browsers except for Safari will show the image
# (regardless of the content type header). This fixes the preview thumbs for Safari.
我觉得这是我应该进行更改的地方,但根据我(有限的)知识,在我看来应该显示 PDF 预览,因为它们已在 DocumentUploader 中转换为 jpg。
我尝试了各种不同的调整和更改,但 PDF 预览仍然是断开的链接。我错过了什么/我哪里出错了?我觉得这应该是一个简单的解决方案,但我只是没有看到它,并且沮丧正在开始。
【问题讨论】:
标签: ruby-on-rails ruby pdf