Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ProcessBuilder不使用wkhtmltopdf_Java_Wkhtmltopdf_Runtime.exec_Processbuilder - Fatal编程技术网

Java ProcessBuilder不使用wkhtmltopdf

Java ProcessBuilder不使用wkhtmltopdf,java,wkhtmltopdf,runtime.exec,processbuilder,Java,Wkhtmltopdf,Runtime.exec,Processbuilder,我正在尝试从网页生成pdf,通过Centos上的java ProcessBuilder调用wkhtmltopdf。问题是,当我使用main方法运行一个简单类时,该进程将以以下方式终止: Command has terminated with status: 139 Output: Error: Loading pages (1/6) .... 并创建一个空pdf文件(大小为0B) 我已经包含了一个方法,它打印我在类中调用wkhtmltopdf时使用的参数,当我复制命令并在bash中运行它时,

我正在尝试从网页生成pdf,通过Centos上的java ProcessBuilder调用wkhtmltopdf。问题是,当我使用main方法运行一个简单类时,该进程将以以下方式终止:

Command has terminated with status: 139
Output:

Error: Loading pages (1/6) ....
并创建一个空pdf文件(大小为0B)

我已经包含了一个方法,它打印我在类中调用wkhtmltopdf时使用的参数,当我复制命令并在bash中运行它时,它工作并创建pdf。更重要的是:当我在windows中运行完全相同的类时,它工作得很好。什么会导致此错误代码139?这可能是wkhtmltopdf中的错误,还是我做错了什么

以下是更多信息:

操作系统:

我尝试使用process builder运行的命令:

 /root/wk/wkhtmltox/bin/wkhtmltopdf --window-status export-ready 
     --encoding UTF-8 
     --custom-header username username
     --custom-header password pass
     --run-script "<some correctly escaped js>" 
     http://xx.xx.xx.xx/url?param1=1&param2=2 
     /root/sandbox/test.pdf
编辑:添加用于创建带有命令参数的列表的代码:

private List<String> prepareCommandArguments(String inputUrl, String outputUrl) {
    List<String> arguments = new ArrayList<>(15);
    // absolute path to the wkhtmltopdf executable
    arguments.add(wkhtmltopdfLocation);
    // Wait until window.status is equal to this string before rendering page
    arguments.add("--window-status");
    arguments.add("export-ready");
    // Set the default text encoding, for input
    arguments.add("--encoding");
    arguments.add("UTF-8");
    // Set an additional HTTP header for system username
    arguments.add("--custom-header");
    arguments.add("username");
    arguments.add(exportUsername);
    // Set an additional HTTP header for system user password 
    arguments.add("--custom-header");
    arguments.add("password");
    arguments.add(exportPassword);
    // Run this additional javascript after the page is done loading
    // Used to remove irrelevant divisions and spanning of 
    // the html page, leaving only the print preview of the document
    arguments.add("--run-script");
    arguments.add(getScriptFromFile(jsFilePath));
    // Bookmarkable url of the document 
    arguments.add(inputUrl);    
    // Path to the generated pdf
    arguments.add(outputUrl);
    return arguments;
}

您不需要使用
ProcessBuilder
进行任何shell转义。尝试按原样传递JS代码,但不使用转义的美元符号和双引号。

能否显示
prepareCommandArguments
的代码?您的
getScriptFromFile
方法是否返回一个包含在双引号内的字符串。您确定脚本文件本身没有任何双引号,这可能会提前终止外部双引号,因为它似乎是程序中的分段错误,因此是一个bug。根据另一个版本的更新解决了这个问题。
System.out.println(processBuilder.command())
如果放在
processBuilder.start()
之前,它的输出是什么?我想你不需要用
processBuilder
进行任何shell转义。尝试按原样传递JS代码,不使用转义的美元符号和双引号。
public String exportToPdf(final String bookmarkableUrl) {
    String uuid = UUID.randomUUID().toString();

    final String fullUrl = "http://" + hostName + ":" + port + bookmarkableUrl;

    // .html extension at the end is very important - wkhtmltopdf won't read
    // the file if not there
    String generatedPdfPath = tempDirPath + "/EMF/" + uuid;
    try {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(prepareCommandArguments(fullUrl, generatedPdfPath + PDF_FILE_EXTENSION));
        Process start = processBuilder.start();
        // One has to handle the error stream 
        handleStream(start.getErrorStream());
        handleStream(start.getInputStream());
        // Wait until process is executed.
        start.waitFor();
    } catch (IOException | InterruptedException e) {
        throw new RuntimeException("Error while generating PDF", e);
    }
    return generatedPdfPath;
}
private List<String> prepareCommandArguments(String inputUrl, String outputUrl) {
    List<String> arguments = new ArrayList<>(15);
    // absolute path to the wkhtmltopdf executable
    arguments.add(wkhtmltopdfLocation);
    // Wait until window.status is equal to this string before rendering page
    arguments.add("--window-status");
    arguments.add("export-ready");
    // Set the default text encoding, for input
    arguments.add("--encoding");
    arguments.add("UTF-8");
    // Set an additional HTTP header for system username
    arguments.add("--custom-header");
    arguments.add("username");
    arguments.add(exportUsername);
    // Set an additional HTTP header for system user password 
    arguments.add("--custom-header");
    arguments.add("password");
    arguments.add(exportPassword);
    // Run this additional javascript after the page is done loading
    // Used to remove irrelevant divisions and spanning of 
    // the html page, leaving only the print preview of the document
    arguments.add("--run-script");
    arguments.add(getScriptFromFile(jsFilePath));
    // Bookmarkable url of the document 
    arguments.add(inputUrl);    
    // Path to the generated pdf
    arguments.add(outputUrl);
    return arguments;
}
/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://xx.xx.xx.xx:xxxx/url/page.jsf?param1=1&param2=2, /root/sandbox/file.pdf