diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 0264dd4e..5a59c16f 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -93,6 +93,7 @@ Other Changes * https://github.com/clj-commons/etaoin/issues/380[#380]: Etaoin is now Babashka compatible! * https://github.com/clj-commons/etaoin/issues/413[#413]: Etaoin now exports a clj-kondo config to help with the linting of its many handy macros * https://github.com/clj-commons/etaoin/pull/357[#357]: Add support for connecting to a remote WebDriver via `:webdriver-url` (thanks https://github.com/verma[@verma] for the PR and https://github.com/mjmeintjes[@mjmeintjes] for the example usage!) +* https://github.com/clj-commons/etaoin/issues/355[#355]: Add support for W3C WebDriver print to PDF feature * https://github.com/clj-commons/etaoin/issues/453[#453]: The `etaoin.api/with-` macros no longer require `opts` to be specified. This makes the advantage of newer `etaoin.api2/with-` macros maybe less obvious. That said, for Etaoin users who have adopted and prefer the api2 versions, they are still there, but no longer documented in the user guide. diff --git a/doc/01-user-guide.adoc b/doc/01-user-guide.adoc index 8a47545c..98564b95 100644 --- a/doc/01-user-guide.adoc +++ b/doc/01-user-guide.adoc @@ -1504,6 +1504,19 @@ this is equivalent to something along the lines of: (e/screenshot driver "target/etaoin-play/saved-screenshots/chrome-3.png") ---- +== Print Page to PDF + +Use `print-page` to print the current page to a PDF file: + +[source,clojure] +---- +(e/with-firefox-headless driver + (e/go driver sample-page) + (e/print-page driver "target/etaoin-play/printed.pdf")) +---- + +See link:{url-doc}/CURRENT/api/etaoin.api/print-page[API docs] for details. + == Peeking deeper Sometimes it is useful to go a little deeper. diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index e7d590ce..80259818 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -101,6 +101,9 @@ **Screenshots** - [[screenshot]] [[screenshot-element]] [[with-screenshots]] + **Print to PDF** + - [[print-page]] + **Browser Info** - [[supports-logs?]] [[get-log-types]] [[get-logs]] - [[get-user-agent]] @@ -3205,6 +3208,57 @@ new-body (interleave body (repeat screenshot-form#))] `(do ~@new-body))) +;; +;; print current page to PDF +;; + +(defmulti print-page + "Have `driver` print current HTML page to `file` in PDF format. + + `file` can be either a string or `java.io.File`, any missing parent directories are automatically created. + + `opts` map is optional: + - `:orientation` is `:landscape` or `:portrait` (default) + - `:scale` a number, defaults to `1`, min of `0.1`, max of `2` + - `:background` defaults to `false` + - `:page` (default is North American Letter size 8.5x11 inches) + - `:width` in cm, defaults to `21.59` + - `:height` in cm, defaults to `27.94` + - `:margin` + - `:top` in cm, defaults to`1` + - `:bottom` in cm, defaults to `1` + - `:left` in cm, defaults to `1` + - `:right` in cm, default to `1` + - `:shrinkToFit` defaults to `true` + - `:pageRanges` a vector, 1-based pages to include, example `[\"1-3\" \"6\"]` or `[]` for all (default) + + + At the time of this writing current WebDriver support: + - chrome, works in headless mode only, supports all opts + - edge, works in headless only, supports all opts + - firefox, might not support all opts + - safari, feature is not implemented" + {:arglists '([driver file] [driver file opts])} + dispatch-driver) + +(defmethod print-page + :default + [_driver _file & [_opts]] + (util/error "This driver doesn't support printing pages to PDF.")) + +(defmethods print-page + [:chrome :edge :firefox] + [driver file & [opts]] + (let [resp (execute {:driver driver + :method :post + :path [:session (:session driver) :print] + :data opts}) + b64str (-> resp :value not-empty)] + (when (not b64str) + (util/error "Empty page")) + (create-dirs-for-file file) + (b64-to-file b64str file))) + ;; ;; postmortem ;;