使用Apache Common Exec来写入PDF。

huangapple go评论68阅读模式
英文:

Using apache common exec to write to pdf

问题

HI all i am trying to use this apache common exec, using this i am trying to create and write to a file.
the command line argument to write to a file is follows
Example: PDFAnnotator.exe "C:\My Documents\Test.pdf";
I have tried the following

    public PrintResultHandler print(final File file, final long printJobTimeout, final boolean printInBackground)
                throws IOException {
    
            int exitValue;
            ExecuteWatchdog watchdog = null;
            PrintResultHandler resultHandler;
    
            // build up the command line to using a 'java.io.File'
            CommandLine commandLine = new CommandLine("C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe");
            //CommandLine cmdLine = new CommandLine("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
    
            Map map = new HashMap();
            map.put("file", new File("C:\\test\\invoice.pdf"));
            commandLine.addArgument("/p");
            commandLine.addArgument("/h");
            commandLine.addArgument("${file}");
    
            // create the executor and consider the exitValue '1' as success
            final Executor executor = new DefaultExecutor();
            executor.setExitValue(1);
    
            // create a watchdog if requested
            if (printJobTimeout > 0) {
                watchdog = new ExecuteWatchdog(printJobTimeout);
                executor.setWatchdog(watchdog);
            }
    
            // pass a "ExecuteResultHandler" when doing background printing
            if (printInBackground) {
                System.out.println("[print] Executing non-blocking print job  ...");
                resultHandler = new PrintResultHandler(watchdog);
                executor.execute(commandLine, (Map<String, String>) resultHandler);
            }
            else {
                System.out.println("[print] Executing blocking print job  ...");
                exitValue = executor.execute(commandLine);
                resultHandler = new PrintResultHandler(exitValue);
            }
    
            return resultHandler;
        }

it does not create any pdf file as an output can you please suggest.
英文:

HI all i am trying to use this apache common exec, using this i am trying to create and write to a file.
the command line argument to write to a file is follows
Example: PDFAnnotator.exe "C:\My Documents\Test.pdf"
I have tried the following

public PrintResultHandler print(final File file, final long printJobTimeout, final boolean printInBackground)
throws IOException {
int exitValue;
ExecuteWatchdog watchdog = null;
PrintResultHandler resultHandler;
// build up the command line to using a &#39;java.io.File&#39;
CommandLine commandLine = new CommandLine(&quot;C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe&quot;);
//CommandLine cmdLine = new CommandLine(&quot;C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe&quot;);
Map map = new HashMap();
map.put(&quot;file&quot;, new File(&quot;C:\\test\\invoice.pdf&quot;));
commandLine.addArgument(&quot;/p&quot;);
commandLine.addArgument(&quot;/h&quot;);
commandLine.addArgument(&quot;${file}&quot;);
// create the executor and consider the exitValue &#39;1&#39; as success
final Executor executor = new DefaultExecutor();
executor.setExitValue(1);
// create a watchdog if requested
if (printJobTimeout &gt; 0) {
watchdog = new ExecuteWatchdog(printJobTimeout);
executor.setWatchdog(watchdog);
}
// pass a &quot;ExecuteResultHandler&quot; when doing background printing
if (printInBackground) {
System.out.println(&quot;[print] Executing non-blocking print job  ...&quot;);
resultHandler = new PrintResultHandler(watchdog);
executor.execute(commandLine, (Map&lt;String, String&gt;) resultHandler);
}
else {
System.out.println(&quot;[print] Executing blocking print job  ...&quot;);
exitValue = executor.execute(commandLine);
resultHandler = new PrintResultHandler(exitValue);
}
return resultHandler;
}

it does not create any pdf file as an output can you please suggest.

答案1

得分: 1

这段代码似乎已经从<a href="http://commons.apache.org/proper/commons-exec/xref-test/org/apache/commons/exec/TutorialTest.html">Apache Commons Exec</a>教程代码进行了修改。代码中有一些修改似乎是您所做的,这些修改引起了问题。

首先,您删除了以下这行代码:

      commandLine.setSubstitutionMap(map);

没有这行代码,您创建了变量map,将一个值放入该映射中,然后没有进一步处理它。显然,拥有一个从未读取任何值的映射毫无意义。重新加入这行代码,它很重要。

另一个问题是以下这行代码:

            executor.execute(commandLine, (Map&lt;String, String&gt;) resultHandler);

这段代码与教程代码的不同之处在于,您添加了对Map&lt;String, String&gt;的强制转换。resultHandler是一个PrintResultHandler,但是该类并未实现Map&lt;String, String&gt;,因此这个转换将失败。

我不明白您为什么要进行这个转换。将其删除,留下以下代码:

            executor.execute(commandLine, resultHandler);

如果您的代码仍然无法正常工作,那么我无法确定原因。也许Adobe Reader可执行文件不在您认为的位置,文件可能不存在或没有读取权限。无论如何,适当的详细信息应写入标准输出或标准错误,以帮助您进一步诊断问题。

英文:

It seems this code has been modified from the <a href="http://commons.apache.org/proper/commons-exec/xref-test/org/apache/commons/exec/TutorialTest.html">Apache Commons Exec</a> tutorial code. There are a couple of modifications to the code it seems you have made which have caused problems.

Firstly, you have deleted the line

      commandLine.setSubstitutionMap(map);

Without this line, you are creating the variable map, putting a single value into this map and then doing nothing further with it. Clearly, having a map that you never read any values out of achieves nothing. Reinstate this line, it's important.

The other problem is the line

            executor.execute(commandLine, (Map&lt;String, String&gt;) resultHandler);

The difference between this code and the tutorial code is that you have added the cast to Map&lt;String, String&gt;. resultHandler is a PrintResultHandler, but this class does not implement Map&lt;String, String&gt; so this cast will fail.

I don't see why you have the cast at all. Get rid of it to leave you with:

            executor.execute(commandLine, resultHandler);

If your code continues not to work, then I can't say what the reasons would be. Maybe the Adode Reader executable isn't where you think it is, maybe the file doesn't exist or doesn't have read permissions. In any case, suitable details should be written to standard output or standard error to help you further diagnose the problem.

huangapple
  • 本文由 发表于 2020年9月11日 14:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定