2012-12-06 67 views
3

我用蝦創建PDF文件,我想把徽標放在文件中。我已經看過這個網站Creating pdf來引用如何添加圖像。添加圖像使用蝦,得到錯誤:蝦:::錯誤:: UnsupportedImageType,PNG使用不受支持的交錯方法

這是我在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 

所以我想問問爲什麼我得到這個錯誤,蝦不支持rt png文件?我怎樣才能把圖像放在pdf文件中?

更新

我用jpgjpeg文件,但它沒有工作過。

回答

2

經過一個小時的搜索谷歌,我還沒有找到問題。然後我嘗試將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圖像文件。

5

PNG有很多種 - 絕大多數會和蝦一起工作,但有一些不會。

最簡單的選擇是將其轉換爲JPG或將其重新保存爲不隔行的PNG。