What are you struggling with? If you want an image output, there a reason you want to use KaTeX for this, and can't just use actual LaTeX?
KaTeX depends on the browser engine to do most of the layout, so generating an image would require a tool which converts the HTML nodes back into an image. There are some tools that can do that (like html2canvas), but using LaTeX to generate a PDF and then converting that to an image is probably going to be easier.