Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### Added

- `print_to_pdf` now supports a `:timezone` option to set the timezone before navigating with `Emulation.setTimezoneOverride`

## [1.17.0] - 2024-08-09

### Changed
Expand Down
11 changes: 11 additions & 0 deletions lib/chromic_pdf/pdf/protocols/navigate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ defmodule ChromicPDF.Navigate do
await_response(:cookie_set, [])
end

if_option :timezone do
call(
:timezone,
"Emulation.setTimezoneOverride",
&%{"timezoneId" => Map.fetch!(&1, :timezone)},
%{}
)

await_response(:timezone_set, [])
end

if_option {:source_type, :html} do
call(:get_frame_tree, "Page.getFrameTree", [], %{})
await_response(:frame_tree, [{["frameTree", "frame", "id"], "frameId"}])
Expand Down
1 change: 1 addition & 0 deletions lib/chromic_pdf/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ defmodule ChromicPDF.Supervisor do
| {:assigns, map()}
| evaluate_option()
| wait_for_option()
| {:timezone, String.t()}

@type pdf_option ::
{:print_to_pdf, map()}
Expand Down
19 changes: 19 additions & 0 deletions test/integration/pdf_generation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,25 @@ defmodule ChromicPDF.PDFGenerationTest do
end
end

describe "timezone can be set thru print_to_pdf/2" do
setup do
start_supervised!(ChromicPDF)
start_supervised!(TestServer.bandit(:http))

%{port: TestServer.port(:http)}
end

@tag :disable_logger
test "timezone can be set", %{port: port} do
input = {:url, "http://localhost:#{port}/timezone_echo"}

print_to_pdf(input, [timezone: "America/New_York"], fn text ->
text = text |> String.trim() |> Jason.decode!()
assert text == %{"timezoneId" => "America/New_York"}
end)
end
end

describe "certificate error handling" do
setup do
start_supervised!(ChromicPDF)
Expand Down
15 changes: 15 additions & 0 deletions test/integration/support/test_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ defmodule ChromicPDF.TestServer do
send_resp(conn, 200, inspect(conn.req_cookies))
end

get "/timezone_echo" do
body = """
<html>
<body>
<span id="timezone"></span>
<script>
document.getElementById("timezone").innerText = JSON.stringify({ timezoneId: Intl.DateTimeFormat().resolvedOptions().timeZone });
</script>
</body>
</html>
"""

send_resp(conn, 200, body)
end

get "/with_plug" do
ChromicPDF.Plug.call(conn, [])
end
Expand Down