playwright-runner 1.1.0 → 1.2.0

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: 96731975daab3c59ab001fb663aadfdd3cdc79b79af624f24823fd886624908e
4
- data.tar.gz: 61b9e76b66eab8488d5569988726cee661c0fc9e2ad4549fe8fe3faf49c0c688
3
+ metadata.gz: 9d3cb5e46560a71cbf7ea7392e258784661fac579f9c9b7dcf3f8a6072eb2f7f
4
+ data.tar.gz: fad84f8a69bbd9150ecb0e9395d2d57e5f5673009285ff5b2b95acc9e45a9053
5
5
  SHA512:
6
- metadata.gz: 470deb264f7e15c05ced9c9021e4e0efad66f5b2e36f797f75a69a21f7fc5ff938899e1499aa4bf1709f61ef51ae442835d8d230ec4818d230596021e2e311c2
7
- data.tar.gz: a75b6871098d4954b84579a9059141e92daea649cfaf30ee68fd6bfe47fd1f2707fa583a3ec7162a2fd7b80e52af94afebc980e8d8aa3b0f2efc200c44030514
6
+ metadata.gz: 3679ea32e8f8279b11f1ea3cb3d3beefda7598b590e342ced27a49995436968c3cbb79947c2edc0a88e823f81cde9fd097bb70dcfa683bf1b0a99c987d7e9f11
7
+ data.tar.gz: aafdac6993fa160ff9b00efdcd97bdeaef9be79fdb99337b92502b71130971eb6b6fd1c6c7fcb341cced7f6e2550a49abda9c940c957e1a7c2876605682d134d
data/README.md CHANGED
@@ -27,31 +27,35 @@ Of course PlaywrightRunner needs Playwright npm library.
27
27
 
28
28
  ## Usage
29
29
 
30
- ### Mermaid to PDF or SVG
30
+ ### Mermaid to PNG, PDF or SVG
31
31
 
32
- Let's convert HTML files contain [Mermaid](https://mermaid.js.org/) to PDF/SVG.
33
- You have to install two components also to crop and convert PDF before running:
32
+ Let's convert HTML files contain [Mermaid](https://mermaid.js.org/) to PNG/PDF/SVG.
34
33
 
