-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of PDF processor Added console to expedite development
- Loading branch information
Showing
15 changed files
with
255 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
*.gem | ||
Gemfile.lock | ||
package-lock.json | ||
/node_modules | ||
/coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
Layout/DotPosition: | ||
EnforcedStyle: trailing | ||
|
||
Metrics/LineLength: | ||
Max: 120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,56 @@ | ||
[![Travis Build Status](https://img.shields.io/travis/Studiosity/grover.svg?style=flat)](https://travis-ci.org/Studiosity/grover) | ||
[![Maintainability](https://api.codeclimate.com/v1/badges/37609653789bcf2c8d94/maintainability)](https://codeclimate.com/github/Studiosity/grover/maintainability) | ||
[![Test Coverage](https://api.codeclimate.com/v1/badges/37609653789bcf2c8d94/test_coverage)](https://codeclimate.com/github/Studiosity/grover/test_coverage) | ||
[![Gem Version](https://img.shields.io/gem/v/grover.svg?style=flat)](#) | ||
|
||
# Grover | ||
|
||
A Ruby gem to transform HTML into PDFs using Google Puppeteer | ||
A Ruby gem to transform HTML into PDFs using [Google Puppeteer](https://github.com/GoogleChrome/puppeteer) | ||
and [Chromium](https://www.chromium.org/Home). | ||
|
||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
```ruby | ||
gem 'grover' | ||
``` | ||
|
||
### Google Puppeteer | ||
```bash | ||
npm install puppeteer | ||
``` | ||
|
||
|
||
## Usage | ||
```ruby | ||
# Grover.new accepts a URL and optional parameters for `puppeteer` | ||
grover = Grover.new('https://google.com', page_size: 'A4') | ||
|
||
# Get an inline PDF | ||
pdf = grover.to_pdf | ||
|
||
``` | ||
|
||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/Studiosity/grover. | ||
|
||
Note that spec tests are appreciated to minimise regressions. Before submitting a PR, please ensure that: | ||
|
||
```bash | ||
$ rspec | ||
``` | ||
and | ||
|
||
```bash | ||
$ rubocop | ||
``` | ||
both succeed | ||
|
||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'bundler/setup' | ||
require 'grover' | ||
|
||
require 'irb' | ||
IRB.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'grover/version' | ||
|
||
require 'grover/utils' | ||
require 'grover/processor' | ||
require 'grover/grover' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# | ||
# Grover interface for converting HTML to PDF | ||
# | ||
class Grover | ||
# | ||
# @param [String] url URL of the page to convert | ||
# @param [Hash] options Optional parameters to pass to PDF processor | ||
# see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions | ||
# | ||
def initialize(url, options = {}) | ||
@url = url | ||
@options = options | ||
end | ||
|
||
# | ||
# Request URL with provided options and create PDF | ||
# | ||
# @param [String] path Optional path to write the PDF to | ||
# @return [Array<Integer>] Byte array of the resulting PDF | ||
# | ||
def to_pdf(path = nil) | ||
options = @options.dup | ||
options[:path] = path if path | ||
result = Grover::Processor.new(root_path).convert_pdf(@url, options) | ||
result['data'].pack('c*') | ||
end | ||
|
||
def inspect | ||
format( | ||
'#<%<class_name>s:0x%<object_id>p @url="%<url>s">', | ||
class_name: self.class.name, | ||
object_id: object_id, | ||
url: url | ||
) | ||
end | ||
|
||
private | ||
|
||
def root_path | ||
File.expand_path(__dir__) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'schmooze' | ||
|
||
class Grover | ||
# | ||
# Processor helper class for calling out to Puppeteer NodeJS library | ||
# | ||
class Processor < Schmooze::Base | ||
dependencies puppeteer: 'puppeteer' | ||
|
||
method :convert_pdf, Utils.squish(<<-FUNCTION) | ||
async (url, options) => { | ||
let browser; | ||
try { | ||
browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
await page.goto(url, { waitUntil: 'networkidle2' }); | ||
return await page.pdf(options); | ||
} finally { | ||
if (browser) { | ||
await browser.close(); | ||
} | ||
} | ||
} | ||
FUNCTION | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Grover | ||
# | ||
# Utility class for Grover helper methods | ||
# | ||
class Utils | ||
def self.squish(string) | ||
string. | ||
gsub(/\A[[:space:]]+/, ''). | ||
gsub(/[[:space:]]+\z/, ''). | ||
gsub(/[[:space:]]+/, ' ') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
class Grover | ||
VERSION = '0.0.1'.freeze | ||
VERSION = '0.1.0'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "grover", | ||
"version": "0.1.0", | ||
"description": "A Ruby gem to transform HTML into PDFs using Google Puppeteer/Chromium", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Studiosity/grover.git" | ||
}, | ||
"keywords": [ | ||
"PDF", | ||
"puppeteer", | ||
"chromium" | ||
], | ||
"author": "[email protected]", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Studiosity/grover/issues" | ||
}, | ||
"homepage": "https://github.com/Studiosity/grover#readme", | ||
"devDependencies": { | ||
"puppeteer": "^1.7.0" | ||
}, | ||
"dependencies": { | ||
"logging": "^3.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'spec_helper' | ||
|
||
describe Grover do | ||
describe '.new' do | ||
subject(:new) { Grover.new('http://google.com') } | ||
|
||
it { is_expected.to be_a Grover } | ||
it { expect(subject.instance_variable_get('@url')).to eq 'http://google.com' } | ||
it { expect(subject.instance_variable_get('@options')).to eq({}) } | ||
|
||
context 'with options passed' do | ||
subject(:new) { Grover.new('http://happyfuntimes.com', page_size: 'A4') } | ||
|
||
it { expect(subject.instance_variable_get('@url')).to eq 'http://happyfuntimes.com' } | ||
it { expect(subject.instance_variable_get('@options')).to eq(page_size: 'A4') } | ||
end | ||
end | ||
|
||
describe '#to_pdf' do | ||
subject(:to_pdf) { Grover.new('https://www.google.com').to_pdf } | ||
|
||
it { is_expected.to start_with "%PDF-1.4\n" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'spec_helper' | ||
|
||
describe Grover::Utils do | ||
describe '.squish' do | ||
subject(:squish) { described_class.squish string } | ||
|
||
context 'with an empty string' do | ||
let(:string) { '' } | ||
|
||
it { is_expected.to eq '' } | ||
end | ||
|
||
context 'with leading spaces' do | ||
let(:string) { ' Foo' } | ||
|
||
it { is_expected.to eq 'Foo' } | ||
end | ||
|
||
context 'with trailing spaces' do | ||
let(:string) { 'Bar ' } | ||
|
||
it { is_expected.to eq 'Bar' } | ||
end | ||
|
||
context 'with spaces in the middle' do | ||
let(:string) { 'Foo Bar Baz' } | ||
|
||
it { is_expected.to eq 'Foo Bar Baz' } | ||
end | ||
|
||
context 'with newlines' do | ||
let(:string) { "\nFoo\nBar Baz\nBoop\n" } | ||
|
||
it { is_expected.to eq 'Foo Bar Baz Boop' } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'simplecov' | ||
SimpleCov.start | ||
|
||
$LOAD_PATH.unshift File.expand_path('../lib', __dir__) | ||
require 'grover' | ||
|
||
RSpec.configure do |config| | ||
config.order = 'random' | ||
end |