prawn-html 0.1.0 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5a671ae12f6a66358c10f739404ab709f3fdcaacb059b9943433a7535a539a5
4
- data.tar.gz: 274afa7a2d6cbb2a75e1ca03e2b82ee38985bf279f41ecd3878166ce9eabf15a
3
+ metadata.gz: 2a2fb462a91991cadb770f8bbee7d07c99f670d46eeb62d2a3b467b1839e5364
4
+ data.tar.gz: 1710135c8ca94e2de836e913ce8e177b9bc47b4ee37725a23729912b086aff25
5
5
  SHA512:
6
- metadata.gz: a67e743e2c77c1b0e9caecefd9b6f1fc2d33243d0b86b37548f5bbed7e8e569ea11562d849c82fbdfd8ca7eeb5bdf2921cf789464868f0e90de4b631dcf89a10
7
- data.tar.gz: dd06b9b0410cceb28eaf68ab6eaeb6007086047478f3ec1ba2ffd37a0a38de38264f56fae3232b4025a5d6f45b776fe2d79bb23eb76926a8a87139e82294e14d
6
+ metadata.gz: ceec4ef90b155ea23f659771ab4e6275f12c9c5d4dd29a5da11b4d7f23512320ec71246623ff3c0eb82e0bed9a5e837292c40ac5a8d82585eaa4cb64cdc0653b
7
+ data.tar.gz: efea6fe48069d63b56c8c2e7a0142ed959af8cb200e8bc6ff03f8b252614379130feed7bd54942d59a97689c43f3b8a407e739db2b3c9fd1087f10c75fe0ca35
data/README.md CHANGED
@@ -1,29 +1,37 @@
1
1
  # Prawn HTML
2
+ [![gem version](https://badge.fury.io/rb/prawn-html.svg)](https://badge.fury.io/rb/prawn-html)
2
3
  [![linters](https://github.com/blocknotes/prawn-html/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/prawn-html/actions/workflows/linters.yml)
3
4
  [![specs](https://github.com/blocknotes/prawn-html/actions/workflows/specs.yml/badge.svg)](https://github.com/blocknotes/prawn-html/actions/workflows/specs.yml)
4
5
 
5
6
  HTML to PDF renderer using [Prawn PDF](https://github.com/prawnpdf/prawn).
6
7
 
7
- > Still in beta. [prawn-styled-text](https://github.com/blocknotes/prawn-styled-text) rewritten from scratch
8
+ Features:
9
+ - support a [good set](#supported-tags--attributes) of HTML tags and CSS properties;
10
+ - handle [document styles](#document-styles);
11
+ - no extra settings: it just parses an input HTML and output to a Prawn PDF document.
8
12
 
9
13
  **Notice**: render HTML documents properly is not an easy task, this gem support only some HTML tags and a small set of CSS attributes. If you need more rendering accuracy take a look at other projects like WickedPDF.
10
14
 
15
+ > [prawn-styled-text](https://github.com/blocknotes/prawn-styled-text) rewritten from scratch, finally!
16
+
11
17
  Please :star: if you like it.
12
18
 
13
19
  ## Install
14
20
 
15
- - Add to your Gemfile: `gem 'prawn-html', git: 'https://github.com/blocknotes/prawn-html.git'` (and execute `bundle`)
16
- - Use the class `HtmlHandler` on a `Prawn::Document` instance
21
+ - Add to your Gemfile: `gem 'prawn-html'` (and execute `bundle`)
22
+ - Just call `PrawnHtml.append_html` on a `Prawn::Document` instance (see the examples)
17
23
 
18
24
  ## Examples
19
25
 
20
26
  ```rb
21
27
  require 'prawn-html'
22
28
  pdf = Prawn::Document.new(page_size: 'A4')
23
- PrawnHtml::HtmlHandler.new(pdf).process('<h1 style="text-align: center">Just a test</h1>')
29
+ PrawnHtml.append_html(pdf, '<h1 style="text-align: center">Just a test</h1>')
24
30
  pdf.render_file('test.pdf')
25
31
  ```
26
32
 
33
+ To check some examples with the PDF output see [examples](examples/) folder.
34
+
27
35
  ## Supported tags & attributes
28
36
 
29
37
  HTML tags:
@@ -69,6 +77,34 @@ CSS attributes (dimensional units are ignored and considered in pixel):
69
77
  - **text-decoration**: `underline`, ex. `style="text-decoration: underline"`
70
78
  - **width**: for *img* tag, support also percentage, ex. `<img src="image.jpg" style="width: 50%; height: 200px"/>`
71
79
 
80
+ ## Document styles
81
+
82
+ [Experimental feature] You can define document CSS rules inside an _head_ tag, but with a limited support for now.
83
+ Only single CSS selectors and basic ones are supported. Example:
84
+
85
+ ```html
86
+ <!DOCTYPE html>
87
+ <html>
88
+ <head>
89
+ <title>A test</title>
90
+ <style>
91
+ body { color: #abbccc }
92
+ .green {
93
+ color: #0f0;
94
+ font-family: Courier;
95
+ }
96
+ #test-1 { font-weight: bold }
97
+ </style>
98
+ </head>
99
+ <body>
100
+ <div class="green">
101
+ Div content
102
+ <span id="test-1">Span content</span>
103
+ </div>
104
+ </body>
105
+ </html>
106
+ ```
107
+
72
108
  ## Do you like it? Star it!
73
109
 
74
110
  If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
data/lib/prawn-html.rb CHANGED
@@ -13,5 +13,12 @@ require 'prawn_html/document_renderer'
13
13
  require 'prawn_html/html_handler'
14
14
 
15
15
  module PrawnHtml
16
- PX = 0.66 # conversion costant for pixel sixes
16
+ PX = 0.66 # conversion constant for pixel sixes
17
+
18
+ def append_html(pdf, html)
19
+ handler = PrawnHtml::HtmlHandler.new(pdf)
20
+ handler.process(html)
21
+ end
22
+
23
+ module_function :append_html
17
24
  end
@@ -8,9 +8,7 @@ module PrawnHtml
8
8
  ELEMENTS = [:a].freeze
9
9
 
10
10
  def styles
11
- super.merge(
12
- link: attrs.hash.href
13
- )
11
+ attrs.hash.href ? super.merge(link: attrs.hash.href) : super
14
12
  end
15
13
  end
16
14
  end
@@ -21,7 +21,7 @@ module PrawnHtml
21
21
  merged_styles = document_styles.each_with_object({}) do |(sel, attributes), res|
22
22
  res.merge!(attributes) if selectors.include?(sel)
23
23
  end
24
- styles.merge!(merged_styles)
24
+ @styles = merged_styles.merge(styles)
25
25
  end
26
26
 
27
27
  def block?
@@ -10,6 +10,7 @@ module PrawnHtml
10
10
  def update_styles(styles)
11
11
  size = (styles[:size] || Context::DEF_FONT_SIZE) * 0.85
12
12
  styles[:size] = size
13
+ styles
13
14
  end
14
15
  end
15
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PrawnHtml # :nodoc:
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Roccoberton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-10 00:00:00.000000000 Z
11
+ date: 2021-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga