This topic contains 4 replies, has 2 voices, and was last updated by  Garry Christensen 7 years, 9 months ago.

os.execute and inverted commas

  • I came across some interesting behaviour today. As some background, I need to receive an ORU message, convert the result to PDF and then attach the PDF for forwarding to a destination. I’m using the information from the inbound message and creating an HTML file then using wkHTMLToPDF to generate the PDF.

    The following works OK and generates the PDF:

    os.execute('"c:\\program files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" c:\\temp\\17086.htm  c:\\temp\\17086.pdf')

    <returns 0>

    But, I wanted to have the source and destination path quoted because there may be spaces in the path so I tried this but it failed:

    os.execute('"c:\\program files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" "c:\\temp\\17086.htm"  "c:\\temp\\17086.pdf"')

    <returns 1>

    The following also failed:

    os.execute('"c:\\program files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" "c:\\temp\\17086.htm"  c:\\temp\\17086.pdf')

    <returns 1>

    I also wanted to include a footer so I unsuccessfully tried:

    os.execute('"c:\\program files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" c:\\temp\\17086.htm --footer-right "Page [Page] of [Topage]" c:\\temp\\17086.pdf')

    <returns 1>

    In tried escaping the quotes as described in the documentation:

    os.execute('\"c:\\program files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\" c:\\temp\\17086.htm  c:\\temp\\17086.pdf')

    <returns 0, unquoted file path>

    os.execute('\"c:\\program files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\" \"c:\\temp\\17086.htm\"  c:\\temp\\17086.pdf')

    <returns 1, quoted file path>

    It just seems that only one set of quotes works. So I tried:

    os.execute('cd "c:\\program files\\wkhtmltopdf\\bin\\" && wkhtmltopdf.exe "c:\\temp\\17086.htm"  "c:\\temp\\17086.pdf"')

    <returns 0>

    IT WORKED!!!
    So there’s the answer. Now I can add the footer bits

    os.execute('cd "c:\\program files\\wkhtmltopdf\\bin\\" && wkhtmltopdf.exe "c:\\temp\\17086.htm" --footer-right "Page [Page] of [Topage]" "c:\\temp\\17086.pdf"')

    <returns 0>

    Still works.

    So, any idea why I cannot quote the path to the exe and include quotes in the parameters?

    While the quoting issue you experienced is interesting and perhaps a bit inexplicable, there are at least three other potential solutions to the original problem.

    1. Create a batch file that accepts file names as parameters (%1,%2, etc.) that calls the executable and place it in the Iguana installation directory, then call the batch file from os.execute()
    2. Add the path to the executable (c:\program files\wkhtmltopdf\bin\) to the System environment’s PATH, which would eliminate the need to supply the directory name as part of the executable
    3. Use the 8.3 path to call the executable (you can determine this using dir /X at the command prompt); this would look something like “C:\\PROGRA~1\\WKHTML~1\\BIN\\WKHTML~1.EXE”. If the paths to the source and target files have spaces in them, you’ll likely also need to modify those similarly.

    All would circumvent the issue of spaces within path names on Windows.

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊

    Hi Jeff,
    Thanks for the ideas.
    I did try using a batch file, although I created it on the fly with the full command line rather than passing the parameters. Again, the results were ‘interesting’.

    A batch file that consisted a single line with the path to the executable quoted failed:
    "c:\program files\wkhtmltopdf\bin\wkhtmltopdf.exe" "c:\temp\17086.htm" "c:\temp\17086.pdf"

    A batch file that changed the directory first was successful:
    cd "c:\program files\wkhtmltopdf\bin\"
    wkhtmltopdf.exe "c:\temp\17086.htm" "c:\temp\17086.pdf"

    When I tried this using a batch file and passing the parameters, I found the same pattern. Curiouser and curiouser.

    I’m sure your other suggestions will also work as they remove the need for quoting the path to the exe. Thanks for the input.

    I was thinking more along the lines of a batch file that did the following:


    @echo off
    PATH=%PATH%;C:\Program Files\wkhtmltopdf\bin
    cd \temp
    wkhtmltopdf %BASEFILE%.html %BASEFILE%.pdf

    call it from os.execute():

    local rc = os.execute([[CMD.EXE /C PDFME.CMD 17086]])

    (untested, may require odd quoting)

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊

    Thanks Jeff. I can’t see any reason it won’t work.

You must be logged in to reply to this topic.