1. <i id='KoQqM'><tr id='KoQqM'><dt id='KoQqM'><q id='KoQqM'><span id='KoQqM'><b id='KoQqM'><form id='KoQqM'><ins id='KoQqM'></ins><ul id='KoQqM'></ul><sub id='KoQqM'></sub></form><legend id='KoQqM'></legend><bdo id='KoQqM'><pre id='KoQqM'><center id='KoQqM'></center></pre></bdo></b><th id='KoQqM'></th></span></q></dt></tr></i><div id='KoQqM'><tfoot id='KoQqM'></tfoot><dl id='KoQqM'><fieldset id='KoQqM'></fieldset></dl></div>

      <small id='KoQqM'></small><noframes id='KoQqM'>

      <legend id='KoQqM'><style id='KoQqM'><dir id='KoQqM'><q id='KoQqM'></q></dir></style></legend>
      • <bdo id='KoQqM'></bdo><ul id='KoQqM'></ul>

        <tfoot id='KoQqM'></tfoot>

        使用 chromedriver 从 Selenium 打印 PDF

        时间:2023-07-05
      1. <small id='83v6J'></small><noframes id='83v6J'>

            <tbody id='83v6J'></tbody>
              <bdo id='83v6J'></bdo><ul id='83v6J'></ul>
                <tfoot id='83v6J'></tfoot>

                <i id='83v6J'><tr id='83v6J'><dt id='83v6J'><q id='83v6J'><span id='83v6J'><b id='83v6J'><form id='83v6J'><ins id='83v6J'></ins><ul id='83v6J'></ul><sub id='83v6J'></sub></form><legend id='83v6J'></legend><bdo id='83v6J'><pre id='83v6J'><center id='83v6J'></center></pre></bdo></b><th id='83v6J'></th></span></q></dt></tr></i><div id='83v6J'><tfoot id='83v6J'></tfoot><dl id='83v6J'><fieldset id='83v6J'></fieldset></dl></div>
                1. <legend id='83v6J'><style id='83v6J'><dir id='83v6J'><q id='83v6J'></q></dir></style></legend>
                2. 本文介绍了使用 chromedriver 从 Selenium 打印 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Selenium、chromedriver 和 python 将 html/css 内容打印为 PDF.

                  I am trying to implement printing html/css contents as PDF with Selenium, chromedriver and python.

                  我可以使用以下代码进行打印,但无法更改打印设置.我想以 Letter 大小打印,没有页眉/页脚.官方信息 chromedriver 或 Selenium 并没有告诉我很多,所以我遇到了麻烦.有谁知道如何更改打印设置或永远无法完成.

                  I could printing with a below code, but I cannot change printing setting. I would like to print in Letter size and no header/footer. Official information chromedriver or Selenium doesn't tell me a lot, so I'm in trouble. Does anyone know that how printing setting can be changed or it can never be done.

                  import json
                  import os
                  from selenium import webdriver
                  
                  # setting html path
                  htmlPath = os.getcwd() + "\sample.html"
                  addr = "file:///" + htmlPath
                  
                  # setting Chrome Driver
                  chromeOpt = webdriver.ChromeOptions()
                  appState = {
                     "recentDestinations": [
                          {
                              "id": "Save as PDF",
                              "origin": "local",
                              "account": ""
                          }
                      ],
                      "selectedDestinationId": "Save as PDF",
                      "version": 2
                  }
                  prefs = {
                      'printing.print_preview_sticky_settings.appState': json.dumps(appState)}
                  chromeOpt.add_experimental_option('prefs', prefs)
                  chromeOpt.add_argument('--kiosk-printing')
                  driver = webdriver.Chrome('.\bin\chromedriver', options=chromeOpt)
                  
                  # HTML open and print
                  driver.get(addr)
                  driver.execute_script('return window.print()')
                  

                  推荐答案

                  添加 --headless 并尝试如下:

                  Add --headless and try it like this:

                  pdf = driver.execute_cdp_cmd("Page.printToPDF", {
                    "printBackground": True
                  })
                  
                  import base64
                  
                  with open("file.pdf", "wb") as f:
                    f.write(base64.b64decode(pdf['data']))
                  

                  这里有一些选项可以摆弄

                  这篇关于使用 chromedriver 从 Selenium 打印 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Selenium 找不到元素,即使它们存在 下一篇:本地主机端点到 DynamoDB 本地与 Boto3

                  相关文章

                  <legend id='mReKo'><style id='mReKo'><dir id='mReKo'><q id='mReKo'></q></dir></style></legend>

                        <bdo id='mReKo'></bdo><ul id='mReKo'></ul>
                    1. <i id='mReKo'><tr id='mReKo'><dt id='mReKo'><q id='mReKo'><span id='mReKo'><b id='mReKo'><form id='mReKo'><ins id='mReKo'></ins><ul id='mReKo'></ul><sub id='mReKo'></sub></form><legend id='mReKo'></legend><bdo id='mReKo'><pre id='mReKo'><center id='mReKo'></center></pre></bdo></b><th id='mReKo'></th></span></q></dt></tr></i><div id='mReKo'><tfoot id='mReKo'></tfoot><dl id='mReKo'><fieldset id='mReKo'></fieldset></dl></div>
                      <tfoot id='mReKo'></tfoot>

                      <small id='mReKo'></small><noframes id='mReKo'>