使用rmagick将pdf转换为图像会产生黑色背景图像

问题描述

我正在使用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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...