2016-09-29 54 views
0

我有一個應用程序,其中將我的瀏覽器中的Fedex標籤顯示爲PDF。Ruby send_data在瀏覽器中將數組呈現爲PDF

我已經使用send_data呈現每個標籤如下,它完美的作品:

@label_image = Base64.decode64(image_hex).html_safe #image_hex is a text field where I store the label in db 
send_data @label_image, :type => 'application/pdf', :disposition => 'inline', :filename => 'test.pdf' 

但是,我想渲染多個標籤在類似的方式,即我可以有標籤say @labels_images的數組,在一個請求中將它們呈現給瀏覽器。

PDF的每一頁應該有一個標籤。

我該如何使用send_data或其他任何可用的庫來做到這一點?

感謝

回答

0

我結束了使用combine_pdf gem,它可以幫助我合併多個對象,以在瀏覽器中呈現爲PDF。

工作的代碼如下所示:

pdf = CombinePDF.new 
@label_items.each { |item| pdf << CombinePDF.parse(Base64.decode64(item.image_hex).html_safe) } 
send_data pdf.to_pdf, :type => 'application/pdf', :disposition => 'inline', :filename => 'test.pdf' 
1

這聽起來像你需要首先創建多頁PDF,一旦你有,你可以將它傳遞到send_data

紅寶石中有很多pdf庫,在ruby-toolbox上有很好的列表。

或者,如果您不想使用多頁PDF,則可以將多個pdf一起壓縮成一個文件並將其傳遞到send_data

+0

感謝您的答覆,我用combine_pdf寶石得到這個工作! – dp7

相關問題