Dhalang is a Ruby wrapper for Google's Puppeteer.
- Generate PDFs from pages
- Generate PDFs from html ( external images/stylesheets supported )
- Capture a screenshot of a webpage
Add this line to your application's Gemfile:
gem 'Dhalang'
And then execute:
$ bundle update
Install puppeteer in your application's root directory:
$ npm install puppeteer
NodeJS v7.6.0 or greater is required
Get a PDF of a website url
Dhalang::PDF.get_from_url("https://www.google.com")
It is important to pass the complete url, leaving out https://, http:// or www. will result in an error.
Get a PDF of a HTML string
Dhalang::PDF.get_from_html("<html><head></head><body><h1>examplestring</h1></body></html>")
Get a PNG screenshot of a website
Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
Get a JPEG screenshot of a website
Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
All methods return a string containing the PDF or JPEG/PNG in binary.
To return the PDF from a Rails controller you can do the following:
def example_controller_method
binary_pdf = Dhalang::PDF.get_from_url("https://www.google.com")
send_data(binary_pdf, filename: 'pdfofgoogle.pdf', type: 'application/pdf')
end
To return the PNG from a Rails controller you can do the following:
def example_controller_method
binary_png = Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
send_data(binary_png, filename: 'screenshotofgoogle.png', type: 'image/png')
end
To return the JPEG from a Rails controller you can do the following:
def example_controller_method
binary_jpeg = Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
send_data(binary_jpeg, filename: 'screenshotofgoogle.jpeg', type: 'image/jpeg')
end