corrupt PDF attachment using ActionMailer

361 views
Skip to first unread message

Luma

unread,
May 31, 2013, 8:06:02 AM5/31/13
to prawn...@googlegroups.com
Hi,

I can successfully generate and save PDFs using prawn 1.0.0.rc2 and rails 3.2.13. But when I attach such a file to an email and send it via ActionMailer, the attachment is corrupt and can't be opened using Adobe Reader 10.1.7.27. The german error message is: "Beim Öffnen dieses Dokuments ist ein Fehler aufgetreten. Die Datei ist beschädigt und kann nicht repariert werden."

The attachment is not corrupt when I remove all 3 of the following lines:

pdf.font_families.update("SlimbachStd" => font_files)
pdf.image "#{Rails.root}/public/images/Logo_v2.jpg"
pdf.encrypt_document(:owner_password => PDF_CONFIG['encryption_owner_pass'], 
        :permissions => {:print_document => true, :modify_contents => false, :copy_contents => true, :modify_annotations => false})

So how can I make it work?

It also works when I change my mailer to generate the PDF directly instead of reading it from the filesystem. But that's not an option for me because I also have to send PDFs that have previously generated and saved using prawn, and also the request for sending the email takes 1-2 minutes then in development mode!


invoice_pdf.rb:

# encoding: utf-8
require "prawn"
require "prawn/measurement_extensions"

class InvoicePdf
  
  def create_and_save(directory, filename, content_hash)
    pdf = Prawn::Document.new
    
    # prepare the fonts
    font_files = {}
    font_path = "#{Rails.root}/vendor/fonts"
    [:bold, :book, :medium].each do |style|
      font_files[style] = "#{font_path}/SlimbachStd-#{style.to_s.capitalize}.ttf"
    end
    pdf.font_families.update("SlimbachStd" => font_files)    # makes it corrupt
    
    # set the default font
    pdf.font "SlimbachStd", :style => :book
    pdf.font_size 10
    
    # start content
    pdf.image "#{Rails.root}/public/images/Logo_v2.jpg"    # makes it corrupt
    
    pdf.text "Invoice", :size => 30
    pdf.text "signs like äöüßÄÖÜ are working"
    
    # some more content...
    
    # encryption
    pdf.encrypt_document(:owner_password => PDF_CONFIG['encryption_owner_pass'], 
        :permissions => {:print_document => true, :modify_contents => false, :copy_contents => true, :modify_annotations => false})    # makes it corrupt
    
    # save
    FileUtils.makedirs(directory)
    pdf.render_file("#{directory}/#{filename}")
  end
  
end


order_mailer.rb:

class OrderMailer < ActionMailer::Base
  
  def order_confirmation_email(pdf_attachments, time, sender, receivers, cc, bcc, subject, body_hash)
    # template is order_confirmation_email.text.erb
    attach_pdfs(pdf_attachments)
    mail(:date => time, :from => sender, :to => receivers, :cc => cc, :bcc => bcc, :subject => subject)
  end

  def edited_template_email(pdf_attachments, time, sender, receivers, cc, bcc, subject, body_string)
    attach_pdfs(pdf_attachments)
    mail(:date => time, :from => sender, :to => receivers, :cc => cc, :bcc => bcc, :subject => subject) do |format|
      format.text { render :text => body_string, :layout => false }
    end
  end
  
  private

  def attach_pdfs(pdf_attachments)
    pdf_attachments.each do |path_filename, filename_attachment|
      attachments[filename_attachment] = {
        mime_type: 'application/pdf',
        content: File.read(path_filename)
      }
    end
  end

end


gemfile.lock:

GIT
  revision: fba53264342fbfd5069b20c84f45664275c74931
  tag: 1.0.0.rc2
  specs:
    prawn (1.0.0.rc2)
      afm
      pdf-reader (>= 0.9.0, < 2.0)
      ruby-rc4
      ttfunk (~> 1.0.3)

GIT
  revision: 3f27d414bb47e064341af3a623d40ebd2c2b5c6e
  branch: rails-3
  specs:
    validates_date_time (1.0.0)

