簡體   English   中英

Java ProcessBuilder不使用wkhtmltopdf

[英]Java ProcessBuilder not working with wkhtmltopdf

我正在嘗試從網頁生成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中的錯誤還是我做錯了什么?

以下是一些更多信息:

OS:

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

在Process.start()之前輸出System.out.println(processBuilder.command()):

/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代碼,不要使用美元符號轉義和雙引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM