javaspring-bootwkhtmltopdfwkhtmltoimage

How to convert an HTML String to PDF using wkhtmltopdf or wkhtmltoImage in Java?


I am using wkhtmltoPDF to generate pdf from html string and It works fine for me as expected, but now I have to generate output as Image and I'm unable to find sample code or any library.

I have referred https://github.com/jhonnymertz/java-wkhtmltopdf-wrapper to generate pdf.

import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;

public class WKhtmlToPdf
 { 
    Pdf pdf = new Pdf();
    pdf.addPageFromString("html code");
    pdf.addToc();

 try{
     pdf.saveAs("Output.pdf");
   }
    catch (IOException e)
     {
     }
    catch (InterruptedException e)
     {     
     }
 }

I wanted an output an Image. Can any one tell me and I don't want to use Runtime.getRuntime().exe() method to generate output.

Thanks in advance.


Solution

  • wkhtmltopdf is a library which is part of the wkhtmltox project, which includes wkhtmltoimage too. You can inspire yourself from the wrapper you linked above to develop a wkhtmltoimage wrapper.

    Also, I generally use ImageMagick to generate images programmatically. IM4Java is an example wrapper to it for Java.