Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Print Page As PDF Keyword #1851

Merged
merged 8 commits into from May 14, 2024
18 changes: 18 additions & 0 deletions atest/acceptance/keywords/print_page.robot
@@ -0,0 +1,18 @@
*** Settings ***
Documentation Suite description
Suite Setup Go To Page "non_ascii.html"
Resource ../resource.robot

*** Test Cases ***
Print Page As PDF Without Print Options
emanlove marked this conversation as resolved.
Show resolved Hide resolved
Print Page As PDF

Provide Print Options From Module
emanlove marked this conversation as resolved.
Show resolved Hide resolved
# ${print_options}= Evaluate sys.modules['selenium.webdriver'].common.print_page_options() sys, selenium.webdriver
${print_options}= Evaluate selenium.webdriver.common.print_page_options.PrintOptions()
# Set To Dictionary ${print_options} scale 0.5
# Evaluate ${print_options}.scale=0.5
# Set Variable ${print_options.scale} 0.5
# Evaluate ${print_options.scale}=0.5
Evaluate setattr($print_options, 'scale', 0.5)
Print Page As PDF ${print_options}
69 changes: 68 additions & 1 deletion src/SeleniumLibrary/keywords/screenshot.py
Expand Up @@ -14,10 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from typing import Union
from typing import Optional, Union
from base64 import b64decode

from robot.utils import get_link_path
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.print_page_options import PrintOptions

from SeleniumLibrary.base import LibraryComponent, keyword
from SeleniumLibrary.utils.path_formatter import _format_path
Expand Down Expand Up @@ -235,3 +237,68 @@ def _embed_to_log_as_file(self, path, width):
f'<a href="{src}"><img src="{src}" width="{width}px"></a>',
html=True,
)

@keyword
# def print_page_as_pdf(self, print_options: Optional[PrintOptions]=None,):
emanlove marked this conversation as resolved.
Show resolved Hide resolved
# def print_page_as_pdf(self, print_options: Optional[Union[PrintOptions, dict]]=None):
# def print_page_as_pdf(self, print_options: Union[PrintOptions, dict, None]=None):
emanlove marked this conversation as resolved.
Show resolved Hide resolved
# """ Print the current page as a PDF
#
# """
# if not print_options:
# print_options = PrintOptions()
# print_options.page_ranges = ['-']
def print_page_as_pdf(self,
background=None,
margin_bottom=None,
margin_left=None,
margin_right=None,
margin_top=None,
orientation=None,
page_height=None,
page_ranges=['-'],
page_width=None,
scale=None,
shrink_to_fit=None,
path_to_file=None,
):
""" Print the current page as a PDF

``page_ranges`` defaults to `['-']` or "all" pages. ``page_ranges`` takes a list of
strings indicating the ranges.

The page size defaults to 21.59 for ``page_width`` and 27.94 for ``page_height``.
This is the equivalent size of US-Letter. The assumed units on these parameters
is centimeters.

The default margin for top, left, bottom, right is `1`. The assumed units on
these parameters is centimeters.

The default ``orientation`` is `portrait`. ``orientation`` can be either `portrait`
or `landscape`.

The default ``scale`` is `1`. ``scale`` must be greater than or equal to `0.1` and
less than or equal to `2`.

``background`` and ``scale_to_fite`` can be either `${True}` or `${False}`..

If all print options are None then a pdf will failed to print silently.s
"""

print_options = PrintOptions()
print_options.background = background
print_options.margin_bottom = margin_bottom
print_options.margin_left = margin_left
print_options.margin_right = margin_right
print_options.margin_top = margin_top
print_options.orientation = orientation
print_options.page_height = page_height
print_options.page_ranges = page_ranges
print_options.page_width = page_width
print_options.scale = scale
print_options.shrink_to_fit = shrink_to_fit

base64code = self.driver.print_page(print_options)
pdfdata = b64decode(base64code)
with open('test.pdf', mode='wb') as pdf:
emanlove marked this conversation as resolved.
Show resolved Hide resolved
pdf.write(pdfdata)