chromium-pdf 1.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e69272be5a3fdd74fe47708fbd938dd4970abb1e5e665eacbbd4c6f16c048052
4
+ data.tar.gz: d8cc59b9a10b591286c184cd94afb9b3e8f08311909d6b0cb71e98e9a1b11489
5
+ SHA512:
6
+ metadata.gz: c00facb71c117edd74b88472cc2c8d136d913e2ab91c92fc21d9b2888740b4f5d5f9fa7986850eadc18c6ff84da121956a79be18a2089fef2e79839d0465a7fb
7
+ data.tar.gz: 042e68a56a4f7c09822827428fcfce1bee747ac7f7c916cba23f26ded142a4d3245619131e3de67f6731e2712337faa13ff59da8a5b33fe39561e50747f39ee2
data/.rubocop.yml ADDED
@@ -0,0 +1,78 @@
1
+ AllCops:
2
+ Exclude:
3
+ - bin/**/*
4
+ - db/**/*
5
+ - vendor/**/*
6
+ - config/**/*
7
+ - tmp/**/*
8
+ - env.rb
9
+ NewCops: enable
10
+
11
+ Metrics:
12
+ Severity: refactor
13
+
14
+ # Hard to make this pass always and want to keep flexibility
15
+ Layout/ArrayAlignment:
16
+ Enabled: false
17
+
18
+ # Best Practices for readability
19
+ Layout:
20
+ Severity: refactor
21
+
22
+ Layout/ArgumentAlignment:
23
+ EnforcedStyle: with_fixed_indentation
24
+
25
+ Layout/FirstArrayElementIndentation:
26
+ EnforcedStyle: consistent
27
+
28
+ Layout/FirstHashElementIndentation:
29
+ EnforcedStyle: consistent
30
+
31
+ Layout/MultilineMethodCallIndentation:
32
+ EnforcedStyle: indented
33
+
34
+ Layout/MultilineOperationIndentation:
35
+ EnforcedStyle: indented
36
+
37
+ Lint/UnusedMethodArgument:
38
+ Severity: refactor
39
+ AutoCorrect: false
40
+
41
+ Lint/UnusedBlockArgument:
42
+ Severity: refactor
43
+ AutoCorrect: false
44
+
45
+ Lint/UselessAssignment:
46
+ Severity: refactor
47
+ AutoCorrect: false
48
+
49
+ Lint/UnderscorePrefixedVariableName:
50
+ Enabled: false
51
+
52
+ Lint/Loop:
53
+ Enabled: false
54
+
55
+ Lint/SuppressedException:
56
+ Enabled: false
57
+
58
+ Naming/BlockForwarding:
59
+ EnforcedStyle: explicit
60
+
61
+ Style:
62
+ Severity: refactor
63
+
64
+ Style/ClassAndModuleChildren:
65
+ Enabled: false
66
+
67
+ Style/Documentation:
68
+ Enabled: false
69
+
70
+ Style/HashAsLastArrayItem:
71
+ Enabled: false
72
+
73
+ Style/HashSyntax:
74
+ Enabled: false
75
+
76
+ # It is ok to use inject at times
77
+ Style/EachWithObject:
78
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-08-14
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Andy Cohen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Chromium::Pdf
2
+
3
+ A gem wrapping the Chrome print-to-pdf functionality used for creating pdf files.
4
+
5
+ It includes generators for establishing a template framework for implementing the generation into GoodJobs.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+ ```ruby
11
+ gem 'chromium-pdf', github: 'RoleModel/chromium-pdf'
12
+ ```
13
+
14
+ And then execute:
15
+ ```shell
16
+ bundle
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Run the installation generator to create dependency templates with:
22
+ ```shell
23
+ bin/rails g chromium:pdf:install
24
+ ```
25
+
26
+ This will create an example job file, GoodJobs initializer, and app.json.
27
+
28
+ ## Contributing
29
+
30
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RoleModel/chromium-pdf.
31
+
32
+ ## License
33
+
34
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,39 @@
1
+ # Print Landscape
2
+
3
+ It is possible to dynamically configure a web page to be printed in landscape orientation using scss.
4
+
5
+ Example:
6
+
7
+ ```scss
8
+ @page landscape {
9
+ size: 11in 8.5in;
10
+ }
11
+
12
+ .print-landscape {
13
+ page: landscape;
14
+ .app__content {
15
+ max-width: 100%;
16
+ }
17
+ }
18
+ ```
19
+
20
+ This class can then be added conditionally to the body of the application view:
21
+
22
+ ```slim
23
+ body.app-body class="#{'print-landscape' if @print_landscape}"
24
+ = turbo_frame_tag 'modal'
25
+ = render 'confirm'
26
+ /...
27
+ ```
28
+
29
+ And this can then be controlled by passing the `@print_landscape` variable from any controller.
30
+
31
+ ## Scaling
32
+
33
+ Printing in landscape orientation this way effectively scales up the web page compared to portrait orientation. This can be rectified with the following class:
34
+
35
+ ```scss
36
+ .landscape-scale {
37
+ zoom: 0.77; // Letter size difference, 8.5in / 11in = 77%
38
+ }
39
+ ```
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chromium
4
+ module Pdf
5
+ VERSION = '1.0.0.pre'
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'railtie' if defined?(Rails::Railtie)
4
+ require_relative 'pdf/version'
5
+ require 'active_support/concern'
6
+
7
+ module Chromium
8
+ module Pdf
9
+ extend ActiveSupport::Concern
10
+
11
+ ##
12
+ # @param unescaped_filename [String] The filename to save the PDF as.
13
+ # @param print_url [String] The URL of the page you want to be processed.
14
+ # @param arguments [Array<String>] An array of arguments to pass to the Chrome binary.
15
+ # @yield [file, filename] Yields the file object and the filename to the block.
16
+ #
17
+ def generate_pdf!(unescaped_filename, print_url, arguments: ['--headless --disable-gpu'])
18
+ chrome_path = ENV.fetch('GOOGLE_CHROME_BIN', nil)
19
+ filename = unescaped_filename.gsub('&', 'and')
20
+
21
+ Dir.mktmpdir do |path|
22
+ filepath = "#{path}/#{filename}"
23
+
24
+ chrome_print chrome_path, print_url, filename, filepath, arguments
25
+ end
26
+ end
27
+
28
+ protected
29
+
30
+ def chrome_print(chrome_path, print_url, filename, filepath, arguments)
31
+ system("LD_PRELOAD='' #{chrome_path} --print-to-pdf='#{filepath}' #{arguments.join(' ')} #{print_url}")
32
+
33
+ File.open(filepath) do |file|
34
+ yield file, filename
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module Chromium
6
+ module Pdf
7
+ class Railtie < ::Rails::Railtie
8
+ generators do
9
+ require 'generators/chromium/pdf/install/install_generator'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+ require 'rails/generators/active_record'
5
+
6
+ module Chromium
7
+ module Pdf
8
+ module Generators
9
+ class InstallGenerator < ::Rails::Generators::Base
10
+ desc 'Create template files for pdf generation job'
11
+ source_root File.expand_path('templates', __dir__)
12
+
13
+ def create_pdf_job_templates
14
+ copy_file 'app/jobs/generate_pdf_job.rb'
15
+ copy_file 'config/initializers/good_job.rb'
16
+ end
17
+
18
+ def include_chrome_buildpack_in_app_json
19
+ say(<<~SAY, :yellow)
20
+ N.B. app.json changes are only applied to new heroku apps.
21
+ In the case where your app already exists make sure to manually add the buildpack by running the command:
22
+ `heroku buildpacks:add heroku-community/chrome-for-testing`
23
+ SAY
24
+ add_buildpack_to_app_json
25
+ end
26
+
27
+ private
28
+
29
+ def add_buildpack_to_app_json
30
+ in_root do
31
+ update_json_file('app.json') do |data|
32
+ if data['buildpacks'].none? { |entry| entry['url'] == 'heroku-community/chrome-for-testing' }
33
+ data['buildpacks'] << { 'url' => 'heroku-community/chrome-for-testing' }
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def update_json_file(file_name)
40
+ data = if File.exist?(file_name)
41
+ ActiveSupport::JSON.decode(File.read(file_name))
42
+ else
43
+ {}
44
+ end
45
+ data['buildpacks'] ||= []
46
+
47
+ yield data
48
+
49
+ File.write(file_name, pretty_json(data))
50
+ end
51
+
52
+ def pretty_json(data)
53
+ require 'json'
54
+ JSON.pretty_generate(data)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ class GeneratePdfJob < ApplicationJob
4
+ JobTimeoutError = Class.new(StandardError)
5
+ include GoodJob::ActiveJobExtensions::Concurrency
6
+ include Chromium::Pdf
7
+
8
+ queue_as :pdf
9
+ retry_on Errno::ENOENT, wait: :polynomially_longer, attempts: 3
10
+ retry_on JobTimeoutError, wait: :polynomially_longer, attempts: 3
11
+
12
+ good_job_control_concurrency_with(
13
+ enqueue_limit: 1,
14
+ perform_limit: 1,
15
+ key: arguments.first.id
16
+ )
17
+
18
+ around_perform do |_job, block|
19
+ Timeout.timeout(180.seconds, JobTimeoutError) do
20
+ block.call
21
+ end
22
+ end
23
+
24
+ def perform(model)
25
+ # Any pre-generation logic
26
+ return if model.attachment.attached?
27
+
28
+ generate_pdf!(model.filename, model.print_url) do |file, filename|
29
+ # What to do with the generated file
30
+ model.attachment.attach(io: File.open(file.path), filename: filename, content_type: 'application/pdf')
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # NOTE: queues - Configuring queues is an important part of prioritization. GoodJob uses
4
+ # multiple ways to override. 12factor override used here with fallback to a common queue
5
+ # override example/pattern for Heroku apps using pdf generation limiting to serial
6
+ # processing to constrain resource [memory] limitations
7
+
8
+ # NOTE: execution_mode - development defaults to async but has known to hang in certain
9
+ # [unidentified] situations so use external which gives devs more control on running too
10
+
11
+ Rails.application.configure do
12
+ config.good_job = {
13
+ smaller_number_is_higher_priority: true, # new default in V4 and for all of ActiveJob
14
+ queues: ENV.fetch('GOOD_JOB_QUEUES', 'pdf:1;-pdf'), # see note above
15
+ execution_mode: Rails.env.test? ? :inline : :external # see note above
16
+ # enable_cron: true,
17
+ # cron: {
18
+ # <cron_label>: {
19
+ # cron: 'every friday at 12pm EST',
20
+ # class: '<class_name>'
21
+ # }
22
+ # }
23
+ }
24
+ end
@@ -0,0 +1,6 @@
1
+ module Chromium
2
+ module Pdf
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chromium-pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - RoleModel Software
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Add support for generating PDFs with chrome to a rails app
14
+ email:
15
+ - it-support@rolemodelsoftware.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - examples/print_landscape.md
26
+ - lib/chromium/pdf.rb
27
+ - lib/chromium/pdf/version.rb
28
+ - lib/chromium/railtie.rb
29
+ - lib/generators/chromium/pdf/install/install_generator.rb
30
+ - lib/generators/chromium/pdf/install/templates/app/jobs/generate_pdf_job.rb
31
+ - lib/generators/chromium/pdf/install/templates/config/initializers/good_job.rb
32
+ - sig/chromium/pdf.rbs
33
+ homepage: https://github.com/RoleModel/chromium-pdf
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/RoleModel/chromium-pdf
38
+ source_code_uri: https://github.com/RoleModel/chromium-pdf
39
+ changelog_uri: https://github.com/RoleModel/chromium-pdf/blob/main/CHANGELOG.md
40
+ rubygems_mfa_required: 'true'
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 3.0.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.5.17
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: A wrapper around headless Chrome's print-to-pdf functionality.
60
+ test_files: []