diff --git a/Gemfile b/Gemfile index 5a8f72e..c920a2c 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/README.md b/README.md index a462215..78065ed 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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). @@ -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. @@ -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 diff --git a/grover.gemspec b/grover.gemspec index 60ad719..b87f59d 100644 --- a/grover.gemspec +++ b/grover.gemspec @@ -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 diff --git a/lib/grover/configuration.rb b/lib/grover/configuration.rb index d015f0f..ea71e3a 100644 --- a/lib/grover/configuration.rb +++ b/lib/grover/configuration.rb @@ -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 @@ -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 diff --git a/lib/grover/middleware.rb b/lib/grover/middleware.rb index 0f1f8e6..5129379 100644 --- a/lib/grover/middleware.rb +++ b/lib/grover/middleware.rb @@ -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 @@ -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 diff --git a/lib/grover/processor.rb b/lib/grover/processor.rb index 839d07d..92cf913 100644 --- a/lib/grover/processor.rb +++ b/lib/grover/processor.rb @@ -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 )