Wicked_pdf Error: Unable to create PDF file

Gemfile

gem "wicked_pdf" gem "wkhtmltopdf-binary" 

mistake:

 RuntimeError in CarsController#show Failed to execute: /usr/bin/wkhtmltopdf --print-media-type -q - - Error: PDF could not be generated! Rails.root: /u/apps/zeepauto/autozeep_update 

cars_controller

 def show @class_showcar = true @class_admin = true @car = Car.find(params[:id]) @search = Car.search(params[:search]) @cars_see_special = Car.where(:special => "1").order('rand()').limit(3) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @car } format.pdf do render :pdf => "#{@car.carname.name}", :print_media_type => true end end end 

show.html.erb

 <p class="show_links"><%= link_to url_for(request.params.merge(:format => :pdf)) do %> <%= image_tag('/images/printversion.png', :alt => 'Download') %> </p> 

wicked_pdf.erb

 # config/initializers/wicked_pdf.rb WickedPdf.config = { # :exe_path => '/var/lib/gems/1.8/bin/wkhtmltopdf' :exe_path => '/usr/bin/wkhtmltopdf' } 
+7
source share
4 answers

I had wkhtmltopdf-binary already in the gemfile , but since it worked on my local computer and not on the server, I left this error to support the server support group .. they checked the path to wkhtmltopdf, they tried to convert plain html to pdf, and it worked. Therefore, they tried to run the bundle update command, after which the PDF conversion worked fine on the server as well. My gem path has changed, and I think it was a problem. I posted my solution if anyone else has this problem.

+8
source

I had the same problem. The solution was to add wkhtmltopdf-binary to the gem file and run bundle install .

 gem "wicked_pdf" gem "wkhtmltopdf-binary" 
+19
source

I have the same problem. I had wkhtmltopdf-binary installed and bundle update didn't help either wkhtmltopdf-binary . Here is what helped me:

The important thing is that I run it on Alpine Linux, and it is not supported by gem wkhtmltopdf_binary_gem https://github.com/zakird/wkhtmltopdf_binary_gem/issues/53

I installed separately wkhtmltopdf in the system: apk add wkhtmltopdf

And then edited the initializer to include the binary path:

 # config/initializers/wicked_pdf.rb require "wicked_pdf" WickedPdf.config = { exe_path: ENV["WKHTMLTOPDF_BIN"] } 
+1
source

The wkhtmltopdf binary is available for Alpine 3. 9+, but I get either a blank PDF or the error “Could not load the document” - despite the fact that everything works fine on MacOSX. Turns out you need to explicitly enable fonts for alpine builds (at least)

Controller action

 def show respond_to do |format| format.html do render 'pdfs/templates/my_template.html.erb' end format.pdf do render( pdf: "file_name", template: 'pdfs/templates/my_template.html.erb', disposition: 'inline' ) end end end 

The above worked locally on a MacOSX computer, but the document failed to load the document on the ruby ​​alpine image server as shown below

Dockerfile

 FROM ruby:2.6.3-alpine3.10 .... # add wkhtmltopdf for use with wicked_pdf gem RUN apk --no-cache add wkhtmltopdf ... 

even simpler example failed with empty pdf

 respond_to do |format| format.pdf do pdf = WickedPdf.new.pdf_from_string('TESTING 123') send_data( pdf, filename: "file_name.pdf", type: 'application/pdf', disposition: 'inline' ) end end 

Decision

Dockerfile

 FROM ruby:2.6.3-alpine3.10 .... # add wkhtmltopdf for use with wicked_pdf gem RUN apk --no-cache add \ ttf-ubuntu-font-family \ wkhtmltopdf ... 

Ideally, Alpine should include the base font with the wkhtmltopdf package, but until then I found it to be a useful source of information and / or a good way to add a multi-stage Docker file.

https://github.com/madnight/docker-alpine-wkhtmltopdf/blob/master/Dockerfile

Note:

The lack of an explicit font package in alpine can also lead to a failure of the PDF conversion using libreoffice . We found corrupted PDF files, in particular, when converting from docx files.

0
source

All Articles