我正在創建一個服務,在客戶的pdf檔案頂部添加水印。當我把它添加到pdf源時,html文本加載得很好,但影像卻沒有(顯示了一個小方塊)。
下面是我的一些代碼:
add_online_signature_to_partner_quote_pdf_service.rb:
class AddOnlineSignatureToPartnerQuotePdfService>
def initialize(quote)
@quote = quote
end
def call
pdf = CombinePDF.new
source = CombinePDF.parse(@quote.document.download, { allow_optional_content: true })
signature = CombinePDF.parse(generate_signature(@quote)
pdf <<來源
pdf << 簽名
update_quote_with_signature(@quote, pdf)
結束。
私有
def generate_signature(quote)
project = quote.project
ac = ApplicationController.new
pdf = WickedPdf.new.pdf_from_string(
ac.render_to_string(
template: "advanced_admin/quotes/form/_partner_quote_signature.pdf.erb"/span>。
layout: "pdf.html"。
locals: { quote: quote, project: project, pdf: true }
)
)
end )
def update_quote_with_signature(quote, pdf)
quote.document.attach(
io: StringIO.new(pdf.to_pdf)。
filename: "quote_complete.pdf"/span>。
content_type: "application/pdf"end[/span]]
end end
_partner_quote_signature.pdf.erb:
<div>
<h1>我的標題</h1>
<%= wicked_pdf_image_tag "logo-blue.png" %>
<p>我的段落</p>。
</div>
我想在我的PDF中生成的圖片經典地位于我的資產管道中app/assets/images/logo-blue.png
我嘗試了很多不同的語法,我在其他主題上看到的,但似乎沒有什么作業...。wkhtmltopdf-binary "寶石的版本是0.12.6.5
。你知道我如何能使我的代碼作業嗎?非常感謝!
uj5u.com熱心網友回復:
正如他們在檔案中所說的那樣 :
wkhtmltopdf二進制檔案是在您的Rails應用程式之外運行的;因此,您的正常布局將無法作業。如果你打算使用任何CSS、JavaScript或影像檔案,你必須修改你的布局,以便為這些檔案提供一個絕對參考。對于沒有資產管道的Rails來說,最好的選擇是使用wicked_pdf_stylesheet_link_tag、wicked_pdf_image_tag和wicked_pdf_javascript_include_tag幫助器,或者直接使用CDN(內容交付網路)來獲取jQuery等流行庫。
使用wicked_pdf_helpers和asset pipeline會導致傳遞給helpers的資產名稱不應該包括"/assets/"前綴,錯誤。為了解決這個問題,你可以使用wicked_pdf_asset_base64和普通的Rails助手,但要注意,這將對你的內容進行base64編碼,并在頁面中行內。這對于小型資產來說是非常快的,但是大型資產可能需要很長時間。
所以你可以這樣做 :
<%= image_tag wicked_pdf_asset_base64("logo-blue.png"/span>), height:/span> 300 %>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/322773.html
標籤:
