我已经使用主动存储上传pdf文件,我需要将它转换成图像,并将其保存为活动存储的附件。当我使用这段代码时,我使用了这里建议的代码,How to convert PDF files to images using RMagick and Ruby
project_file.rb
class ProjectFile < ApplicationRecord
has_many_attached: files
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'
attr_reader :pdf
def initialize(pdf)
@pdf = pdf
end
def perform
Magick::ImageList.new(pdf)
end
end当我试图执行时,它给了我这个错误。
no data returned `http://localhost:3001/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d3dc048a53b43337dc372b3845901a7912391f9e/MA42.pdf' @ error/url.c/ReadURLImage/247
可能我的代码有问题,所以有人建议我做错了什么,或者我的问题有什么更好的解决方案。
ruby '2.6.5‘
钢轨'6.0.1‘
宝石'rmagick‘
发布于 2020-08-12 18:24:29
根据rmagick文档,imagelist不支持转换图像的urls。您需要使用open-uri、gem和URI.open方法来打开PDF并将其传递给映像列表。
pdf_to_image.rb
class PdfToImage
require 'rmagick'
require 'open-uri'
attr_reader :pdf
def initialize(pdf)
@pdf = pdf
end
def perform
Magick::ImageList.new(URI.open(@pdf).path)
end
endhttps://stackoverflow.com/questions/63370212
复制相似问题