EzDevInfo.com

pdfkit

A Ruby gem to transform HTML + CSS into PDFs using the command-line utility wkhtmltopdf

pdfkit command failed: "/usr/bin/wkhtmltopdf" "--page-size" "Letter" ".... "--quiet" "-" "-"

i'm using pdfkit for generating pdf when i generate pdf it gives me following error.

command failed: "/usr/bin/wkhtmltopdf" "--page-size" "Letter" 
"--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" 
"0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" 
"--quiet" "-" "-"

Any help?

  • Ateq

Source: (StackOverflow)

PDFKit doesn't display custom font

I am trying to use a custom font in a pdf I generate from html with PDFKit in my Rails app. I added the font in ...app/assets/fonts/ and included it in my html template:

css:
  @font-face {
    font-family: 'journal';
    src: url(file://#{Rails.root.join('app', 'assets', 'fonts', 'journal.eot')});
    src: url(file://#{Rails.root.join('app', 'assets', 'fonts', 'journal.woff')} format("woff")),
         url(file://#{Rails.root.join('app', 'assets', 'fonts', 'journal.ttf')}) format("truetype");
  }

called it in the css:

h1 {font-family: journal; }

Source: (StackOverflow)

Advertisements

Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?

Background

After reading around it seemed to me that Prawn is out and wkhtmltopdf is in. It also seems like the PDFKit and wicked_pdf gems for Rails are the new cool. So I found a screencast by Ryan on how to use PDFKit. I installed everything, tested wkhtmltopdf on the CLI with no problems, fiddled around with Rails settingsto run multiple processes so the asset pipeline works, and all seemed good, except I'm still stuck at the very end of the process (actually getting the PDF response from the server).

Question

When I request a .pdf version of my view (I'm using the PDFKit Middleware option) my browser just sits there waiting for a response, but as soon as I kill the Rails process the PDF I expected to get only then pops up in my browser window. What gives?

What I'm Using

  • OS: OSX 10.8.1
  • Rails: 3.2.8
  • Ruby: 1.9.3
  • wkhtmltopdf: 0.11.0_rc1 (although when I run wkhtmltopdf -V it says 0.10.0_rc2)
  • qt: 4.8.2

What I've Done

  • used the PDFKit middleware by loading config.middleware.use "PDFKit::Middleware" in my application.rb file.
  • included gem 'pdfkit' in my Gemfile and installed it with Bundler
  • set the .pdf mime-type in my mime_types.rb initializer with Mime::Type.register_alias "application/pdf", :pdf
  • added config.threadsafe! to config/environments/development.rb for multiple threads so asset pipeline doesn't conflict with PDF engine
  • tested wkhtmltopdf http://www.google.com google.pdf and it generated a PDF of the Google homepage as expected
  • tried swapping PDFKit for wicked_pdf and encountered the same problem (hanging, but when Rails process is killed the PDF renders as expected)

What it Looks Like

This is the regular html page rendered by Rails (I've blurred the client details): enter image description here

This is the CLI output by Rails when I try to navigate to localhost:3000/some/path.pdf. (the app hangs while waiting for a response): enter image description here

When I finally kill the Rails process with ctrl-c the PDF finally shows up in the browser as I expected to see it (CSS and HTML rendered properly, so assets seem to load fine): enter image description here

Conclusions So Far

Swapping PDFKit for wicked_pdf and getting the same results seems to make me think the problem isn't with those libraries, but something to do with my development environment. But wkhtmltopdf runs fine off the command line, so that makes me think that it and QT are doing their job. The problem must be in Rails. Maybe I'm not configuring something properly?

Plea for Help

How do I determine what exactly the problem is and how do I fix it?

I'll love you if you can help me <3

Update

I've also tried using an alternative method of rendering the PDF (with .to_pdf) without the middleware option as follows (doing this I commented out config.middleware.use "PDFKit::Middleware" from my application.rb file):

respond_to do |format|
    format.html
    format.pdf do
        html = '<html><body>This is a test.</body></html>'
        @pdf = PDFKit.new(html)

        send_data @pdf.to_pdf, 
            :filename => 'whatever.pdf', 
            :type => 'application/pdf', 
            :disposition => 'attachment'
    end
end

Source: (StackOverflow)

How to avoid page break inside table row for wkhtmltopdf

I am generating pdf report from html page with one table.

I am using wkhtmltopdf for the purpose.

when pdf is generated it breaks anywhere in tr tag.

I want to avoid it .


Source: (StackOverflow)

How can I add page numbers to PDFKit generated PDFs?

I have multiple pages generated using PDFKit. How can I add page numbers to the bottom?


Source: (StackOverflow)

PDFKIT apparently not working because of "wkhtmltopdf"

I'm having trouble generating a PDF out of a html page.

I'm using PDFkit. In the process of installing it, I noticed I needed wkhtmltopdf. So I installed it too. I did everything PDFkit's documentation said to do.. and now I'm getting this error when I try to load the PDF.

Here is the error:

command failed: "/usr/local/bin/wkhtmltopdf" "--margin-right" "0.75in" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-bottom" "0.75in" "--encoding" "UTF-8" "--margin-left" "0.75in" "--quiet" "-" "-"

Any idea of what this could be?

Thanks


Source: (StackOverflow)

Wkhtmltopdf Characters in single line partially cut between pages

I am working in a project using ruby on rails(3.1). My requirement is to produce pdf from the html content. So I use pdfkit gem.

In some pages, characters in single line partially cut between pages. When I convert html convert to pdf using pdfkit gem

version of wkhtmltopdf: wkhtmltopdf -- 0.11.0 rc1

operating system: Linux CentOS 5.5

In the image below showing character partially cut between pages.

Please suggest a solution.

Example 1

enter image description here

Example 2

enter image description here


Source: (StackOverflow)

pdfkit does not style pdfs

I have a rails 3.1 app that creates pdf documents using pdfkit, and everything works as specified, except for the fact that the generated pdfs don't have any styling. I am assuming that wkhtmltopdf doesn't have access to my stylesheets and that it is not a larger issue than that. Would anyone have a clue as to how you would allow access to these stylesheets? I have basically followed railscast #220 on the subject, however I have had to create a new initializer to get pdfkit to work with rails 3.1.

This is the initializer that I had to use to get pdfkit to work with rails 3.1

ActionController::Base.asset_host = Proc.new { |source, request|
  if request.env["REQUEST_PATH"].include? ".pdf"
    "file://#{Rails.root.join('public')}"
  else
    "#{request.protocol}#{request.host_with_port}"
  end
 } 

The link to the pdf looks like this:

<%= link_to 'Download PDF', load_path(@load, :format => "pdf") %>

This will give me a link to the pdf that has no styling.

In my application.rb I have configured pdfkit as such:

config.middleware.use PDFKit::Middleware, :print_media_type => true

I have also added this to my layouts/application.html.erb file:

<%= stylesheet_link_tag    "application", :media => "all" %>

Source: (StackOverflow)

render_to_string does not find partials (PDFKit controller response)

Ruby 1.8.7, Rails 3.0.4, PDFKit 0.5.0

I'm trying to create a PDF with PDFKit without using the middleware so I can disable javascript (there's an accordion action in there that hides a lot of info that should be on the PDF). However, whenever I try, it fails because it says the partials in my view (show.html.erb) are missing:

Missing partial programs/details with {:locale=>[:en, :en], :formats=>[:pdf], :handlers=>[:erb, :rjs, :builder, :rhtml, :rxml]}

If I remove the references to the partials, it works fine. I've also tried putting the partials in the same directory with show.html.erb to no avail. Here is the code in my controller's show action:

respond_to do |format| 
  format.html # show.html.erb 
  format.pdf {
    html = render_to_string(:template => "show.html.erb")
    kit = PDFKit.new(html, :disable_javascript => true )
    send_data(kit.to_pdf, :filename => "test_pdf", :type => "application/pdf", :disposition => 'attachment')
  }
end

Is there any way to do this and keep the partials?

EDIT: for now I've done this:

# config/initializers/pdfkit.rb
PDFKit.configure do |config|
  config.default_options = {
    :page_size => 'Legal',
    :print_media_type => true,
    :disable_javascript => true
  }
end

This has the disadvantage of turning off javascript for every PDF I generate, but it'll do for now. Any answers on the original question of getting the partials to work still with render_to_string still appreciated.


Source: (StackOverflow)

Rails PDFKit command failed

I'm trying to use PDFKit as middleware in a rails 3 app.

I can use wkhtmltopdf from the command line just fine, but my app keeps throwing me this error

command failed: "/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" "--quiet" "-" "-"

If i run this in terminal, it waits for my input, so I type some HTML, then press Ctrl-d and it spits what appears to be some PDF... but no luck in rails.

Here's what I have:

application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'pdfkit'
Bundler.require(:default, Rails.env) if defined?(Bundler)

module Mpr
  class Application < Rails::Application

    YEARS_ARRAY =  (2006..2012).map {|y| [y,y]}.unshift(["Year",nil])
    MONTHS_ARRAY = (1..12).map{|m| [ Date::MONTHNAMES[m], m]}.unshift(["All months",nil])
    config.middleware.use "PDFKit::Middleware", :print_media_type => true
    PDFKit.configure do |config|
      config.wkhtmltopdf = '/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf'
    end
  end
end

in my controller (first line)

respond_to :html, :pdf

I think I've gone through all threads on SO, Github and Google, but no luck.

Can anyone help or point me in the right direction?

thanks, P.


Source: (StackOverflow)

Rails 3.1 asset pipeline with PDFKit

I am using PDFkit with rails 3.1. In the past I was able to use the render_to_string function and create a pdf from that string. I then add the stylesheets as follows. My issue is that I have no idea how to access them from within the asset pipeline. (This is how I did it in rails 3.0)

html_string = render_to_string(:template => "/faxes/show.html.erb", :layout => 'trade_request')
kit = PDFKit.new(html_string, :page_size => 'Letter')
kit.stylesheets << "#{Rails.root.to_s}/public/stylesheets/trade_request.css"

So my question in how do i get direct access from my controller to my css file through the asset pipline?

I know I can use the Rack Middleware with PDFkit to render the pdf to the browser, but in this case i need to send the pdf off to a third party fax service.

Thanks for your help.

Ryan


Source: (StackOverflow)

Python Django PDFKIT - [Errno 9] Bad file descriptor

I use pdfkit and wkhtmltopdf to generate pdf documents. When i generate the first pdf all is well. When i quickly (within 5 seconds) generate an other i get the error [Errno 9] Bad file descriptor. If i close the error (step back in browser) and open again, it will create the pdf.

my views.py

config = pdfkit.configuration(wkhtmltopdf='C:/wkhtmltopdf/bin/wkhtmltopdf.exe')
pdfgen = pdfkit.from_url(url, printname, configuration=config)
pdf = open(printname, 'rb')

response = HttpResponse(pdf.read())
response['Content-Type'] = 'application/pdf'
response['Content-disposition'] = 'attachment ; filename =' + filename
pdf.close()
return response

Maybe important note: i run this site on IIS8, when running from commandline (python manage.py runserver) the error is not present.

Any guidelines on how to handle this error would be great.


Source: (StackOverflow)

wicked_pdf does not run on Ubuntu server : wkhtmltopdf: cannot connect to X server

I'm trying to use wicked_pdf on my prod server but it keeps failling :

RuntimeError (Failed to execute:
"/usr/bin/wkhtmltopdf" -q        "file:////tmp/wicked_pdf20130709-23109-1adqx5g.html" "/tmp/wicked_pdf_generated_file20130709-23109-1ic5dbe.pdf"
Error: PDF could not be generated!
 Command Error: wkhtmltopdf: cannot connect to X server
):
  app/controllers/contrats_controller.rb:15:in `block (2 levels) in show'
  app/controllers/contrats_controller.rb:11:in `show'

I tried to follow this answer : wkhtmltopdf: cannot connect to X server but it still does not work.


Source: (StackOverflow)

Save PDF file shown by PDFKit middleware

If somebody is interested in saving the PDF file in the filesystem which is shown by PDFKit middleware gem, then here it is...

  1. Override the call method of middleware.rb file.
  2. In overriding just replace this line:

    body = PDFKit.new(translate_paths(body, env), @options).to_pdf
    

    with

    pdf = PDFKit.new(translate_paths(body, env), @options)
    file = pdf.to_file('Your/file/name/path')
    Mymodel.my_method()     #You can write your method here to use that file
    body = pdf.to_pdf   #Here you can change the response body
    

You can also override the response-body and content-type if you don't want to give pdf response. If you have any further query then go ahead.

This procedure is really help full because when you are having heavy JavaScript an CSS in your view file then render_to_string method will not work i.e. it will not render the heavy JavaScript.


Source: (StackOverflow)

pdfkit with google web fonts

I'm trying to create a pdf with PDFKit from a webpage with custom Google Web fonts.

I can't seem to get the custom fonts to work properly.
I'm using the default PDFKit middleware for Rails, as shown here.

Can someone please show me a .html file and the (preferably ruby) code that converts it to a pdf using PDFKit or just wkhtmltopdf. The converted pdf should have pretty fonts :)


Source: (StackOverflow)