Print Margins Settings - Custom Margins

738 views
Skip to first unread message

Alexandre M Mello

unread,
Dec 30, 2021, 7:31:12 PM12/30/21
to ChromeDriver Users
Hello,

I have a running script to print a URL using ChromeDriver.
I could not find any way to set the custom margin printing settings.
The closest example I found is:

"marginsType": 2,

ex:

settings = { "recentDestinations": [{ "id": "Save as PDF", "origin": "local", "account": "" }], "selectedDestinationId": "Save as PDF", "version": 2, "isHeaderFooterEnabled": False, "mediaSize": { "height_microns": 210000, "name": "ISO_A5", "width_microns": 148000, "custom_display_name": "A5" }, "customMargins": {}, "marginsType": 2, "scaling": 175, "scalingType": 3, "scalingTypePdf": 3, "isCssBackgroundEnabled": True }

I have changed it to:

"marginsType": 3

And I tried to set the custom margins here:

"customMargins": {
"margin-top": "2cm",
"margin-bottom": "2cm",
"margin-left": "2cm",
"margin-right": "2cm"
},

And several other syntaxes that I tried to guess, but nothing worked.

It tries to load the setting but end with this error:

selenium.common.exceptions.TimeoutException: Message: script timeout

How can I set the custom margins?

I would appreciate any ideas.

Thank you!



Alexandre M Mello

unread,
Jan 2, 2022, 9:57:36 AM1/2/22
to ChromeDriver Users
I found a working solution by trial and error.
Here is how to set the Custom Print Margins on Selenium ChromeDriver:

"customMargins": "1cm, 0cm, 1cm, 0cm",
# Order: Top, right, bottom, left

And remove the marginsType:

# "marginsType": 2,

Note: It didn't work with "mm" in my testings.

In my previous example, it would be something like:

settings = { "recentDestinations": [{ "id": "Save as PDF", "origin": "local", "account": "" }], "selectedDestinationId": "Save as PDF", "version": 2, "isHeaderFooterEnabled": False, "mediaSize": { "height_microns": 210000, "name": "ISO_A5", "width_microns": 148000, "custom_display_name": "A5" }, "customMargins": "1cm, 0cm, 1cm, 0cm", "scaling": 175, "scalingType": 3, "scalingTypePdf": 3, "isCssBackgroundEnabled": True }

Best Regards,

Alexandre.
Reply all
Reply to author
Forward
0 new messages