From 5f598aa3b6d43d0a2ba1bfe0c0abc9acb1543d94 Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Mon, 3 Mar 2025 07:25:24 -0700 Subject: [PATCH 01/10] add typespec for timezone --- lib/chromic_pdf/supervisor.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/chromic_pdf/supervisor.ex b/lib/chromic_pdf/supervisor.ex index 92d3859..95c17ef 100644 --- a/lib/chromic_pdf/supervisor.ex +++ b/lib/chromic_pdf/supervisor.ex @@ -179,11 +179,14 @@ defmodule ChromicPDF.Supervisor do required(:attribute) => binary() }} + @type set_timezone_option :: {:set_timezone, %{required(:timezoneId) => String.t()}} + @type navigate_option :: {:set_cookie, map()} | {:assigns, map()} | evaluate_option() | wait_for_option() + | set_timezone_option() @type pdf_option :: {:print_to_pdf, map()} From c3aa322d8cdafbcb5a4ce3eb03ee65800a5f50b0 Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Mon, 3 Mar 2025 07:25:53 -0700 Subject: [PATCH 02/10] Add set_timezone call --- lib/chromic_pdf/pdf/protocols/navigate.ex | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/chromic_pdf/pdf/protocols/navigate.ex b/lib/chromic_pdf/pdf/protocols/navigate.ex index 093376d..327b9a1 100644 --- a/lib/chromic_pdf/pdf/protocols/navigate.ex +++ b/lib/chromic_pdf/pdf/protocols/navigate.ex @@ -12,6 +12,12 @@ defmodule ChromicPDF.Navigate do await_response(:cookie_set, []) end + if_option :set_timezone do + call(:set_timezone, "Emulation.setTimezoneOverride", &Map.fetch!(&1, :set_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"}]) From ccdd7da60a20c110ad894505c9fc9ef80a0edcc3 Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Mon, 3 Mar 2025 07:27:14 -0700 Subject: [PATCH 03/10] Test setting a timezone --- test/integration/pdf_generation_test.exs | 19 +++++++++++++++++++ test/integration/support/test_server.ex | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/test/integration/pdf_generation_test.exs b/test/integration/pdf_generation_test.exs index b106989..2884230 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, offline: false}) + 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, [set_timezone: %{timezoneId: "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 From 814d859e998364051fd99b7d390ed1cadd68e600 Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Mon, 3 Mar 2025 10:45:06 -0700 Subject: [PATCH 04/10] Update typespecs --- lib/chromic_pdf/supervisor.ex | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/chromic_pdf/supervisor.ex b/lib/chromic_pdf/supervisor.ex index 95c17ef..2b5cf65 100644 --- a/lib/chromic_pdf/supervisor.ex +++ b/lib/chromic_pdf/supervisor.ex @@ -179,14 +179,12 @@ defmodule ChromicPDF.Supervisor do required(:attribute) => binary() }} - @type set_timezone_option :: {:set_timezone, %{required(:timezoneId) => String.t()}} - @type navigate_option :: {:set_cookie, map()} | {:assigns, map()} | evaluate_option() | wait_for_option() - | set_timezone_option() + | {:set_timezone, String.t()} @type pdf_option :: {:print_to_pdf, map()} From a92e27b93cc9485e6cde7824a748aec40e6e592b Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Mon, 3 Mar 2025 10:45:18 -0700 Subject: [PATCH 05/10] update call --- lib/chromic_pdf/pdf/protocols/navigate.ex | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/chromic_pdf/pdf/protocols/navigate.ex b/lib/chromic_pdf/pdf/protocols/navigate.ex index 327b9a1..5d052c7 100644 --- a/lib/chromic_pdf/pdf/protocols/navigate.ex +++ b/lib/chromic_pdf/pdf/protocols/navigate.ex @@ -12,8 +12,13 @@ defmodule ChromicPDF.Navigate do await_response(:cookie_set, []) end - if_option :set_timezone do - call(:set_timezone, "Emulation.setTimezoneOverride", &Map.fetch!(&1, :set_timezone), %{}) + if_option :timezone do + call( + :timezone, + "Emulation.setTimezoneOverride", + &%{"timezoneId" => Map.fetch!(&1, :timezone)}, + %{} + ) await_response(:timezone_set, []) end From fc3cfc68be2f920a8484b9f3f003f0e6e6d3c48c Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Mon, 3 Mar 2025 10:46:10 -0700 Subject: [PATCH 06/10] update test --- test/integration/pdf_generation_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/pdf_generation_test.exs b/test/integration/pdf_generation_test.exs index 2884230..ebecaff 100644 --- a/test/integration/pdf_generation_test.exs +++ b/test/integration/pdf_generation_test.exs @@ -406,7 +406,7 @@ defmodule ChromicPDF.PDFGenerationTest do test "timezone can be set", %{port: port} do input = {:url, "http://localhost:#{port}/timezone_echo"} - print_to_pdf(input, [set_timezone: %{timezoneId: "America/New_York"}], fn text -> + print_to_pdf(input, [timezone: "America/New_York"], fn text -> text = text |> String.trim() |> Jason.decode!() assert text == %{"timezoneId" => "America/New_York"} end) From 754dd678859af68184bb0ebcafb3c4e8a5d8f666 Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Mon, 3 Mar 2025 10:53:04 -0700 Subject: [PATCH 07/10] Updated changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ac7487..a2d383c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## Unreleased + +### Added + +- :timezone to navigation protocol + ## [1.17.0] - 2024-08-09 ### Changed From 55c3aabc34728216ed29658a9d1508366794f7bb Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Mon, 3 Mar 2025 10:53:50 -0700 Subject: [PATCH 08/10] update spec to correct name --- lib/chromic_pdf/supervisor.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chromic_pdf/supervisor.ex b/lib/chromic_pdf/supervisor.ex index 2b5cf65..cfc5251 100644 --- a/lib/chromic_pdf/supervisor.ex +++ b/lib/chromic_pdf/supervisor.ex @@ -184,7 +184,7 @@ defmodule ChromicPDF.Supervisor do | {:assigns, map()} | evaluate_option() | wait_for_option() - | {:set_timezone, String.t()} + | {:timezone, String.t()} @type pdf_option :: {:print_to_pdf, map()} From 7f59d3e26dd16d14b6378ead7618c51ab01e1905 Mon Sep 17 00:00:00 2001 From: casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Tue, 4 Mar 2025 04:23:23 -0700 Subject: [PATCH 09/10] remove offline opt in test --- test/integration/pdf_generation_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/pdf_generation_test.exs b/test/integration/pdf_generation_test.exs index ebecaff..2a9bc7b 100644 --- a/test/integration/pdf_generation_test.exs +++ b/test/integration/pdf_generation_test.exs @@ -396,7 +396,7 @@ defmodule ChromicPDF.PDFGenerationTest do describe "timezone can be set thru print_to_pdf/2" do setup do - start_supervised!({ChromicPDF, offline: false}) + start_supervised!(ChromicPDF) start_supervised!(TestServer.bandit(:http)) %{port: TestServer.port(:http)} From 9bc677667b8dd43f7daeb9ab5b184051b2908f2e Mon Sep 17 00:00:00 2001 From: Casey <9088410+CaseyInHaengsin@users.noreply.github.com> Date: Tue, 4 Mar 2025 04:24:39 -0700 Subject: [PATCH 10/10] Update CHANGELOG.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Andreas Knöpfle --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2d383c..4280eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Added -- :timezone to navigation protocol +- `print_to_pdf` now supports a `:timezone` option to set the timezone before navigating with `Emulation.setTimezoneOverride` ## [1.17.0] - 2024-08-09