I used Prawn to create pdf file, I want to put the logo in file. I have looked at this site Creating pdf to reference how to add image.
This is my code in ExamResultPdf.rb file to generate 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
But when I go to page /result.pdf, it has error:
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'
My 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
So I want to ask why I got this error, Prawn is not support png file? How can I put image in pdf file?
Update
I have used jpg or jpeg file, but it did not work too.
There are many types of PNG - the vast majority will work with prawn, but a few won't.
The easiest option would be to convert it to a JPG or re-save it as a PNG that isn't interlaced.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With