问题描述
我正在使用Active Storage上传pdf文件,我需要将其转换为图像并将其另存为附件。下面是我用于此目的的代码,但它为我提供了具有黑色背景的转换图像。
project_file.rb
class ProjectFile < ApplicationRecord
has_many_attached: files
end
converted_image.rb
class ConvertedImage < ApplicationRecord
has_one_attached: image
end
some_controller.rb
def show
pdf = url_for(ProjectFile.last.files.first)
PdfToImage.new(pdf).perform
end
pdf_to_image.rb
class PdfToImage
require 'rmagick'
require 'open-uri'
attr_reader :pdf
def initialize(pdf)
@pdf = pdf
end
def perform
jpg = Magick::ImageList.new(URI.open(pdf).path)
file = Tempfile.new(['converted','.jpg'])
jpg.write(file.path)
image = File.open(file)
ConvertedImage.last.image.attach(io: image,filename: "converted.jpg",content_type: 'application/jpg')
image.close
end
end
有人对此有任何解决方案吗?我是Rmagick的新手。
红宝石“ 2.6.5”
将'6.0.1'设置为
gem'rmagick'
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)