GEM
  specs:
    Ascii85 (1.0.2)
    actionmailer (3.2.13)
      actionpack (= 3.2.13)
      mail (~> 2.5.3)
    actionpack (3.2.13)
      activemodel (= 3.2.13)
      activesupport (= 3.2.13)
      builder (~> 3.0.0)
      erubis (~> 2.7.0)
      journey (~> 1.0.4)
      rack (~> 1.4.5)
      rack-cache (~> 1.2)
      rack-test (~> 0.6.1)
      sprockets (~> 2.2.1)
    activemodel (3.2.13)
      activesupport (= 3.2.13)
      builder (~> 3.0.0)
    activerecord (3.2.13)
      activemodel (= 3.2.13)
      activesupport (= 3.2.13)
      arel (~> 3.0.2)
      tzinfo (~> 0.3.29)
    activeresource (3.2.13)
      activemodel (= 3.2.13)
      activesupport (= 3.2.13)
    activesupport (3.2.13)
      i18n (= 0.6.1)
      multi_json (~> 1.0)
    afm (0.2.0)
    arel (3.0.2)
    builder (3.0.4)
    certified (0.1.1)
    coffee-rails (3.2.2)
      coffee-script (>= 2.2.0)
      railties (~> 3.2.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.6.2)
    daemons (1.1.9)
    dynamic_form (1.1.4)
    erubis (2.7.0)
    eventmachine (1.0.3)
    eventmachine (1.0.3-x86-mingw32)
    exception_notification (3.0.1)
      actionmailer (>= 3.0.4)
    execjs (1.4.0)
      multi_json (~> 1.0)
    hashery (2.1.0)
    hike (1.2.2)
    i18n (0.6.1)
    journey (1.0.4)
    jquery-rails (2.2.1)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    json (1.8.0)
    mail (2.5.4)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.23)
    multi_json (1.7.3)
    mysql2 (0.3.11)
    mysql2 (0.3.11-x86-mingw32)
    nokogiri (1.5.9)
    nokogiri (1.5.9-x86-mingw32)
    pdf-reader (1.3.3)
      Ascii85 (~> 1.0.0)
      afm (~> 0.2.0)
      hashery (~> 2.0)
      ruby-rc4
      ttfunk
    polyglot (0.3.3)
    rack (1.4.5)
    rack-cache (1.2)
      rack (>= 0.4)
    rack-ssl (1.3.3)
      rack
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (3.2.13)
      actionmailer (= 3.2.13)
      actionpack (= 3.2.13)
      activerecord (= 3.2.13)
      activeresource (= 3.2.13)
      activesupport (= 3.2.13)
      bundler (~> 1.0)
      railties (= 3.2.13)
    railties (3.2.13)
      actionpack (= 3.2.13)
      activesupport (= 3.2.13)
      rack-ssl (~> 1.3.2)
      rake (>= 0.8.7)
      rdoc (~> 3.4)
      thor (>= 0.14.6, < 2.0)
    rake (10.0.4)
    rdoc (3.12.2)
      json (~> 1.4)
    ruby-rc4 (0.1.5)
    sass (3.2.9)
    sass-rails (3.2.6)
      railties (~> 3.2.0)
      sass (>= 3.1.10)
      tilt (~> 1.3)
    sprockets (2.2.2)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    thin (1.5.1)
      daemons (>= 1.0.9)
      eventmachine (>= 0.12.6)
      rack (>= 1.0.0)
    thor (0.18.1)
    tilt (1.4.1)
    treetop (1.4.12)
      polyglot
      polyglot (>= 0.3.1)
    ttfunk (1.0.3)
    tzinfo (0.3.37)
    uglifier (2.1.0)
      execjs (>= 0.3.0)
      multi_json (~> 1.0, >= 1.0.2)

PLATFORMS
  x86-mingw32

DEPENDENCIES
  certified
  coffee-rails (~> 3.2.1)
  dynamic_form
  exception_notification
  jquery-rails
  mysql2
  nokogiri
  prawn!
  rails (= 3.2.13)
  sass-rails (~> 3.2.3)
  thin
  uglifier (>= 1.0.3)
  validates_date_time!

Message has been deleted

Luma

unread,
Jun 1, 2013, 10:03:50 AM6/1/13
to prawn...@googlegroups.com
The problem description above relates to Windows 7 64-bit. It works when I run the app on Ubuntu 12.04 LTS...

Naveed Kakal

unread,
Aug 4, 2017, 1:48:43 PM8/4/17
to Prawn
This is due to Windows - I'm not sure on the why, but to fix it, open your file in `rb` mode, or use `File.binread` when working with attachments and ActionMailer.

In your original code (addition in red):

def attach_pdfs(pdf_attachments)
    pdf_attachments.each do |path_filename, filename_attachment|
      attachments[filename_attachment] = {
        mime_type: 'application/pdf',
        content: File.read(path_filename, 'rb')
      }
    end
  end
Reply all
Reply to author
Forward
0 new messages