I am trying to create a pdf file from a webpage by calling wkhtmltopdf via java ProcessBuilder on Centos OS. The problem is that when I run a simple class with the main method, the process ends with:
Command has terminated with status: 139
Output:
Error: Loading pages (1/6) ....
and creates an empty PDF file (size 0B)
I have included a method that prints the arguments with which I call wkhtmltopdf in the class, and when I copy the command and run it in bash, it works and creates pdf. Even more: when I run the same class in windows, it works fine. What can cause this error code 139? Maybe this is an error in wkhtmltopdf or am I something wrong?
Here is some more info:
OS:
[root@host sandbox]
Linux xxxxxx.com 2.6.32-279.22.1.el6.x86_64
[root@host bin]
Name:
wkhtmltopdf 0.12.0 03c001de254b857f08eba80b62d4b6490ffed41d
The command I'm trying to run using the process builder:
/root/wk/wkhtmltox/bin/wkhtmltopdf
http://xx.xx.xx.xx/url?param1=1¶m2=2
/root/sandbox/test.pdf
Code to generate pdf:
public String exportToPdf(final String bookmarkableUrl) {
String uuid = UUID.randomUUID().toString();
final String fullUrl = "http://" + hostName + ":" + port + bookmarkableUrl;
String generatedPdfPath = tempDirPath + "/EMF/" + uuid;
try {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(prepareCommandArguments(fullUrl, generatedPdfPath + PDF_FILE_EXTENSION));
Process start = processBuilder.start();
handleStream(start.getErrorStream());
handleStream(start.getInputStream());
start.waitFor();
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Error while generating PDF", e);
}
return generatedPdfPath;
}
EDIT: , :
private List<String> prepareCommandArguments(String inputUrl, String outputUrl) {
List<String> arguments = new ArrayList<>(15);
arguments.add(wkhtmltopdfLocation);
arguments.add("--window-status");
arguments.add("export-ready");
arguments.add("--encoding");
arguments.add("UTF-8");
arguments.add("--custom-header");
arguments.add("username");
arguments.add(exportUsername);
arguments.add("--custom-header");
arguments.add("password");
arguments.add(exportPassword);
arguments.add("--run-script");
arguments.add(getScriptFromFile(jsFilePath));
arguments.add(inputUrl);
arguments.add(outputUrl);
return arguments;
}
System.out.println(processBuilder.command()) Process.start():
/root/wk/wkhtmltox/bin/wkhtmltopdf, --window-status, export-ready, --encoding, UTF-8, --custom-header, username, admin, --custom-header, password, admin, --run-script, "\$('.idoc-comments-column').remove(); \$('.idoc-left-column').remove(); \$('.idoc-left-column').remove(); \$('#topHeader').remove(); \$('#header').remove(); \$('.tree-header.breadcrumb_header').remove(); \$('.idoc-middle-column.pull-left.idoc-first-row').remove(); \$('.idoc-middle-column.pull-left').remove(); \$('.pull-left.text-center').remove(); \$('html').addClass('print-override-overflow'); \$('.idoc-editor').css('width', '80%'); \$('.idoc-editor').css('font-size', '14px'); \$('.idoc-editor').css('max-width', 'none'); \$('.idoc-editor').css('margin-left', '10%'); \$('.idoc-editor').css('margin-right', '10%'); \$('.idoc-editor').css('margin-top', '5%'); \$('.idoc-editor').css('margin-bottom', '5%');", http: