mileszs / wicked_pdf

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

PDF with carrierwave pictures #384

Closed henjin closed 9 years ago

henjin commented 9 years ago

Hi, im using carrierwave-mongoid with mongoid-grid_fs as uploader for my project, and now im trying insert uploaded pictures in to PDF file, but it doesnt display! I tried to use both: image_tag() and wicked_pdf_image_tag(),

thank you in advance!

im using: Rails 4.1.8 Ruby 2.1.1 carrierwave-mongoid 0.7.1 mongoid-grid_fs 2.1.0 wicked_pdf 0.11.0

unixmonkey commented 9 years ago

WickedPdf will render your html out to a string and save it to a file on the disk, so relative links to assets won't work.

This will not work:

<%= image_tag('foo.png') %>

This should work, but can be slow, since wkhtmltopdf tries to pull the file from the web:

<%= image_tag('http://example.com/images/foo.png') %>

wicked_pdf_image_tag tries to speed things up by referencing the file directly from the filesystem, effectively translating into something like this:

<%= image_tag(Rails.root.join('app', 'assets', 'images', 'foo.png')) %>

If you can make it so that there are absolute references to the files, then it should work.

I see that grid_fs lets you do something like this:

<%= image_tag(src: file.data_uri) %>

What does the html output of that look like? You can use the WickedPdf option debug: true to render an HTML version of the page so you can view the source.

henjin commented 9 years ago

Thanks a lot for tips i just created helper method that worked for me

def pdf_image_tag(img, options = {})
    image_tag Rails.root.join(img), options
end
henjin commented 9 years ago

im really sorry for misleading, in HTML version it worked, with options show_as_html: true, but when i deleted and rendered as PDF it did not display it. To show img in html with gridfs i needed to put this in routes.rb file

get "/upload/grid/*path" => "gridfs#serve"

it uses gridfs serve action

unixmonkey commented 9 years ago

What is the HTML source generated for the image tag with show_as_html: true?

It looks to me like you need to add the full path like:

<%= image_tag("http://example.com/upload/grid/#{foo.png}") %>

Is it rendering without the image, or is it hanging forever?

If it hangs instead of just rendering a pdf without your image, that means you are running Rails in single-threaded mode, so the first request (to the pdf) never finishes because it is waiting for the second (to the asset), but Rails is only serving one request at a time... You'll need to set it up so it can serve multiple requests at once, or work around the issue some other way.

henjin commented 9 years ago

with

= image_tag(@property.first_picture.result.url)

its generating

<img alt="picture" src="/upload/grid/uploads/picture/image/54dcf65568656e777a000000/picture.jpg">

and in html version its shows fine, but when i switch to pdf, then its just rendering without image, when i'm trying your example its hanging out on request and doesnt load anything!

unixmonkey commented 9 years ago

That means it is working, however like I just said above, wkhtmltopdf's request for the asset will never resolve because it has to go through Rails (routes) to be served, and the pdf request will never finish because wkhtmltopdf is waiting for the pdf to finish... This is a deadlock because in dev mode, by default, Rails will only serve 1 request at a time.

If you have a staging environment (or production even), it should work just fine because most production environments are setup to run multiple web workers (with passenger or unicorn or something), but to check this in development mode, you'll probably need to configure your app to run multiple web workers.

I would suggest you add puma to your gemfile, and running your dev server with rails server Puma --workers 3

Let me know how it goes.

henjin commented 9 years ago

Yep, when i add more workers it works like a charm, thank you! i changed helper method to this:

def pdf_image_tag(img, options = {})
    image_tag ["http://", request.host_with_port, img].join(), options
end
numsweb commented 9 years ago

is there anything special that needs to be done when the images come from an https site? my code: <%= debug image_tag(vehicle.avatar1.flyer.url) %> <%= image_tag(vehicle.avatar1.flyer.url) %> and the resulting PDF, the url for the image is correct, but nothing appears in the PDF:

screen shot 2015-03-26 at 5 40 27 pm

unixmonkey commented 9 years ago

@numsweb There were some versions of wkhtmltopdf (in the 12.x version range) that used SSLv3 for HTTPS. Many hosts (including Heroku) disabled SSLv3 to prevent Heartbleed attacks, so things just kind of stopped working when they did that. Newer versions of wkhtmltopdf have addressed the issue.

I recommend updating to the newest wkhtmltopdf version if you can.

numsweb commented 9 years ago

I installed with Rails gem: wkhtmltopdf-binary (0.9.9.3)

which hasn't been updated for about 3 years.

Where do I get the latest version?


John Kropka Software Developer at CabForward http://www.linkedin.com/in/kropka (512) 461-6914 => Mobile Skype => s2000.coder

On Thu, Mar 26, 2015 at 5:48 PM, David Jones notifications@github.com wrote:

@numsweb https://github.com/numsweb There were some versions of wkhtmltopdf (in the 12.x version range) that used SSLv3 for HTTPS. Many hosts (including Heroku) disabled SSLv3 to prevent Heartbleed attacks, so things just kind of stopped working when they did that. Newer versions of wkhtmltopdf have addressed the issue.

I recommend updating to the newest wkhtmltopdf version if you can.

— Reply to this email directly or view it on GitHub https://github.com/mileszs/wicked_pdf/issues/384#issuecomment-86744266.

numsweb commented 9 years ago

