ferrum_pdf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fd59867787cfd16bf19645eda1dce8c8f21541d3d8b52614e025a60847787930
4
+ data.tar.gz: 5dd180545309a3a0c61ba90286943404382478c44337495c81237f49953559dd
5
+ SHA512:
6
+ metadata.gz: 4b42648daf392f18fc9f1b8dfa947a1b26a1af29eb615af558dd23440222e80f9d846c40fe44206b1dd9f8bfcad5be64f5987559c6e833a2547b246a97093226
7
+ data.tar.gz: 649e5d9b57f8bfd4ad9e0c62d23cba3f1b435ec3a7a13a237e8e0cd567914e4958d93489519c103166bed3912327547bfe443af0f8f04697043a05e33658a7ed
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Chris Oliver
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # FerrumPdf
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "ferrum_pdf"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install ferrum_pdf
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ 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,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module FerrumPdf
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "ferrum_pdf.controller" do
4
+ ActiveSupport.on_load(:action_controller) do
5
+ include FerrumPdf::Controller
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module FerrumPdf
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ferrum_pdf.rb ADDED
@@ -0,0 +1,64 @@
1
+ require "ferrum_pdf/version"
2
+ require "ferrum_pdf/railtie"
3
+
4
+ require "ferrum"
5
+
6
+ module FerrumPdf
7
+ def self.browser(**options)
8
+ @browser ||= Ferrum::Browser.new(options)
9
+ end
10
+
11
+ def self.render_pdf(host:, protocol:, html: nil, url: nil)
12
+ Tempfile.create do |file|
13
+ browser.create_page do |page|
14
+ if html
15
+ page.content = FerrumPdf::HTMLPreprocessor.process(html, host, protocol)
16
+ page.network.wait_for_idle
17
+ else
18
+ page.go_to(url)
19
+ end
20
+ page.pdf(path: file.path)
21
+ file.read
22
+ end
23
+ end
24
+ end
25
+
26
+ # Helper module for preparing HTML for conversion
27
+ #
28
+ # Sourced from the PDFKit project
29
+ # @see https://github.com/pdfkit/pdfkit
30
+ module HTMLPreprocessor
31
+ # Change relative paths to absolute, and relative protocols to absolute protocols
32
+ def self.process(html, root_url, protocol)
33
+ html = translate_relative_paths(html, root_url) if root_url
34
+ html = translate_relative_protocols(html, protocol) if protocol
35
+ html
36
+ end
37
+
38
+ def self.translate_relative_paths(html, root_url)
39
+ # Try out this regexp using rubular http://rubular.com/r/hiAxBNX7KE
40
+ html.gsub(%r{(href|src)=(['"])/([^/"']([^"']*|[^"']*))?['"]}, "\\1=\\2#{root_url}\\3\\2")
41
+ end
42
+ private_class_method :translate_relative_paths
43
+
44
+ def self.translate_relative_protocols(body, protocol)
45
+ # Try out this regexp using rubular http://rubular.com/r/0Ohk0wFYxV
46
+ body.gsub(%r{(href|src)=(['"])//([^"']*|[^"']*)['"]}, "\\1=\\2#{protocol}://\\3\\2")
47
+ end
48
+ private_class_method :translate_relative_protocols
49
+ end
50
+
51
+ module Controller
52
+ extend ActiveSupport::Concern
53
+
54
+ def render_pdf(name = action_name, formats: [ :html ])
55
+ content = render_to_string(name, formats: formats)
56
+
57
+ FerrumPdf.render_pdf(
58
+ html: content,
59
+ host: request.host_with_port,
60
+ protocol: request.protocol
61
+ )
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :ferrum_pdf do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ferrum_pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Oliver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: ferrum
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.15'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.15'
41
+ description: Export PDFs from HTML in Rails using Ferrum & headless Chrome
42
+ email:
43
+ - excid3@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/ferrum_pdf.rb
52
+ - lib/ferrum_pdf/railtie.rb
53
+ - lib/ferrum_pdf/version.rb
54
+ - lib/tasks/ferrum_pdf_tasks.rake
55
+ homepage: https://github.com/excid3/ferrum_pdf
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ homepage_uri: https://github.com/excid3/ferrum_pdf
60
+ source_code_uri: https://github.com/excid3/ferrum_pdf
61
+ changelog_uri: https://github.com/excid3/ferrum_pdf/blob/main/CHANGELOG.md
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.5.18
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: PDFs for Rails using Ferrum & headless Chrome
81
+ test_files: []