[Python] The same command runs perfectly when executed directly on the Linux server, but it...

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I have a Python script that uses pyhtml2pdf:


    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)


    When I run the command directly on a Linux server:

    DISPLAY=:99 python3 test.py 298.html 298.pdf


    it executes perfectly, and 298.pdf is generated. However, when I try to run this command from within a Java program using:

    String comds = "DISPLAY=:99 python3 test.py 298.html 298.pdf";
    Runtime.getRuntime().exec(comds);


    I encounter the error:

    2024-09-27 11:33:14 - ERROR>Traceback (most recent call last):

    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>


    How can I resolve this issue?

    chromedriver -v
    ChromeDriver 129.0.6668.70

    Continue reading...

Compartilhe esta Página