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).
Create a pure PDF using Prawn, You have to do all the formatting using the Prawn API
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
Rails 5 - Using Polymorphic Associations - Rendering the Views
More Concise Version of Max/Min Without the Block
Performing a "Fast-Forward" Merge with Rugged
Why Does the Rand() Return Always the Same Number
Ruby on Rails: Creating a Model Entry with a Belongs_To Association
Remove All Data from Active Storage
Problem Installing Ruby 1.9.2 on MAC Os Lion
How to Write Down the Rspec to Test Rescue Block
Compare the Content, Not the Results, of Procs
What's the Best Way to Return an Enumerator::Lazy When Your Class Doesn't Define #Each
Rmagick Error After Installing Os X Mavericks
Advice on How to Validate Names and Surnames Using Regex
Ruby on Rails How to Deal with Nan
Why Is the << Operation on an Array in Ruby Not Atomic
Ruby Equivalent of C#'s 'Yield' Keyword, Or, Creating Sequences Without Preallocating Memory
If 'Self' Is Always the Implied Receiver in Ruby, Why Doesn't 'Self.Puts' Work