html2pdf_chrome 0.0.1

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: c3d5b21d04b04f6b07b0d0e3d57a7fd5238fa0f7e336c010b9bbbc1d22226bc2
4
+ data.tar.gz: 6c94ff62b209aa7271587196c7f78ed830fd99a4d354aabb75a1de571dd0835c
5
+ SHA512:
6
+ metadata.gz: 43adc939f77e8bc8e651419679381036f941e0db52d0a39eb0781b479c84a3a18f3956fd598257ad19ba13af107d70db6941be36291edffbda8e0ccdfbc7e3c4
7
+ data.tar.gz: 169ec2aeff966b151d6e39840eea096095f0e20be8eaedd5df817ca33f323b58994980ccad81b1709f907ff9ee75cd2df840339e8de3e16fabdff5d6454fd661
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Jonathan Hooper
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,26 @@
1
+ # HTML2PDFChrome
2
+
3
+ This gem provides a tool for converting HTML documents to PDFs.
4
+
5
+ There are a lof of tools out there that do this. This one uses Chromedriver to render the HTML and convert it to a PDF. It uses Selenium to fire up Chromedriver.
6
+
7
+ ## Installation
8
+
9
+ This gem can be installed with rubygems:
10
+
11
+ ```ruby
12
+ gem "html2pdf_chrome"
13
+ ```
14
+
15
+ You will, of course, also need a working Google Chrome and Chromedriver installation. Chromedriver can be downloaded from Google or it can be installed via homebrew.
16
+
17
+ ## Usage
18
+
19
+ Once everything is installed using it is pretty easy:
20
+
21
+ ```ruby
22
+ require "html2pdf_chrome"
23
+ my_html = "<p>This PDF just has this paragraph</p>"
24
+ pdf_data = HTML2PDFChrome.convert_html_to_pdf(my_html)
25
+ File.write("my_pdf.pdf", pdf_data)
26
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ module HTML2PDFChrome
2
+ class Chromedriver
3
+ ##
4
+ # Starting chromedriver can be slow. This method initializes a single
5
+ # driver instance to share. This driver is accessed via a Mutex to prevent
6
+ # async caller's operations from bleeding into each other.
7
+ #
8
+ class << self
9
+ def fetch_driver
10
+ @driver ||= initialize_driver
11
+ @semaphore ||= Mutex.new
12
+ @semaphore.synchronize do
13
+ yield @driver
14
+ end
15
+ nil
16
+ end
17
+
18
+ private
19
+
20
+ def initialize_driver
21
+ options = Selenium::WebDriver::Chrome::Options.new
22
+ options.add_argument('--headless=new')
23
+ options.add_argument('--disable-gpu')
24
+ options.add_argument('--no-sandbox')
25
+ options.add_argument('--disable-software-rasterizer')
26
+
27
+ Selenium::WebDriver.for(:chrome, options: options)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ module HTML2PDFChrome
2
+ class HtmlConverter
3
+ attr_reader :html
4
+
5
+ def initialize(html)
6
+ @html = html
7
+ end
8
+
9
+ def convert_to_pdf!
10
+ encoded_html = Base64.strict_encode64(html)
11
+ data_url = "data:text/html;base64,#{encoded_html}"
12
+
13
+ pdf_data = nil
14
+ Chromedriver.fetch_driver do |driver|
15
+ driver.navigate.to(data_url)
16
+ cdp_response = driver.execute_cdp('Page.printToPDF')
17
+ pdf_data = Base64.decode64(cdp_response['data'])
18
+ end
19
+ pdf_data
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTML2PDFChrome
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'selenium-webdriver'
5
+
6
+ require_relative "html2pdf_chrome/version"
7
+
8
+ require_relative "./html2pdf_chrome/chromedriver"
9
+ require_relative "./html2pdf_chrome/html_converter"
10
+
11
+ module HTML2PDFChrome
12
+ class Error < StandardError; end
13
+
14
+ def self.convert_html_to_pdf(html)
15
+ HtmlConverter.new(html).convert_to_pdf!
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module HTML2PDFChrome
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html2pdf_chrome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Hooper
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-03-13 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: selenium-webdriver
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ description: This gem is built to convert HTML document into PDFs. There are lots
27
+ of tools out there that do this. This one uses Selenium+Chromedriver to render the
28
+ HTML and generate the PDF.
29
+ email:
30
+ - jon9820@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/html2pdf_chrome.rb
40
+ - lib/html2pdf_chrome/chromedriver.rb
41
+ - lib/html2pdf_chrome/html_converter.rb
42
+ - lib/html2pdf_chrome/version.rb
43
+ - sig/html2pdf_chrome.rbs
44
+ homepage: https://github.com/Tesibus/html2pdf_chrome
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/Tesibus/html2pdf_chrome
49
+ source_code_uri: https://github.com/Tesibus/html2pdf_chrome
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 3.1.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.6.2
65
+ specification_version: 4
66
+ summary: A gem for generating PDFs from HTML using Chromedriver.
67
+ test_files: []