我有一个使用 pyhtml2pdf 的 Python 脚本:
import os
import sys
from pyhtml2pdf import converter
def convert_html_to_pdf(html_path, pdf_path):
absolute_html_path = os.path.abspath(html_path)
converter.convert(f'file:///{absolute_html_path}', pdf_path, 60, False, 0, False)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python3 test.py <html_path> <pdf_path>")
else:
html_path = sys.argv[1]
pdf_path = sys.argv[2]
convert_html_to_pdf(html_path, pdf_path)
当我直接在Linux服务器上运行命令时:
DISPLAY=:99 python3 test.py 298.html 298.pdf
它完美执行,并生成 298.pdf。但是,当我尝试使用以下命令从 Java 程序中运行此命令时:
String comds = "DISPLAY=:99 python3 test.py 298.html 298.pdf";
Runtime.getRuntime().exec(comds);
我遇到错误:
2024-09-27 11:33:14 - 错误>回溯(最近一次调用最后一次):
2024-09-27 11:33:14 - ERROR>Traceback (most recent call last):
2024-09-27 11:33:14 - ERROR> File "test.py", line 15, in <module>
2024-09-27 11:33:14 - ERROR> convert_html_to_pdf(html_path, pdf_path)
2024-09-27 11:33:14 - ERROR> File "test.py", line 7, in convert_html_to_pdf
2024-09-27 11:33:14 - ERROR> converter.convert(f'file:///{absolute_html_path}', pdf_path, 20, False, 0, False)
2024-09-27 11:33:14 - ERROR> File "/usr/local/lib/python3.6/site-packages/pyhtml2pdf/converter.py", line 39, in convert
2024-09-27 11:33:14 - ERROR> source, timeout, install_driver, print_options)
2024-09-27 11:33:14 - ERROR> File "/usr/local/lib/python3.6/site-packages/pyhtml2pdf/converter.py", line 79, in __get_pdf_from_html
2024-09-27 11:33:14 - ERROR> driver = webdriver.Chrome(options=webdriver_options)
2024-09-27 11:33:14 - ERROR> File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
2024-09-27 11:33:14 - ERROR> self.service.start()
2024-09-27 11:33:14 - ERROR> File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 98, in start
2024-09-27 11:33:14 - ERROR> self.assert_process_still_running()
2024-09-27 11:33:14 - ERROR> File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
2024-09-27 11:33:14 - ERROR> % (self.path, return_code)
2024-09-27 11:33:14 - ERROR>selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -11
2024-09-27 11:33:14 - ERROR>
如何解决这个问题?
chromedriver -v
ChromeDriver 129.0.6668.70
但是,在 Java 代码中直接设置 DISPLAY=:99 可能无法按预期工作。最好确保您的 X11 环境为您的 Java 应用程序正确配置,并让它自动获取 DISPLAY 变量。
执行命令之前设置 DISPLAY 环境变量:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MyPythonTest {
public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("DISPLAY", "99");
String command = " /Users/xxx/IdeaProjects/test/venv/bin/Python /Users/xxx/IdeaProjects/test/test.py /Users/xxx/IdeaProjects/test/298.html /Users/xxx/IdeaProjects/test/298.pdf";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
if (exitCode != 0) {
System.err.println("Script exited with error code: " + exitCode);
}
}
}