使用Selenium和ChromeDriver,自动缩放prin上的页面大小

2024-04-29 06:09:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个脚本,用Chrome自动打印一组网页。如果要手动打印,我会从“比例”下拉列表中选择“自定义”,并在下面的输入字段中输入50。你知道吗

enter image description here

当我使用Selnium with ChromeDriver自动批量打印这些页面时,我不知道要传递什么参数来复制这个设置。你知道吗

appState = { "recentDestinations": [{
                "id": "Save as PDF",
                "origin": "local",
                "account": "",
                "printing.scaling": 'Custom', # <== Does it go here?
             }],
             "selectedDestinationId": "Save as PDF",
             "version": 2,
             "printing.scaling": 'Custom',  # <== Or here?
           }
profile = { 'printing.print_preview_sticky_settings.appState': json.dumps(appState),
            'printing.print_header_footer': False,

            # So many different versions of things I have tried :-(
            'printing.scaling': 'Custom',
            'printing.scaling_type': 'Custom',
            'print_preview.scaling': 'Custom',
            'print_preview.scaling_type': 'Custom',
            'printing.custom_scaling': True,
            'printing.fit_to_page_scaling': 50,
            'printing.page_scaling': True,
          }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
br = webdriver.Chrome(options=chrome_options)

上面显示的所有不同的选项都是在阅读了大量铬源试图得到提示后的猜测。你知道吗

https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markuphttps://chromium.googlesource.com/chromium/src/+/master/printing/print_job_constants.cchttps://chromium.googlesource.com/chromium/src/+/master/printing/print_job_constants.h

我没有线索了。。。有人有什么想法吗?谢谢您!你知道吗


Tags: httpssrcpdfsaveascustomchromepreview
1条回答
网友
1楼 · 发布于 2024-04-29 06:09:49

我知道这不完全是你问题的答案,但可能是你问题的解决办法。你知道吗

您是否尝试过不传递首选项,而只是缩小页面内容,然后打印页面?你知道吗

driver.execute_script("document.body.style.zoom='50%'")
# continue with printing

对我来说,缩小并使用Default缩放选项打印就像打印Scale设置为50的页面一样

相关问题 更多 >