Skip to content

Commit 761f217

Browse files
committed
Fix encoding and mutable string issues
1 parent 411a8c5 commit 761f217

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ browser.screenshot(path: "google.jpg") # => 30902
346346
browser.screenshot(full: true, quality: 60) # "iVBORw0KGgoAAAANSUhEUgAABAAAAAMACAYAAAC6uhUNAAAAAXNSR0IArs4c6Q...
347347
```
348348

349-
#### pdf(\*\*options) : `String` | `Integer`
349+
#### pdf(\*\*options) : `String` | `Boolean`
350350

351351
Saves PDF on a disk or returns it as base64.
352352

@@ -366,7 +366,7 @@ Saves PDF on a disk or returns it as base64.
366366
```ruby
367367
browser.go_to("https://google.com/")
368368
# Save to disk as a PDF
369-
browser.pdf(path: "google.pdf", paper_width: 1.0, paper_height: 1.0) # => 14983
369+
browser.pdf(path: "google.pdf", paper_width: 1.0, paper_height: 1.0) # => true
370370
```
371371

372372
#### mhtml(\*\*options) : `String` | `Integer`

lib/ferrum/page/screenshot.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module Screenshot
2424
A6: { width: 4.13, height: 5.83 },
2525
}.freeze
2626

27+
STREAM_CHUNK = 128 * 1024
28+
2729
def screenshot(**opts)
2830
path, encoding = common_options(**opts)
2931
options = screenshot_options(path, **opts)
@@ -34,14 +36,15 @@ def screenshot(**opts)
3436
save_file(path, bin)
3537
end
3638

37-
def pdf(**opts)
39+
def pdf(**opts)
3840
path, encoding = common_options(**opts)
3941
options = pdf_options(**opts).merge(transferMode: "ReturnAsStream")
40-
stream_handle = command("Page.printToPDF", **options).fetch("stream")
42+
handle = command("Page.printToPDF", **options).fetch("stream")
43+
4144
if path
42-
stream_to_file(stream_handle, path)
45+
stream_to_file(handle, path: path)
4346
else
44-
stream_to_memory(stream_handle)
47+
stream_to_memory(handle, encoding: encoding)
4548
end
4649
end
4750

@@ -71,25 +74,26 @@ def save_file(path, data)
7174
File.open(path.to_s, "wb") { |f| f.write(data) }
7275
end
7376

74-
def stream_to_file(stream_handle, path)
75-
File.open(path, 'wb') do |output_file|
76-
stream_to stream_handle, output_file
77-
end
77+
def stream_to_file(handle, path:)
78+
File.open(path, "wb") { |f| stream_to(handle, f) }
79+
true
7880
end
7981

80-
def stream_to_memory(stream_handle)
81-
in_memory_data = ''
82-
stream_to stream_handle, in_memory_data
83-
in_memory_data
82+
def stream_to_memory(handle, encoding:)
83+
data = String.new("") # Mutable string has << and compatible to File
84+
stream_to(handle, data)
85+
encoding == :base64 ? Base64.encode64(data) : data
8486
end
8587

86-
def stream_to(stream_handle, output)
88+
def stream_to(handle, output)
8789
loop do
88-
read_result = command("IO.read", handle: stream_handle, size: 131072)
89-
data_chunk = read_result['data']
90-
data_chunk = Base64.decode64(data_chunk) if read_result['base64Encoded']
90+
result = command("IO.read", handle: handle, size: STREAM_CHUNK)
91+
92+
data_chunk = result["data"]
93+
data_chunk = Base64.decode64(data_chunk) if result["base64Encoded"]
9194
output << data_chunk
92-
break if read_result['eof']
95+
96+
break if result["eof"]
9397
end
9498
end
9599

spec/screenshot_spec.rb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -312,23 +312,24 @@ def create_screenshot(path: file, **options)
312312
scale: 1,
313313
)) { { "stream" => "1" } }
314314

315-
allow(browser.page).to receive(:command).with("IO.read", hash_including(
316-
handle: "1"
317-
)) { { "data" => "", "base64Encoded" => false, "eof" => true } }
318-
319-
browser.pdf(path: file, landscape: false,
320-
display_header_footer: false,
321-
print_background: false,
322-
scale: 1,
323-
paper_width: 8.5,
324-
paper_height: 11,
325-
margin_top: 0.4,
326-
margin_bottom: 0.4,
327-
margin_left: 0.4,
328-
margin_right: 0.4,
329-
page_ranges: "",
330-
ignore_invalid_page_ranges: false,
331-
prefer_css_page_size: false)
315+
allow(browser.page).to receive(:command).with("IO.read", hash_including(handle: "1")) {
316+
{ "data" => "", "base64Encoded" => false, "eof" => true }
317+
}
318+
319+
browser.pdf(path: file,
320+
landscape: false,
321+
display_header_footer: false,
322+
print_background: false,
323+
scale: 1,
324+
paper_width: 8.5,
325+
paper_height: 11,
326+
margin_top: 0.4,
327+
margin_bottom: 0.4,
328+
margin_left: 0.4,
329+
margin_right: 0.4,
330+
page_ranges: "",
331+
ignore_invalid_page_ranges: false,
332+
prefer_css_page_size: false)
332333
end
333334
end
334335
end

0 commit comments

Comments
 (0)