Search code examples
wkhtmltopdf

Is there a way to pass an html string as command line argument to wkhtmltopdf.exe?


I am creating an Azure function to run wkhtmltopdf.exe within that to convert an html string to pdf and am needing to pass the html string to the .exe. Is there any way to achieve this?

thanks Angelo


Solution

  • This answer is for Linux, however I believe it will work the same on Windows (but I can't test that currently).

    When printing the help text for wkhtmltopdf (for example by running the program from the terminal without any arguments) the first line is as follows:

    You need to specify at least one input file, and exactly one output file
    Use - for stdin or stdout
    

    This means that instead of providing an input filename you can put - at the position of the filename and the input will be read from stdin (standard input). Standard input is typed into the terminal and is ended by CTRL-D on Linux (CTRL-Z on Windows).

    So you could do the following:

    wkhtmltopdf - fromstdin.pdf
    <h1>this is a header</h1>CTRL-D
    Loading page (1/2)
    Printing pages (2/2)                                               
    Done
    

    However, a more useful way is to redirect output from another command to stdin, using a pipe (the | symbol). Any output from any program can be piped, for example echo (on both Linux and Windows). Example:

    echo "<b>this is bold</b> <i>this is italic</i>"|wkhtmltopdf - fromstdin.pdf
    Loading page (1/2)
    Printing pages (2/2)                                               
    Done   
    

    Resulting pdf page:

    enter image description here