Skip to content

Commit

Permalink
Make combine_pdf dependency optional + support Yarn PnP (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
le0pard authored Nov 19, 2024
1 parent 3cb9fac commit 3e72dd2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ source 'https://rubygems.org'
gemspec

gem 'childprocess', '~> 5.0'
gem 'combine_pdf', '~> 1.0'
gem 'mini_magick', '~> 4.12'
gem 'pdf-reader', '~> 2.11'
gem 'puma', '~> 6.4'
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ Grover.configure do |config|
end
```

#### Yarn PnP strategy

If you are using the Yarn PnP strategy, you can override the run JS runtime for grover:

```ruby
Grover.configure do |config|
config.js_runtime_bin = ['yarn', 'node']
end
```

## Middleware
Grover comes with a middleware that allows users to get a PDF, PNG or JPEG view of
any page on your site by appending .pdf, .png or .jpeg/.jpg to the URL.
Expand All @@ -324,7 +334,7 @@ To enable them, there are configuration options for each image type as well as a

If either of the image handling middleware options are enabled, the [ignore_path](#ignore_path) and/or
[ignore_request](#ignore_request) should also be configured, otherwise assets are likely to be handled
which would likely result in 404 responses.
which would likely result in 404 responses.

```ruby
# config/initializers/grover.rb
Expand Down Expand Up @@ -401,7 +411,7 @@ end
```

### allow_file_uris
The `allow_file_uris` option can be used to render an html document from the file system.
The `allow_file_uris` option can be used to render an html document from the file system.
This should be used with *EXTREME CAUTION*. If used improperly it could potentially be manipulated to reveal
sensitive files on the system. Do not enable if rendering content from outside entities
(user uploads, external URLs, etc).
Expand Down Expand Up @@ -438,6 +448,8 @@ You can specify relative paths to the cover page contents using the `front_cover
options either via the global configuration, or via meta tags. These paths (with query parameters) are then
requested from the downstream app.

Note, to use this functionality you need to add the [combine_pdf](https://rubygems.org/gems/combine_pdf) gem to your app.

The cover pages are converted to PDF in isolation, and then combined together with the original PDF response,
before being returned back up through the Rack stack.

Expand Down Expand Up @@ -559,7 +571,7 @@ The middleware and HTML preprocessing components were used heavily in the implem

Thanks are also given to the excellent [Schmooze project](https://github.com/Shopify/schmooze).
The Ruby to NodeJS interface in Grover is heavily based off that work. Grover previously used that gem,
however migrated away due to differing requirements over persistence/cleanup of the NodeJS worker process.
however migrated away due to differing requirements over persistence/cleanup of the NodeJS worker process.

## License

Expand Down
3 changes: 1 addition & 2 deletions grover.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'combine_pdf', '~> 1.0'
spec.add_dependency 'nokogiri', '~> 1.0'
spec.add_dependency 'nokogiri', '~> 1'
end
6 changes: 4 additions & 2 deletions lib/grover/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class Grover
class Configuration
attr_accessor :options, :meta_tag_prefix, :ignore_path, :ignore_request,
:root_url, :use_pdf_middleware, :use_png_middleware,
:use_jpeg_middleware, :node_env_vars, :allow_file_uris
:use_jpeg_middleware, :js_runtime_bin,
:node_env_vars, :allow_file_uris

def initialize
def initialize # rubocop:disable Metrics/MethodLength
@options = {}
@meta_tag_prefix = 'grover-'
@ignore_path = nil
Expand All @@ -18,6 +19,7 @@ def initialize
@use_pdf_middleware = true
@use_png_middleware = false
@use_jpeg_middleware = false
@js_runtime_bin = ['node']
@node_env_vars = {}
@allow_file_uris = false
end
Expand Down
9 changes: 7 additions & 2 deletions lib/grover/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'combine_pdf'

class Grover
#
# Rack middleware for catching PDF requests and returning the upstream HTML as a PDF
Expand Down Expand Up @@ -132,12 +130,19 @@ def create_grover_for_response(response) # rubocop:disable Metrics/AbcSize
end

def add_cover_content(grover)
load_combine_pdf
pdf = CombinePDF.parse grover.to_pdf
pdf >> fetch_cover_pdf(grover.front_cover_path) if grover.show_front_cover?
pdf << fetch_cover_pdf(grover.back_cover_path) if grover.show_back_cover?
pdf.to_pdf
end

def load_combine_pdf
require 'combine_pdf'
rescue ::LoadError
raise Grover::Error, 'Please add/install the "combine_pdf" gem to use the front/back cover page feature'
end

def fetch_cover_pdf(path)
temp_env = env.deep_dup
scrub_env! temp_env
Expand Down
2 changes: 1 addition & 1 deletion lib/grover/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def convert(method, url_or_html, options)
def spawn_process
@stdin, @stdout, @stderr, @wait_thr = Open3.popen3(
Grover.configuration.node_env_vars,
'node',
*Grover.configuration.js_runtime_bin,
File.expand_path(File.join(__dir__, 'js/processor.cjs')),
chdir: app_root
)
Expand Down

0 comments on commit 3e72dd2

Please sign in to comment.