当前位置 : 主页 > 编程语言 > ruby >

ruby-on-rails – 使用Prawn添加图像,得到错误:Prawn :: Errors :: UnsupportedImageType,PNG使

来源:互联网 收集:自由互联 发布时间:2021-06-23
我用Prawn创建了pdf文件,我想将徽标放在文件中.我已经看过这个网站 Creating pdf,以参考如何添加图像. 这是我在ExamResultPdf.rb文件中生成pdf的代码: def initialize(exam, view) super(top_margin: 70) @
我用Prawn创建了pdf文件,我想将徽标放在文件中.我已经看过这个网站 Creating pdf,以参考如何添加图像.

这是我在ExamResultPdf.rb文件中生成pdf的代码:

def initialize(exam, view)
    super(top_margin: 70)
    @exam = exam
    exam_number # line 9, I removed codes are not relevant
  end

  def exam_number
    logo_path = "#{Rails.root}/app/assets/images/logo_hoasen.png"
    image logo_path, height: 200, width: 200
    text "Examination Result \##{@exam.id}", size: 30, style: :bold
  end

但是当我去页面/result.pdf时,它有错误:

Prawn::Errors::UnsupportedImageType in ExamsController#result
PNG uses unsupported interlace method

# Trace
app/pdfs/exam_result_pdf.rb:18:in `exam_number'
app/pdfs/exam_result_pdf.rb:9:in `initialize'
app/controllers/exams_controller.rb:43:in `new'
app/controllers/exams_controller.rb:43:in `block (2 levels) in result'
app/controllers/exams_controller.rb:40:in `result'

我的考试控制器:

def result
    @exam = Exam.find(params[:id])
    @general_exam = GeneralExam.where(id: @exam.general_exam_id).first
    @topic_questions = TopicQuestion.where(general_exam_id: @general_exam.id)
    @exam_result = ExamResult.where(exam_id: @exam.id, user_id: @exam.user_id).first

    respond_to do |format|
      format.html
      format.pdf do
        pdf = ExamResultPdf.new(@exam, view_context) # line 43
        send_data pdf.render, filename: "exam_result_#{@exam.id}",
                                  type: "application/pdf",
                           disposition: "inline"
      end
    end
  end

所以我想问为什么我得到这个错误,Prawn不支持png文件?如何将图像放入pdf文件?

更新

我使用过jpg或jpeg文件,但它也没有用.

有许多类型的PNG – 绝大多数都会与虾一起工作,但有些不会.

最简单的选择是将其转换为JPG或将其重新保存为非隔行扫描的PNG.

网友评论