4

私は現在 Rails 3.1 を実行しており、PDF 生成には最新バージョンの Wicked_pdf を使用しています。すべてが正しく設定されており、PDF は問題なく生成されます。

ただし、ユーザーがボタンをクリックしてpdfをダウンロードできるようにしたいと考えています。現時点では、クリックすると、ブラウザーは pdf をレンダリングし、ページに表示します。

<%= link_to "Download PDF", { :action => "show", :format => :pdf }, class: 'button nice red large radius', target: '_blank'%>

私のコントローラー。

def show
    @curric = Curric.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @curric }
      format.pdf do
        render :pdf => @curric.name + " CV",
        :margin => {:top                => 20,  
                    :bottom             => 20 }

      end
    end
  end

私はsend_fileを指摘されましたが、このシナリオでそれを使用する方法がまったくわかりません.

どんな助けでも大歓迎です。

4

4 に答える 4

4

私はそのようにしています:

  def show
    @candidate = Candidate.find params[:id]

    respond_to do |format|
      format.html
      format.pdf do
        @pdf = render_to_string :pdf => @candidate.cv_filename,
            :encoding => "UTF-8"
        send_data(@pdf, :filename => @candidate.cv_filename,  :type=>"application/pdf")
      end
    end    

  end

そしてそれは私のために働きます;-)

于 2011-12-15T12:13:26.487 に答える
3

やってみよう:

pdf = render_to_string :pdf => @curric.name + " CV",
                       :margin => {:top     => 20,  
                                   :bottom  => 20 }
send_file pdf
于 2011-10-27T14:56:51.300 に答える