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

        PDF printing from Selenium with chromedriver(使用 chromedriver 从 Selenium 打印 PDF)
      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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

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

                            <bdo id='mReKo'></bdo><ul id='mReKo'></ul>
                          • <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'>