1

I am trying to use wkhtml2pdf in JBoss with Java to create PDF files. I was successful, but it only creates a PDF file from my login-page, rather than from the jsp file.

I read that it is possible to add parameters like username or password, and this is what I did:

String command = applicationLocation + "wkhtmltopdf.exe " + "--post j_username=" + username +" --post j_password=" + password + " " + reqURL + "?" + reqQuery + " c:/PDF/" + folderName + "/" + id + "/" + folderName + ".pdf";

But that doesn't create a PDF file, not even an error output. This is how my class looks like:

public class GeneratePDF {

    String logUserId = "0";
    public String path = "c:/PDF";
    String applicationLocation = "C:\\Program Files\\wkhtmltopdf\\";

    /**
     * This method creates a command which calls wkhtmltopdf from Google in order to create a PDF file.
     * 
     * @param reqURL
     * @param reqQuery
     * @param folderName
     * @param id
     */
    public void genrateCmd(String reqURL, String username, String password, String reqQuery, String folderName, String id) {

        try {
            // make sure c:/eGP exists
            File destFoldereGP = new File("c:/eGP");
            if (destFoldereGP.exists() == false) {
                destFoldereGP.mkdirs();
                System.out.println("successfully created c:/eGP");
            }

            // make sure c:/PDF exists
            File destFolderPDF = new File("c:/PDF/");
            if (destFolderPDF.exists() == false) {
                destFolderPDF.mkdirs();
                System.out.println("successfully created c:/PDF");
            }

            // make sure c:/PDF/foldername exists
            File destFolder = new File("c:/PDF/" + folderName);
            if (destFolder.exists() == false) {
                destFolder.mkdirs();
                System.out.println("successfully created c:/PDF/foldername");
            }

            // make sure c:/PDF/foldername/id exists
            File destFolder2 = new File("c:/PDF/" + folderName + "/" + id);
            if (destFolder2.exists() == false) {
                destFolder2.mkdirs();
                System.out.println("successfully created c:/PDF/foldername/id");
            }

            //For Image change 'wkhtmltopdf.exe' to 'wkhtmltoimage.exe' and '.pdf' to '.jpeg'
            String command = applicationLocation + "wkhtmltopdf.exe " + "--post j_username=" + username +" --post j_password=" + password + " " + reqURL + "?" + reqQuery + " c:/PDF/" + folderName + "/" + id + "/" + folderName + ".pdf";
            System.out.println("Command to create PDF: " + command);
            Runtime.getRuntime().exec(command);

            System.out.println("Successfully created a new PDF file.");
        } catch (IOException e1) {
            System.out.println("Exception: " + e1);
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

Any idea how to create PDFs with wkhtml2pdf and form based authentication in JBoss? Thanks a lot.

nimrod
  • 5,595
  • 29
  • 85
  • 149
  • Does the command work as expected from command line (outside of java program)? – DRCB Jul 10 '12 at 08:25
  • yes, without "--post j_username=" + username +" --post j_password=" it just creates a pdf from my login-page. – nimrod Jul 10 '12 at 08:28
  • Do you mean you can generate PDFs for other pages only from command line, but not from java? – DRCB Jul 10 '12 at 08:56
  • No, I can create PDF files without any problem. My only problem is that I can not create a PDF file for a page that is using form based authentication from JBoss. That's why I tried to add "--post j_username=" + username +" --post j_password=", but that did not work. My question is no, how can I accomplish that? – nimrod Jul 10 '12 at 09:02

0 Answers0