gotenberg 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 117763ebd188d065008ebbfa04c29db6d164608356ed6b11f9d4457f5e798d20
4
- data.tar.gz: c4d5bb32b5fb9b638384857a3ef264097d368dd23ce976439c52374f123f86f9
3
+ metadata.gz: 489dc50093dc4d7dfcf8934e4f7f1e66c42c3ee331b8f6712206a4f9e4bb3f95
4
+ data.tar.gz: 32a0dacebb19c92447cc448ea93fa94de7081cc7fdff11a5fd271d20d262ae1e
5
5
  SHA512:
6
- metadata.gz: 747de8d3f0e7b8ffe094c77fb3298307c08de8cdfc60b391bf8e91c0cee056ff2cb5bab055751e4df5f88f3c8ce691bf3b1db5430596210f9c6f9bc54dcba61b
7
- data.tar.gz: d58c30a3cfb85840ee72bc1104c68307c843b8abccb29d94953a0aed3962926a8dcef424216bd8e078a34e8130f36d04d900b81b85149714dace507a48c126b0
6
+ metadata.gz: a39d81adfa325e16a1a9e5105103e1e9f68acd22600d6b56fcf951f8a45c97353eb676dc1e36c17f6f9113ef20c4bfe05280cc0fa53552a18f4a6d3d176d6674
7
+ data.tar.gz: dc6294d7356a315e002eb47e8a311908c541c18b0980e3d29325558fd72612bcb628f999f9b841b88c7e68480796a4adb49437a8bd20690f990c76a2771bb2c9
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gotenberg
4
+ module Assets
5
+ ASSET_TYPES = {
6
+ "js" => "javascripts",
7
+ "css" => "stylesheets",
8
+
9
+ # Fonts
10
+ "otf" => "fonts",
11
+ "ttf" => "fonts",
12
+ "woff" => "fonts",
13
+ "woff2" => "fonts",
14
+ "eot" => "fonts",
15
+ "sfnt" => "fonts",
16
+ "fnt" => "fonts",
17
+ "pfa" => "fonts",
18
+ "pfb" => "fonts",
19
+ "afm" => "fonts",
20
+ "pcf" => "fonts",
21
+ "snf" => "fonts",
22
+ "bdf" => "fonts",
23
+
24
+ # Images
25
+ "bmp" => "images",
26
+ "gif" => "images",
27
+ "jpg" => "images",
28
+ "jpeg" => "images",
29
+ "png" => "images",
30
+ "svg" => "images",
31
+ "tiff" => "images",
32
+ "tif" => "images",
33
+ "webp" => "images",
34
+ "ico" => "images",
35
+ "psd" => "images",
36
+ "heic" => "images"
37
+ }.freeze
38
+ end
39
+ end
@@ -48,7 +48,8 @@ module Gotenberg
48
48
  # client = Gotenberg::Client.new("http://localhost:3000")
49
49
  # pdf_content = client.html(htmls, asset_paths, properties)
50
50
  #
51
- def html(htmls, asset_paths, properties = {}) # rubocop:disable Metrics/CyclomaticComplexity
51
+ # Credit: https://github.com/jbd0101/ruby-gotenberg-client/blob/master/lib/gotenberg.rb
52
+ def html(htmls, asset_paths, properties = {})
52
53
  raise GotenbergDownError unless up?
53
54
 
54
55
  raise IndexFileMissing unless (htmls.keys & ["index", :index]).any?
@@ -66,6 +67,12 @@ module Gotenberg
66
67
  # Gotenberg requires all files to be in the same directory
67
68
  asset_paths.each do |path|
68
69
  FileUtils.cp(path, dir_path)
70
+ next unless path.split(".").last == "css"
71
+
72
+ # This preprocessing is necessary for the gotenberg
73
+ raw_css = File.read(path)
74
+ preprocessed = raw_css.gsub("url(/assets/", "url(").gsub("url(/", "url(")
75
+ File.write(path, preprocessed)
69
76
  end
70
77
 
71
78
  # Rejecting .. and .
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "assets"
4
+
3
5
  module Gotenberg
4
6
  module Helper # rubocop:disable Style/Documentation
5
7
  class ExtensionMissing < StandardError; end
6
8
 