Yeah, I had 0.12.1 and the latest is 0.12.2.1, but installing the newer one made no difference.

John


John Kropka Software Developer at CabForward http://www.linkedin.com/in/kropka (512) 461-6914 => Mobile Skype => s2000.coder

On Thu, Mar 26, 2015 at 5:54 PM, John Kropka john@cabforward.com wrote:

I installed with Rails gem: wkhtmltopdf-binary (0.9.9.3)

which hasn't been updated for about 3 years.

Where do I get the latest version?


John Kropka Software Developer at CabForward http://www.linkedin.com/in/kropka (512) 461-6914 => Mobile Skype => s2000.coder

On Thu, Mar 26, 2015 at 5:48 PM, David Jones notifications@github.com wrote:

@numsweb https://github.com/numsweb There were some versions of wkhtmltopdf (in the 12.x version range) that used SSLv3 for HTTPS. Many hosts (including Heroku) disabled SSLv3 to prevent Heartbleed attacks, so things just kind of stopped working when they did that. Newer versions of wkhtmltopdf have addressed the issue.

I recommend updating to the newest wkhtmltopdf version if you can.

— Reply to this email directly or view it on GitHub https://github.com/mileszs/wicked_pdf/issues/384#issuecomment-86744266.

unixmonkey commented 9 years ago

Ok, then that probably wasn't the issue. There was an HTTPs issue patched a few days ago in #394

Maybe try updating your Gemfile to use master:

gem 'wicked_pdf', github: 'mileszs/wicked_pdf'

and see if that helps. If so, I'll cut a new version and push to rubygems.

numsweb commented 9 years ago

well, that broke my code, let me see whats changed...


John Kropka Software Developer at CabForward http://www.linkedin.com/in/kropka (512) 461-6914 => Mobile Skype => s2000.coder

On Thu, Mar 26, 2015 at 6:07 PM, David Jones notifications@github.com wrote:

Ok, then that probably wasn't the issue. There was an HTTPs issue patched a few days ago in #394 https://github.com/mileszs/wicked_pdf/pull/394

Maybe try updating your Gemfile to use master:

gem 'wicked_pdf', github: 'mileszs/wicked_pdf'

and see if that helps. If so, I'll cut a new version and push to rubygems.

— Reply to this email directly or view it on GitHub https://github.com/mileszs/wicked_pdf/issues/384#issuecomment-86749659.

numsweb commented 9 years ago

here is what I'm trying to do (in a model):

view = ActionView::Base.new(ActionController::Base.view_paths.first, {}) view.extend(ApplicationHelper) view.extend(Rails.application.routes.url_helpers) pdf = WickedPdf.new.pdf_from_string( view.render( template: 'alerts/show_print.pdf.erb', locals: { :alert => self }), :layout => 'layouts/pdf.html.erb', :print_media_type => false, :page_size => "A4", :disable_smart_shrinking => true )

and I get: ActionView::Template::Error: undefined method `pathname' for nil:NilClass


John Kropka Software Developer at CabForward http://www.linkedin.com/in/kropka (512) 461-6914 => Mobile Skype => s2000.coder

On Thu, Mar 26, 2015 at 6:07 PM, David Jones notifications@github.com wrote:

Ok, then that probably wasn't the issue. There was an HTTPs issue patched a few days ago in #394 https://github.com/mileszs/wicked_pdf/pull/394

Maybe try updating your Gemfile to use master:

gem 'wicked_pdf', github: 'mileszs/wicked_pdf'

and see if that helps. If so, I'll cut a new version and push to rubygems.

— Reply to this email directly or view it on GitHub https://github.com/mileszs/wicked_pdf/issues/384#issuecomment-86749659.

numsweb commented 9 years ago

gotta call it a night, I'll investigate tomorrow.


John Kropka Software Developer at CabForward http://www.linkedin.com/in/kropka (512) 461-6914 => Mobile Skype => s2000.coder

On Thu, Mar 26, 2015 at 6:54 PM, John Kropka john@cabforward.com wrote:

here is what I'm trying to do (in a model):

view = ActionView::Base.new(ActionController::Base.view_paths.first, {}) view.extend(ApplicationHelper) view.extend(Rails.application.routes.url_helpers) pdf = WickedPdf.new.pdf_from_string( view.render( template: 'alerts/show_print.pdf.erb', locals: { :alert => self }), :layout => 'layouts/pdf.html.erb', :print_media_type => false, :page_size => "A4", :disable_smart_shrinking => true )

and I get: ActionView::Template::Error: undefined method `pathname' for nil:NilClass


John Kropka Software Developer at CabForward http://www.linkedin.com/in/kropka (512) 461-6914 => Mobile Skype => s2000.coder

On Thu, Mar 26, 2015 at 6:07 PM, David Jones notifications@github.com wrote:

Ok, then that probably wasn't the issue. There was an HTTPs issue patched a few days ago in #394 https://github.com/mileszs/wicked_pdf/pull/394

Maybe try updating your Gemfile to use master:

gem 'wicked_pdf', github: 'mileszs/wicked_pdf'

and see if that helps. If so, I'll cut a new version and push to rubygems.

— Reply to this email directly or view it on GitHub https://github.com/mileszs/wicked_pdf/issues/384#issuecomment-86749659.

anaumov commented 5 years ago

You can use image_tag with absolute path

= image_tag(object.image.path)