You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用自定义字体时ChromeDriver崩溃问题求助

问题:ChromeDriver调用Page.printToPDF时因页眉模板@font-face导致页面崩溃

当通过ChromeDriver调用Page.printToPDF命令,且传入的页眉模板HTML中包含@font-face自定义字体样式时,会触发页面崩溃错误,移除自定义字体或@font-face定义后功能恢复正常。

复现代码

Java调用代码

chromeDriver.get(url);
var pdf = (String) ((HasCdp) chromeDriver)
    .executeCdpCommand(
        "Page.printToPDF",
        Map.of("headerTemplate", "下方的HTML内容", ... 其他参数))
    .get("data");

触发崩溃的页眉模板HTML

<html>
<head>
<style type="text/css">
  @font-face {
    font-family: "Titillium Web";
    src: url(https://fonts.gstatic.com/s/titilliumweb/v15/NaPDcZTIAOhVxoMyOr9n_E7ffGjEGIVzY4SY.woff2) format('woff2');
  }
</style>
</head>
<body>
  <div>
    <div style="font-weight: 600; font-size: 15pt; font-family: 'Titillium Web'; ">Testing header</div>
    <div style="font-size: 10pt; font-family: 'Titillium Web'; ">This is working as a standalone html, but crashing in ChromeDriver</div>
 </div>
</body>
</html>

错误堆栈信息

org.openqa.selenium.WebDriverException: unknown error: session deleted because of page crash
from unknown error: cannot determine loading status
from tab crashed
  (Session info: headless chrome=105.0.5195.54)
Build info: version: '4.5.0', revision: 'fe167b119a'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-125-generic', java.version: '17.0.4'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [44af04a45bd843fc4fc23163eee329d6, executeCdpCommand {cmd=Page.printToPDF, params={displayHeaderFooter=true, headerTemplate=[something like the html above], marginLeft=0.7874015748031497, marginBottom=1.299212598425197, paperWidth=8.267716535433072, marginTop=1.299212598425197, marginRight=0.7874015748031497, paperHeight=11.692913385826772}}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 105.0.5195.54, chrome: {chromedriverVersion: 105.0.5195.52 (412c95e51883..., userDataDir: /tmp/.com.google.Chrome.2XGZbo}, goog:chromeOptions: {debuggerAddress: localhost:34545}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: LINUX, proxy: Proxy(), se:cdp: ws://localhost:34545/devtoo..., se:cdpVersion: 105.0.5195.54, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 600000, pageLoad: 600000, script: 600000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 44af04a45bd843fc4fc23163eee329d6
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[?:?]
    at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]
    at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[?:?]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[?:?]
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:547) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.remote.RemoteExecuteMethod.execute(RemoteExecuteMethod.java:40) ~[selenium-remote-driver-4.5.0.jar:?]
    at org.openqa.selenium.chromium.AddHasCdp$1.executeCdpCommand(AddHasCdp.java:58) ~[selenium-chromium-driver-4.5.0.jar:?]
    at org.openqa.selenium.chromium.ChromiumDriver.executeCdpCommand(ChromiumDriver.java:212) ~[selenium-chromium-driver-4.5.0.jar:?]

解决办法

  • 内嵌Base64格式字体:将字体文件转换为Base64编码,直接内嵌到@font-face的src中,避免远程加载字体时的异步问题引发崩溃。示例:
@font-face {
  font-family: "Titillium Web";
  src: url(data:font/woff2;base64,[字体文件的Base64编码内容]) format('woff2');
}
  • 升级Chrome及ChromeDriver版本:该问题属于旧版本Chrome/ChromeDriver的已知Bug,升级到110及以上版本可修复此问题。
  • 避免页眉模板中的远程资源:确保页眉模板内所有资源(字体、图片等)都本地内嵌,不发起任何远程网络请求,降低崩溃概率。
  • 增加打印前的等待时间:调用printToPDF前,使用WebDriverWait等待页面完全加载稳定,确保所有资源就绪后再执行打印操作。

内容的提问来源于stack exchange,提问作者Dan D.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 11:55:18