问题描述:

I need to use an image as a watermark on various documents I'm generating and converting to PDFs with wicked-pdf. It took a while, but I have virtually everything working. My show.pdf.erb is:

<!DOCTYPE html>

<html>

<head >

<style type="text/css">

<%= Rails.application.assets.find_asset('wicked.css').to_s.html_safe %>

</style>

</head>

<body class='wicked'>

<div>

<p><%= "#{Doc.last.contents} ".html_safe %></p>

</div>

</body>

</html>

My wicked.css is:

.wicked {

background-image:url(/assets/test2.jpg);

background-repeat: no-repeat;

background-position: 50% -15%;

}

And my controller simply calls:

 render :pdf => "test2",

:formats => [:pdf],

:show_as_html => true

All this works fine. The watermark and the text pulled from the db present properly. However, when I remove the "show_as_html" from the controller, the watermark disappears and only the text displays as a PDF. Similarly, if I add "save_to_file", the file saves with only text and no watermark. I'm baffled as to what I might be doing wrong. I can't find any errors in any logs. Thanks for any suggestions.

网友答案:

I solved this by using the SASS asset-data-urlrelative-asset-path, maybe you can use something like this:

background-image: asset-data-url('test2.png')
网友答案:

This was solved in the WickedPdf Github issue tracker:

https://github.com/mileszs/wicked_pdf/issues/302

The basic issue here is that url(/assets/test2.jpg) is a relative path, when WickedPdf needs an absolute path, and the contents should be quoted like so:

url('file:///home/unconundrum/code/my_app/app/assets/images/test2.jpg')

Which can be done in a more portable way like this:

background-image:url('<%= "file://#{Rails.root.join('app','assets','images','test2.jpg')}" %>');

with an ERB stylesheet.

相关阅读:
Top