Question

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
Was it helpful?

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"

OTHER TIPS

This works just fine for me in Windows 10 1809 Just FYI took me forever to figure it out.

@echo off

"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" estimate.html estimate.pdf

echo Complete!
@pause

This happened to me on windows when trying to run it through system call in a PHP application. The quick and dirty solution is to Copy the contents of the bin directory to the directory of your php/web application.

I've tried setting the path, restarting the web server etc. It works correctly when invoked from the command line but not when run through a system call in a PHP app... even if you give it the full path.

Once you port your app to a correctly configured unix based web server it will work without having to mess about copying files.

Life is too short to figure out MSFT's quirks but if someone does figure it out I'd be happy to know!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top