diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ac7487..4280eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/chromic_pdf/pdf/protocols/navigate.ex b/lib/chromic_pdf/pdf/protocols/navigate.ex index 093376d..5d052c7 100644 --- a/lib/chromic_pdf/pdf/protocols/navigate.ex +++ b/lib/chromic_pdf/pdf/protocols/navigate.ex @@ -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"}]) diff --git a/lib/chromic_pdf/supervisor.ex b/lib/chromic_pdf/supervisor.ex index 92d3859..cfc5251 100644 --- a/lib/chromic_pdf/supervisor.ex +++ b/lib/chromic_pdf/supervisor.ex @@ -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()} diff --git a/test/integration/pdf_generation_test.exs b/test/integration/pdf_generation_test.exs index b106989..2a9bc7b 100644 --- a/test/integration/pdf_generation_test.exs +++ b/test/integration/pdf_generation_test.exs @@ -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) diff --git a/test/integration/support/test_server.ex b/test/integration/support/test_server.ex index 17345b5..75e2a79 100644 --- a/test/integration/support/test_server.ex +++ b/test/integration/support/test_server.ex @@ -33,6 +33,21 @@ defmodule ChromicPDF.TestServer do send_resp(conn, 200, inspect(conn.req_cookies)) end + get "/timezone_echo" do + body = """ + + + + + + + """ + + send_resp(conn, 200, body) + end + get "/with_plug" do ChromicPDF.Plug.call(conn, []) end