Search code examples
batch-filecmdwkhtmltopdf

wkhtmltopdf in batch file


I am trying to make batch file for wkhtmltopdf.exe to convert html to pdf, and come up with this code.

   @echo off &setlocal enabledelayedexpansion
    for %%i in (*.html) do (
        set "line="
        for %%j in ("%%~ni.*") do set line=!line! "%%~j"
        start "" wkhtmltopdf.exe !line!
    )

However, it din't convert anything and gave 2 lines of result

wkhtmltopdf.exe awb112312.html awb112312.pdf
wkhtmltopdf.exe invoice.html invoice.pdf

Solution

  • I suppose, there is the error message

    wkhtmltopdf.exe not recognized as an internal or external command, operable program or batch file.
    

    output on running this batch file.

    The reason is wkhtmltopdf.exe is used in batch file without complete path. Therefore Windows searches first in current working directory of the batch file and next in all directories included in environment variable PATH separated by semicolons for the file wkhtmltopdf.exe and does not find it anywhere as the application is most likely installed into a subdirectory of %ProgramFiles% or %ProgramFiles(x86)%.

    wkhtmltopdf is a console application according to homepage of wkhtmltopdf and therefore the command START should not be really needed at all.

    Also the inner FOR loop does not make much sense for me if simply each *.html file in current working directory should be converted to a PDF file. Why searching for other files with same name as already found *.html file with any file extension in current working directory and concatenate all found file names to one string?

    I think, all really needed is:

    @echo off
    for %%i in (*.html) do "Complete path to wkhtmltopdf\wkhtmltopdf.exe" "%%i" "%%~ni.pdf"