mileszs / wicked_pdf

PDF generator (from HTML) plugin for Ruby on Rails
http://www.mileszs.com/wicked-pdf-plugin
MIT License
3.54k stars 646 forks source link

Background image used as watermark not working on production #383

Open taniadaniela opened 9 years ago

taniadaniela commented 9 years ago

I'm using a background image into body for watermark, it works perfect in development, but not in production server, this is the style: body.watermark{ background-image: url(<%= asset_data_uri 'mylogo.png' %>); background-repeat:repeat-y; background-position: center 250px; background-attachment:fixed; background-size:100%;

}

the style is into a file.css.erb

I also have tried an absolute path like (also change the image format) background-image: url('images/mylogo.jpg');

I've set the property no-background to false..

Bassically I've tried everything but still not working into production server.

I'm using wkhtmltopdf 0.12.1 (with patched qt), Rails 4.1.0.

unixmonkey commented 9 years ago

wkhtmltopdf will need a full file path or URI, since it needs to gather everything from a static file.

Try with a full filesystem path:

background-image: url('<%= Rails.root.join('public','images','mylogo.jpg') %>');

or a full URI with host and protocol:

background-image: url('http://yourdomain.com/mylogo.jpg');

Or by base64-encoding your assets with a helper like this:

def asset_data_base64(path)
  asset = Rails.application.assets.find_asset(path)
  throw "Could not find asset '#{path}'" if asset.nil?
  base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")
  "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end
background-image: url('<%= asset_data_base64('images/mylogo.jpg') %>');

Let me know how it goes!

taniadaniela commented 9 years ago

@unixmonkey still not working on production server :(

unixmonkey commented 9 years ago

Are you putting quotes inside the url('') declarations? wkhtmltopdf can be picky about that.

taniadaniela commented 9 years ago

yes.. Is working on development, but not in production.. that's the rare part.

unixmonkey commented 9 years ago

Can you use debug: true, and paste the relevant parts of the html source?

unixmonkey commented 9 years ago

@taniadaniela Did you get things working?

guillosaracco commented 7 years ago

This worked for me in Rails 5 - in development and production after I changed my .css file to .css.scss. Hope it helps

background-image: asset-data-url("pdf_watermark.png");
igorgesso commented 3 years ago

This worked for me in Rails 5 - in development and production after I changed my .css file to .css.scss. Hope it helps

background-image: asset-data-url("pdf_watermark.png");

don't forget to reference the html as the .css file. I suffered until I discovered this.