Java ProcessBuilder 不适用于 wkhtmltopdf

标签 java wkhtmltopdf runtime.exec processbuilder

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

Command has terminated with status: 139
Output:

Error: Loading pages (1/6) ....

并创建一个空的 pdf 文件(大小为 0B)

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

这里有一些更多的信息:

操作系统:

[root@host sandbox]# uname -a
Linux xxxxxx.com 2.6.32-279.22.1.el6.x86_64 #1 SMP Wed Feb 6 03:10:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

[root@host bin]# ./wkhtmltopdf --version
Name:
  wkhtmltopdf 0.12.0 03c001de254b857f08eba80b62d4b6490ffed41d

我正在尝试使用流程构建器运行的命令:

 /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

pdf生成代码:

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;
}

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://xx.xx.xx.xx:xxxx/url/page.jsf?param1=1&param2=2, /root/sandbox/file.pdf

最佳答案

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

关于Java ProcessBuilder 不适用于 wkhtmltopdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24281883/

相关文章:

java - 朴素贝叶斯,35 个实例数据集上的 15 个特征 - 7 个类别

php - 如何将 wkhtmltopdf 与 POST 数据一起使用

java - ProcessBuilder调试

java - 日期的唯一值

java - Spring Service 默认范围

java - 在 Java 中获取命令行的响应

html - WKHTMLTOPDF 仅将第一页生成为 PDF

ruby-on-rails-3 - 无法让 wicked_pdf 在 heroku 上工作? (但它在本地工作)

java - 从 Java 运行 EXE

java - runtime.exec 参数中有空格