Rails 3 and PDFkit. How to Specify Page Size

How to set pdf page-width dynamically in pdfkit,rb (pdfkit gem)

https://github.com/pdfkit/pdfkit#usage

Create instance with params:

kit = PDFKit.new(html, page_width: '169.33')

Setting header size in PDFKit with Ruby on Rails?

I ended up adding this code

PDFKit.configure do |config|
config.default_options = {
:page_size => 'Letter',
:margin_top => '3.6in',
:margin_right => '0.3in',
:margin_bottom => '1.3in',
:margin_left => '0.3in'
}
end

to /config/initializers/pdfkit.rb. Worked like charm after I restarted the server.

How to Specify Font-Size for PDF report Rails

The gem uses the tool wkhtmltopdf, and merely passes in options that that tool will accept. Since it's a HTML to PDF sort of tool, you should specify the font size using CSS.

Rails: How to print model's details to A4 PDF page?

Basically there is two options: (any PDF creation requires a gem - no default PDF creation for rails).

  1. Create a pure PDF using Prawn, You have to do all the formatting using the Prawn API

  2. Create a HTML version of your receipt and convert it to PDF, One of the better gems to do this is PDFkit. which uses a web-kit powered browser engine.

They both work good, For one page documents I usually use PDFkit to convert HTML and for larger documents that are going have lots of pages I use Prawn because it gives you a smaller file size and handles multiple pages better.

My suggestion would be to make a HTML receipt and display it on the screen and give the user an option to save a PDF version using PDFkit.

EDIT: windows install. (not tested - windows and I have parted company.)

Download the windows installer for wkhtmltopdf: win-wkhtmltopdf

now create an initializer file, e.g. config/intializers/pdfkit_config.rb

in pdfkit_config.rb set the absolute path to wkhtmltopdf on your local machine:

PDFKit.configure do |config|
if RAILS_ENV == 'development'
config.wkhtmltopdf = 'D:\path\to\your\wkhtmltopdf' #this bit i'm not sure about
else
config.wkhtmltopdf = "#{RAILS_ROOT}/lib/wkhtmltopdf"
end
end

for your production ENV you can actually just have a copy of wkhtmltopdf in you repo, a unix version of course. (remember to chmod +x it before you git add it)

How to install PDFKit? (Rails 3.2.1 and PDFKit 0.5.2)

Ok so,

Took a few hours of googling but I finally found the solution in an other stackoverflow question:
pdfkit not rendering correctly in rails 3.1

Thank you for helping.

How to print tilde characters correctly with PDFKit?

Make sure you are setting the encoding on your HTML to UTF-8. It is probably trying to interpret it as US-ASCII

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />


Related Topics



Leave a reply



Submit