【问题标题】:Add image using Prawn, got error: Prawn::Errors::UnsupportedImageType, PNG uses unsupported interlace method使用 Prawn 添加图像,出现错误:Prawn::Errors::UnsupportedImageType, PNG using unsupported interlace method
【发布时间】:2012-12-06 09:59:44
【问题描述】:

我用 Prawn 创建 pdf 文件,我想把 logo 放在文件里。我查看了这个网站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'

我的exams_controller

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 is not support png file?如何将图像放入 pdf 文件中?

更新

我使用过jpgjpeg 文件,但也没有用。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 prawn


    【解决方案1】:

    PNG 有很多种类型 - 绝大多数都适用于对虾,但也有少数不会。

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

    【讨论】:

    【解决方案2】:

    google了一个小时,没找到问题。然后我尝试将 png 文件转换为 jpg 文件。我使用Online Image Converter 将 png 转换为 jpg 文件。图片转换后,我用的是jpg文件:

    logo = "#{Rails.root}/app/assets/images/logo_hoasen.jpg"
    image logo, width: 240, height: 75, position: :left
    

    现在它可以正常工作了。看来问题是因为我的 png 图像文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-06
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多