2011-09-01 2 views
5

Résolu, voir modification en bas.Stockez le PDF généré par le système sur S3


Dans mes 3.1 rails App je générer un pdf comme ceci:

def show 
    @contributor = Contributor.find(params[:id]) 

    respond_to do |format| 
    format.pdf { 
    html = render_to_string(:action => "show.html.erb") 
    kit = PDFKit.new(html) 
    kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css" 
    thepdf = send_data kit.to_pdf, :filename => "blah.pdf", :type => 'application/pdf' 
redirect_to :action => save_to_s3  
} 
end 

Je suis en train de magasin qui a généré PDF sur S3 en téléchargeant avec Paperclip:

def save_to_s3 
    html = render_to_string(:action => "show.html.erb") 
     kit = PDFKit.new(html) 
     kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css" 
     roy = Royarchive.new(:client_id => @contributor.client_id) 
     f = File.open("blah.pdf", 'w+') 
     f.write kit.to_pdf 
     roy.pdf = f 
     roy.save! 
end 

Mais cela me donne "\x9C" from ASCII-8BIT to UTF-8

Comment puis-je utiliser Paperclip pour télécharger ce générés pdf à S3? J'utilise Heroku, donc je ne peux pas enregistrer un fichier temporaire sur le serveur, puis le télécharger.

//// résolu

Oh, mon mauvais, vous pouvez store files for the duration of the session at root.

ceci marche:

def show 
    @contributors = Contributor.where(:client_id => current_user.client_id).paginate(:page => params[:page]) 
    @contributor = Contributor.where(:client_id => current_user.client_id).find(params[:id]) 


    respond_to do |format| 
    format.html 
    format.pdf { 
    html = render_to_string(:action => "show.html.erb") 
    kit = PDFKit.new(html) 
    kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css" 
    send_data kit.to_pdf, :filename => "name.pdf", :type => 'application/pdf' 
    @thepdf = kit.to_file("#{Rails.root}/tmp/name.pdf") 

roy = Royarchive.new(:client_id => @contributor.client_id) 
roy.pdf = @thepdf 
roy.save!  
    } 

end 
end 
+0

je vais - vous avez besoin de plus représentant que je ne dois même pas répondre, et encore moins accepter, votre propre Q dans les 8 heures de l'écrire. – snowangel

Répondre

3

Oh, mon mauvais, vous pouvez store files for the duration of the session at root.

ceci marche:

def save_to_s3 
    html = render_to_string(:action => "show.html.erb") 
    kit = PDFKit.new(html) 
    kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css" 
    thepdf = kit.to_file("#{Rails.root}/tmp/name.pdf") 

    roy = Royarchive.new(:client_id => @contributor.client_id) 
    roy.pdf = thepdf 
    roy.save! 
    redirect_to :action => index 
end 
Questions connexes