geckodriver icon indicating copy to clipboard operation
geckodriver copied to clipboard

Print orientation is not honored

Open dnrce opened this issue 2 years ago • 3 comments

When using the WebDriver print function, landscape orientation is not honored.

Additionally, when page dimensions are provided, the smaller dimension is always used as the page (PDF MediaBox) width and the larger as the page height, but content gets fit to the width and height as provided. (I'm not sure whether this should be a separate issue, but it may be related.)

System

  • Version: 0.30.0
  • Platform: macOS 12.1
  • Firefox: 98.0.2 (64-bit)
  • Selenium: N/A

Testcase

#!/bin/bash

# chromedriver
# WEBDRIVER=http://localhost:9515
# SESSION_ID=$(curl -s -XPOST -H 'Content-Type: application/json' ${WEBDRIVER}/session -d '{"capabilities": {"alwaysMatch": {"browserName": "chrome", "goog:chromeOptions": {"args": ["--headless"]}}}}' | jq -r '.value.sessionId')

# geckodriver
WEBDRIVER=http://localhost:4444
SESSION_ID=$(curl -s -XPOST -H 'Content-Type: application/json' ${WEBDRIVER}/session -d '{"capabilities": {}}' | jq -r '.value.sessionId')

curl -s -XPOST -H 'Content-Type: application/json' ${WEBDRIVER}/session/${SESSION_ID}/url -d '{"url": "https://www.mozilla.org/"}' | jq

curl -s -XPOST -H 'Content-Type: application/json' ${WEBDRIVER}/session/${SESSION_ID}/print -d '{"pageRanges": ["1"]}' \
  | jq -r '.value' | base64 -d > test.pdf

curl -s -XPOST -H 'Content-Type: application/json' ${WEBDRIVER}/session/${SESSION_ID}/print -d '{"pageRanges": ["1"], "orientation": "landscape"}' \
  | jq -r '.value' | base64 -d > test_landscape.pdf

curl -s -XPOST -H 'Content-Type: application/json' ${WEBDRIVER}/session/${SESSION_ID}/print -d '{"pageRanges": ["1"], "page": {"width": 5, "height": 20}}' \
  | jq -r '.value' | base64 -d > test_page_size.pdf

curl -s -XPOST -H 'Content-Type: application/json' ${WEBDRIVER}/session/${SESSION_ID}/print -d '{"pageRanges": ["1"], "page": {"width": 20, "height": 5}}' \
  | jq -r '.value' | base64 -d > test_page_size_width_height_reversed.pdf

curl -s -XDELETE ${WEBDRIVER}/session/${SESSION_ID} | jq

When using chromedriver, the script yields pages oriented as expected.

Trace-level log

Landscape orientation:

1648533064044	webdriver::server	DEBUG	-> POST /session/8d79a20f-66e1-41e5-afea-1da8eadfa877/print {"pageRanges": ["1"], "orientation": "landscape"}
1648533064047	Marionette	DEBUG	0 -> [0,4,"WebDriver:Print",{"background":false,"margin":{"bottom":1,"left":1,"right":1,"top":1},"orientation":"landscape","page":{"height":27.94,"width":21.59},"pageRanges":["1"],"scale":1,"shrinkToFit":true}]
1648533064048	Marionette	DEBUG	Got page ranges [1, 1]
1648533064339	Marionette	DEBUG	PDF output written to /var/folders/4_/y0rdmt9n1jdfvdhkyj2z9zm80000gn/T/Temp-e21f3c92-7339-471a-9580-c0a1ed0031f0/marionette.pdf
1648533064343	Marionette	DEBUG	0 <- [1,4,null,{"value":"JVBERi0xLjMKJcTl8uXrp/Og0MTGCjMgMCBvYmoKPDwgL0ZpbHRlciAvRmxhdGVEZWNvZGUgL0xlbmd0aCAzMjY1ID4+CnN0cmVhbQp4A ... Q2NDg1MGJmMjkwMDk3ZmVjNGEyMDVkMDEwZT4KPDQ2ZWVkNDY0ODUwYmYyOTAwOTdmZWM0YTIwNWQwMTBlPiBdID4+CnN0YXJ0eHJlZgoyNDMxNjIKJSVFT0YK"}]

Width and height:

1648533064840	webdriver::server	DEBUG	-> POST /session/8d79a20f-66e1-41e5-afea-1da8eadfa877/print {"pageRanges": ["1"], "page": {"width": 20, "height": 5}}
1648533064841	Marionette	DEBUG	0 -> [0,6,"WebDriver:Print",{"background":false,"margin":{"bottom":1,"left":1,"right":1,"top":1},"orientation":"portrait","page":{"height":5,"width":20},"pageRanges":["1"],"scale":1,"shrinkToFit":true}]
1648533064841	Marionette	DEBUG	Got page ranges [1, 1]
1648533065077	Marionette	DEBUG	PDF output written to /var/folders/4_/y0rdmt9n1jdfvdhkyj2z9zm80000gn/T/Temp-e21f3c92-7339-471a-9580-c0a1ed0031f0/marionette.pdf
1648533065077	Marionette	DEBUG	0 <- [1,6,null,{"value":"JVBERi0xLjMKJcTl8uXrp/Og0MTGCjMgMCBvYmoKPDwgL0ZpbHRlciAvRmxhdGVEZWNvZGUgL0xlbmd0aCAxNDk5ID4+CnN0cmVhbQp4A ... VkOTZhZjgwMGY3Y2RmYTMwOWM4NzZkMmFkMWE3Pgo8OTY3ZWQ5NmFmODAwZjdjZGZhMzA5Yzg3NmQyYWQxYTc+IF0gPj4Kc3RhcnR4cmVmCjg0ODAKJSVFT0YK"}]

dnrce avatar Mar 29 '22 05:03 dnrce

Thanks for filing! Marionette expects a landscape boolean parameter for some reason and is not following the spec. I filed https://bugzilla.mozilla.org/show_bug.cgi?id=1791819

juliandescottes avatar Sep 21 '22 16:09 juliandescottes

image image When I print bing.com, it seems background also not effected

cro4k avatar Sep 22 '22 12:09 cro4k

The background is handled by https://bugzilla.mozilla.org/show_bug.cgi?id=1783086. I'll update the summary so that we can keep both covered here.

whimboo avatar Sep 22 '22 19:09 whimboo

@whimboo @juliandescottes are there any news on the potential fix for the issue?

dskecse avatar Dec 05 '22 21:12 dskecse

Sadly we won't have the time to work on it within at least the next month. There is a change for early 2023. If it has a high priority for you, we would be more than happy to mentor you in case you want to fix it yourself.

whimboo avatar Dec 06 '22 07:12 whimboo

@whimboo that would be great, I'd love to fix the issue. what would be the best place to start from?

dskecse avatar Dec 12 '22 18:12 dskecse

That sounds great! May you be able to join us on Matrix via https://chat.mozilla.org/#/room/#webdriver:mozilla.org? You can find me there and I'm happy to chat and explain the first steps.

whimboo avatar Dec 12 '22 21:12 whimboo

@whimboo sure, I just sent a message in the room there

dskecse avatar Dec 13 '22 00:12 dskecse

This will be fixed in the Firefox 113 release.

whimboo avatar Mar 27 '23 14:03 whimboo