Search code examples
javaprocesswkhtmltopdfprocessbuilder

Cant get the wkhtmltopdf attributes to work with in java code


I am using wkhtmltopdf converter to convert html file to pdf. It works fine except that the output does not fit the page in pdf. Using --zoom 2 fixes the problem at command line but with using the same in my code doesnt do anything. below is my code.

String wkhtmltopdf = "C:/Program Files (x86)/wkhtmltopdf/wkhtmltopdf.exe ";
String switches = " --zoom 2 ";

ProcessBuilder pb = new ProcessBuilder(wkhtmltopdf, switches, f.getAbsolutePath(), pdfFileName);

Process process = pb.start();

In command line this works just fine.

C:\Program Files (x86)\wkhtmltopdf>wkhtmltopdf.exe --zoom 2 C:\Users\D
esktop\eclipse-jee-indigo-SR2-win32-x86_64\eclipse\temphtml1.htm C:\Users\Desktop\temp\test.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

Could someone let me know what I am doing wrong?


Solution

  • Using Runtime in place of processBuilder fixed it. I still dont know why "--zoom 2" is not excepted as a parameter in processBuilder. However, below is the working code.

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(
      "C:/Program Files (x86)/wkhtmltopdf/wkhtmltopdf.exe --zoom 2 " 
        + temphtmlfile.getAbsolutePath()
        + " " 
        + filePdf.getAbsolutePath()) ;