在 Chrome 中使用 python 将 HTML 文件另存为 PDF 时,页面大小规范不起作用

问题描述 投票:0回答:1

我想在 Chrome 上打开 HTML 文件并将其另存为 PDF, 我使用python编写了以下代码,但即使我将代码中appState中的“pageSize”设置为A3、A5、信纸大小或其他大小, 输出 PDF 文件的页面尺寸始终为 A4。

如果我更改 appState 中其他属性“isLandscapeEnabled”或“isHeaderFooterEnabled”的值,它效果很好。 所以,似乎“pageSize”属性不起作用。

而且,我发现当控制面板->区域->格式(F)设置为日本时,输出PDF文件的页面尺寸将始终为A4, 如果我将其设置为美国,它将始终是信纸大小。 所以,我认为“pageSize”始终设置为默认值。

我是一个Python初学者,我还不太理解它。 我的代码有问题吗?

请给我任何建议。

  • Python 3.9.6
  • 硒3.141.0
  • Chrome 94.0.4606.71
  • Chrome驱动程序94.0.4606.61
  • Windows10
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
import time

def PrintSetUp():
    chopt=webdriver.ChromeOptions()
    appState = {
        "recentDestinations": [
            {
                "id": "Save as PDF",
                "origin": "local",
                "account":""
            }
        ],
        "selectedDestinationId": "Save as PDF",
        "version": 2,
        "isLandscapeEnabled": False,
        "pageSize": 'A3', 
        "marginsType": 0,
        "scalingType": 3 , 
        "scaling": "100" ,
        "isHeaderFooterEnabled": False, #ヘッダーとフッター
        "isCssBackgroundEnabled": True, #背景のグラフィック
    }
    
    prefs = {'printing.print_preview_sticky_settings.appState':
             json.dumps(appState),
             "download.default_directory": "~/Downloads"
             }
    chopt.add_experimental_option('prefs', prefs)
    chopt.add_argument('--kiosk-printing')
    return chopt

def main_WebToPDF(BlogURL):
    chopt = PrintSetUp()
    driver_path = "C:/Work/pythonTest/Ver94/chromedriver_win32/chromedriver.exe" #webdriverのパス
    driver = webdriver.Chrome(executable_path=driver_path, options=chopt)
    driver.implicitly_wait(10) # 秒 暗示的待機 
    driver.get(BlogURL) #ブログのURL 読み込み
    WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)  # ページ上のすべての要素が読み込まれるまで待機(15秒でタイムアウト判定)
    driver.execute_script('return window.print()') #Print as PDF
    time.sleep(10) #ファイルのダウンロードのために10秒待機
    driver.quit() #Close Screen
    
if __name__ == '__main__':
    BlogURLList=["file://C:/Work/pythonTest/1_pdf.html",
                 "file://C:/Work/pythonTest/2_pdf.html",
                 "file://C:/Work/pythonTest/3_pdf.html"]
    for BlogURL in  BlogURLList:
        main_WebToPDF(BlogURL)
python html pdf selenium-chromedriver
1个回答
0
投票

我在“元素”选项卡中找到了完整的媒体大小,因此您可以尝试以下代码:

 appState = {
    "recentDestinations": [
        {
            "id": "Save as PDF",
            "origin": "local",
            "account": ""
        }
    ],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
    "mediaSize": {"height_microns":841000,"imageable_area_bottom_microns":0,
    "imageable_area_left_microns":0,"imageable_area_right_microns":594000,
    "imageable_area_top_microns":841000,"name":"ISO_A1",
    "width_microns":594000,"custom_display_name":"A1"}
}

prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(appState), "savefile.default_directory": folderpath}

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome(options=chrome_options)
driver.get(url)
time.sleep(3)
driver.execute_script('window.print();')
time.sleep(5)
driver.quit()

您可以在此处更改检查您的纸张尺寸: 在此输入图片描述

© www.soinside.com 2019 - 2024. All rights reserved.