7
- class PropshaftAsset # rubocop:disable Style/Documentation
9
+ # Credits: https://github.com/mileszs/wicked_pdf/blob/master/lib/wicked_pdf/wicked_pdf_helper/assets.rb
10
+ class PropshaftAsset
8
11
  attr_reader :asset
9
12
 
10
13
  def initialize(asset)
@@ -63,6 +66,11 @@ module Gotenberg
63
66
  end
64
67
  end
65
68
 
69
+ # Returns the base64 encoded content of a static asset.
70
+ #
71
+ # @param asset_name [String] the name of the asset
72
+ # @raise [MissingAsset] if the asset is not found
73
+ # @return [String] the base64 encoded content of the asset
66
74
  def goten_asset_base64(asset_name)
67
75
  asset = find_asset(goten_static_asset_path(asset_name))
68
76
  raise MissingAsset.new(asset_name, "Could not find asset '#{asset_name}'") if asset.nil?
@@ -71,28 +79,48 @@ module Gotenberg
71
79
  "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
72
80
  end
73
81
 
82
+ # Returns the static path of an asset based on its extension.
83
+ #
84
+ # @param asset_name [String] the name of the asset
85
+ # @raise [ExtensionMissing] if the asset extension is missing
86
+ # @return [String] the static path of the asset
74
87
  def goten_static_asset_path(asset_name)
75
88
  ext = File.extname(asset_name).delete(".")
76
89
 
77
90
  raise ExtensionMissing if ext.empty?
78
91
 
79
- asset_type =
80
- case ext
81
- when "js" then "javascripts"
82
- when "css" then "stylesheets"
83
- else "images"
84
- end
92
+ asset_type = Assets::ASSET_TYPES.fetch(ext)
85
93
 
86
94
  determine_static_path(asset_type, asset_name)
87
95
  end
88
96
 
97
+ # Returns the compiled path of an asset.
98
+ #
99
+ # @param asset_name [String] the name of the asset
100
+ # @return [String] the compiled path of the asset
89
101
  def goten_compiled_asset_path(asset_name)
90
102
  Rails.public_path.to_s +
91
103
  ActionController::Base.helpers.asset_path(asset_name)
92
104
  end
93
105
 
106
+ # Returns the compiled name of an asset.
107
+ #
108
+ # @param asset_name [String] the name of the asset
109
+ # @return [String] the compiled name of the asset
110
+
111
+ def goten_compiled_asset_name(asset_name)
112
+ path = goten_compiled_asset_path(asset_name)
113
+ path.split("/").last
114
+ end
115
+
94
116
  private
95
117
 
118
+ # Determines and returns the static path of an asset.
119
+ #
120
+ # @param asset_type [String] the type of the asset (e.g., "javascripts", "stylesheets", "images")
121
+ # @param asset_name [String] the name of the asset
122
+ # @raise [MissingAsset] if the asset is not found
123
+ # @return [String] the static path of the asset
96
124
  def determine_static_path(asset_type, asset_name)
97
125
  asset_root = Rails.root.join("app", "assets")
98
126
  path = asset_root.join(asset_type, asset_name)
@@ -107,7 +135,10 @@ module Gotenberg
107
135
  path.to_s
108
136
  end
109
137
 
110
- # Thanks WickedPDF 🙏
138
+ # Finds and returns the asset for the given path.
139
+ #
140
+ # @param path [String] the path of the asset
141
+ # @return [PropshaftAsset, LocalAsset, Sprockets::Asset, nil] the found asset or nil if not found
111
142
  def find_asset(path)
112
143
  if Rails.application.assets.respond_to?(:find_asset)
113
144
  Rails.application.assets.find_asset(path, base_path: Rails.application.root.to_s)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gotenberg
4
- VERSION = "1.0.1"
4
+ VERSION = "1.0.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gotenberg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - bugloper
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-06-17 00:00:00.000000000 Z
12
+ date: 2024-06-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mime-types
@@ -47,6 +47,7 @@ extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
49
  - lib/gotenberg.rb
50
+ - lib/gotenberg/assets.rb
50
51
  - lib/gotenberg/client.rb
51
52
  - lib/gotenberg/helper.rb
52
53
  - lib/gotenberg/railtie.rb