35
- - [pdfcrop](https://www.ctan.org/pkg/pdfcrop) in TeXLive.
36
- - [pdftocairo](https://gitlab.freedesktop.org/poppler/poppler) in poppler (if you'd like to get SVG).
34
+ - To get marginless PDF, you have to install [pdfcrop](https://www.ctan.org/pkg/pdfcrop) from TeXLive.
35
+ - To make SVG, you have to install [pdftocairo](https://gitlab.freedesktop.org/poppler/poppler) from poppler.
37
36
 
38
37
  On Debian GNU/Linux or its derivatives, ues `apt-get install texlive-extra-utils poppler-utils`.
39
38
 
39
+ #### Syntax
40
40
  ```ruby
41
+ config = { # you can omit them if you don't want to change anything
42
+ playwright_path: './node_modules/.bin/playwright', # playwright binary path
43
+ selfcrop: true, # use chromium's crop logic. if you'd like to use better cropping, set this to false
44
+ pdfcrop_path: 'pdfcrop', # pdfcrop path (will be ignored when selfcrop: true)
45
+ pdftocairo_path: 'pdftocairo' # pdftocairo path
46
+ }
47
+
41
48
  PlaywrightRunner.mermaids_to_images(
42
- {
43
- playwright_path: './node_modules/.bin/playwright', // playwright binary path
44
- pdfcrop_path: 'pdfcrop', // pdfcrop path
45
- pdftocairo_path: 'pdftocairo' // pdftocairo path
46
- },
47
- src: '.', // source folder contains html files
48
- dest: '.', // destination folder to export pdf or svg
49
- type: 'pdf' // 'pdf' or 'svg'
49
+ config, # config hash. if you want to use default values, specify {}
50
+ src: '.', # source folder contains html files
51
+ dest: '.', # destination folder to export pdf or svg
52
+ type: 'pdf' # 'png', 'pdf', or 'svg'
50
53
  )
51
54
  ```
52
55
 
53
- It converts all HTML files in the folder specified by `src` into PDF or SVG files (specified by `type`) using that name and writes them in the folder specified by `dest`.
56
+ It converts all HTML files in the folder specified by `src` into PNG, PDF or SVG files (specified by `type`) using that name and writes them in the folder specified by `dest`.
54
57
 
58
+ #### Example
55
59
  For Example, here is `p1.html` contains Mermaid code.
56
60
 
57
61
  ```html
@@ -81,7 +85,7 @@ require 'playwrightrunner'
81
85
 
82
86
  PlaywrightRunner.mermaids_to_images(
83
87
  {
84
- playwright_path: './node_modules/.bin/playwright' // modify for your env
88
+ playwright_path: './node_modules/.bin/playwright' # modify for your env
85
89
  },
86
90
  src: '.',
87
91
  dest: '.',
@@ -96,7 +100,8 @@ require 'playwrightrunner'
96
100
 
97
101
  PlaywrightRunner.mermaids_to_images(
98
102
  {
99
- playwright_path: './node_modules/.bin/playwright' // modify for your env
103
+ playwright_path: './node_modules/.bin/playwright', # modify for your env
104
+ pdftocairo: 'pdftocairo' # set the path to your pdftocairo
100
105
  },
101
106
  src: '.',
102
107
  dest: '.',
@@ -106,6 +111,42 @@ PlaywrightRunner.mermaids_to_images(
106
111
 
107
112
  ![sample SVG](p1.svg)
108
113
 
114
+ Convert it to PNG. `p1.png` will be created.
115
+
116
+ ```ruby
117
+ require 'playwrightrunner'
118
+
119
+ PlaywrightRunner.mermaids_to_images(
120
+ {
121
+ playwright_path: './node_modules/.bin/playwright' # modify for your env
122
+ },
123
+ src: '.',
124
+ dest: '.',
125
+ type: 'png'
126
+ )
127
+ ```
128
+
129
+ #### about outside margin of images
130
+
131
+ In the default settings, white margins occur around the image. This is chromium's specification. To avoid this, you need to crop the PDF using the external command `pdfcrop`.
132
+
133
+ After installing `pdfcrop`, specify `selfcrop: false` on config.
134
+
135
+ ```ruby
136
+ require 'playwrightrunner'
137
+
138
+ PlaywrightRunner.mermaids_to_images(
139
+ {
140
+ playwright_path: './node_modules/.bin/playwright' # modify for your env
141
+ selfcrop: false,
142
+ pdfcrop_path: 'pdfcrop' # set the path to your pdfcrop
143
+ },
144
+ src: '.',
145
+ dest: '.',
146
+ type: 'pdf'
147
+ )
148
+ ```
149
+
109
150
  ## License
110
151
 
111
152
  ```
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PlaywrightRunner
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -9,15 +9,19 @@ require 'open3'
9
9
  require 'fileutils'
10
10
 
11
11
  module PlaywrightRunner
12
- def complement_config(config)
12
+ def default_config(config)
13
13
  config[:playwright_path] ||= './node_modules/.bin/playwright'
14
+
15
+ # for Mermaid
16
+ config[:selfcrop] = true if config[:selfcrop].nil?
14
17
  config[:pdfcrop_path] ||= 'pdfcrop'
15
18
  config[:pdftocairo_path] ||= 'pdftocairo'
16
19
 
17
20
  config
18
21
  end
19
22
 
20
- def mermaids_to_images(config, src: '.', dest: '.', type: 'pdf')
23
+ def mermaids_to_images(passed_config, src: '.', dest: '.', type: 'pdf')
24
+ config = default_config(passed_config)
21
25
  Playwright.create(playwright_cli_executable_path: config[:playwright_path]) do |playwright|
22
26
  playwright.chromium.launch(headless: true) do |browser|
23
27
  page = browser.new_page
@@ -34,18 +38,39 @@ module PlaywrightRunner
34
38
  next
35
39
  end
36
40
 
37
- page.pdf(path: File.join(dest, '__PLAYWRIGHT_TMP__.pdf'))
41
+ if config[:selfcrop]
42
+ x = bounds['x'].floor
43
+ y = bounds['y'].floor
44
+ width = bounds['width'].ceil
45
+ height = bounds['height'].ceil
46
+ page.set_viewport_size({ width: x + width, height: y + height })
47
+
48
+ if type == 'png'
49
+ page.screenshot(path: File.join(dest, "#{id}.png"), clip: { x: x, y: y, width: width, height: height })
50
+ break
51
+ end
52
+
53
+ page.pdf(path: File.join(dest, "#{id}.pdf"),
54
+ width: "#{width + (x * 2)}px",
55
+ height: "#{height + (y * 2)}px")
56
+ else
57
+ page.pdf(path: File.join(dest, '__PLAYWRIGHT_TMP__.pdf'))
58
+ Open3.capture2e(config[:pdfcrop_path],
59
+ File.join(dest, '__PLAYWRIGHT_TMP__.pdf'),
60
+ File.join(dest, "#{id}.pdf"))
61
+ end
62
+
38
63
  break
39
64
  end
40
65
 
41
- Open3.capture2e(config[:pdfcrop_path],
42
- File.join(dest, '__PLAYWRIGHT_TMP__.pdf'),
43
- File.join(dest, "#{id}.pdf"))
44
-
45
66
  next unless type == 'svg'
46
67
 
47
68
  Open3.capture2e(config[:pdftocairo_path],
48
69
  '-svg',
70
+ '-f',
71
+ '1',
72
+ '-l',
73
+ '1',
49
74
  File.join(dest, "#{id}.pdf"),
50
75
  File.join(dest, "#{id}.svg"))
51
76
  FileUtils.rm_f(File.join(dest, "#{id}.pdf"))
@@ -56,5 +81,5 @@ module PlaywrightRunner
56
81
  end
57
82
  end
58
83
 
59
- module_function :complement_config, :mermaids_to_images
84
+ module_function :mermaids_to_images, :default_config
60
85
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: playwright-runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenshi Muto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-04 00:00:00.000000000 Z
11
+ date: 2023-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: playwright-ruby-client