簡體   English   中英

Ruby on Rails:Prawn Gem生成PDF,但無法顯示PDF

[英]Ruby on Rails: Prawn Gem Generates PDF, but PDF Cannot Display

我一直在將Ruby on Rails(5.2.1版)和RubyMine IDE一起用於一個項目,並且處於開發階段,我想在其中創建一些基於PDF的報告。 為此,我抓住了Prawn(2.2.2版)寶石。 我設置了一個按鈕,可以從頁面下載PDF:

<%= button_to "Download", action: :download_report %>

它被路由到適當的控制器:

get 'pages#download_report' => 'pages#report_page'
post 'pages#download_report', action: :download_report, controller: 'pages'

在Pages控制器中,download_report方法為:

def download_report
    Prawn::Document.generate("hello.pdf") do
        text "Hello World!"
    end
    send_data("hello.pdf", filename: "hello.pdf", type: 'application/pdf')
end

服務器在開發中運行良好,使用按鈕沒有錯誤。 已發送PDF,並且我可以“打開”它,但是PDF無法加載,並且無法在我嘗試過的任何PDF閱讀器中打開,包括Microsoft Edge和Adobe Acrobat Reader。 我對為什么會發生此錯誤感到困惑,因為看起來這個基本測試應該可以工作。 我對Ruby on Rails還是比較陌生,因此如果它是一個基本錯誤,我不會感到驚訝,但是這並不是我容易看到的錯誤。 任何幫助或建議,不勝感激。

感謝任何提前響應的人! 如果您要我提供更多信息來幫助診斷問題,請告訴我。

send_file更改send_data

 def download_report
    Prawn::Document.generate("hello.pdf") do
      text "Hello World!"
    end
    send_file "hello.pdf",type: 'application/pdf'
end 

我在項目中經常使用大蝦,這是我遵循的配置。 到目前為止還沒有遇到任何問題。

send_data your_file.render, filename: filename, type: "application/pdf"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM