2013-02-14 36 views
7

以下命令在pdf文件上執行ghostscript。 (該pdf_file變量包含的路徑PDF)R腳本自動化時的不同結果

bbox <- system(paste("C:/gs/gs8.64/bin/gswin32c.exe -sDEVICE=bbox -dNOPAUSE -dBATCH -f", pdf_file, "2>&1"), intern=TRUE) 

執行bbox之後包括以下字符串。

GPL Ghostscript 8.64 (2009-02-03) 
Copyright (C) 2009 Artifex Software, Inc. All rights reserved. 
This software comes with NO WARRANTY: see the file PUBLIC for details. 
Processing pages 1 through 1. 
Page 1 
%%BoundingBox: 36 2544 248 2825 
%%HiResBoundingBox: 36.395015 2544.659922 247.070032 2824.685914 
Error: /undefinedfilename in (2>&1) 
Operand stack: 

Execution stack: 
    %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 
Dictionary stack: 
    --dict:1147/1684(ro)(G)-- --dict:1/20(G)-- --dict:69/200(L)-- 
Current allocation mode is local 
Last OS error: No such file or directory 
GPL Ghostscript 8.64: Unrecoverable error, exit code 1 

此字符串那麼爲了操縱用於BoundingBox的尺寸(36 2544 248 2825),以進行分離,並且用於裁剪PDF文件。到目前爲止,一切正常。但是,當我在任務管理器(使用Rscript.exe或Rcmd.exe BATCH)中安排此腳本時,或者腳本位於R塊內時,我按knit HTML,bbox將獲得以下字符串,該字符串缺少BoundingBox信息,並使其無法使用:

GPL Ghostscript 8.64 (2009-02-03) 
Copyright (C) 2009 Artifex Software, Inc. All rights reserved. 
This software comes with NO WARRANTY: see the file PUBLIC for details. 
Processing pages 1 through 1. 
Page 1 
Error: /undefinedfilename in (2>&1) 
Operand stack: 

Execution stack: 
    %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 
Dictionary stack: 
    --dict:1147/1684(ro)(G)-- --dict:1/20(G)-- --dict:69/200(L)-- 
Current allocation mode is local 
Last OS error: No such file or directory 

如何解決此問題並讓腳本自動運行?

(該腳本來自接受答案that question

+0

邊框被寫入stderr, 和'2>&1'將stderr重定向到標準輸出。 但是,這種語法顯然不被Windows識別。 – 2013-02-14 09:04:26

+0

它被Windows命令處理器識別,但這不是從命令shell執行的,因此重定向不會發生。 – KenS 2013-02-14 10:47:17

+0

'pdf_file'是否包含PDF的完整路徑? – robertklep 2013-02-16 11:46:15

回答

6

2>&1你在命令的末尾添加發送到ghostscript的解釋,而不是貝殼。 Ghostscript interprets it a file,因此錯誤。我曾經將procmon看進程創建:

stderr redirection is treated as a file by ghostscript

爲了使外殼解釋它,你必須用cmd /c前綴的命令,這樣

> bbox <- system(paste("cmd /c C:/Progra~1/gs/gs9.07/bin/gswin64c.exe -sDEVICE=bbox -dNOPAUSE -dBATCH -q -f",pdf_file,"2>&1"), intern=TRUE) 
> print (bbox) 
[1] "%%BoundingBox: 28 37 584 691"         "%%HiResBoundingBox: 28.997999 37.511999 583.991982 690.839979" 
2

該裝置的輸出是要到stdout,誤差會到stderr。在終端中,這些顯然都被髮送到終端並一起顯示,在第二種情況下,它們顯然不是並且標準輸出正在丟失。

這並不令人感到意外,因爲您收到了錯誤消息(2> & 1)。這看起來像是將stdout重定向到一個文件,但有兩個問題。首先,您沒有爲要發送的輸出提供文件名,其次,您沒有在命令shell中運行,所以命令處理器不執行重定向。

我對R一無所知,所以我不能告訴你該怎麼做,但是你應該首先從命令行中刪除'2> & 1'。您也可以考慮使用不到4年的Ghostscript版本。目前的版本是9.07,剛剛發佈。

+0

我與gs 9.06有同樣的問題,所以我開始嘗試舊版本。 – 2013-02-14 09:04:14

+0

刪除'2>&1'沒有幫助 – 2013-02-14 11:50:06

+0

它沒有幫助,它只是擺脫了錯誤。您的問題似乎是您正在捕獲stderr,而輸出被定向到stdout。您需要捕獲stdout,或將stdout重定向到stderr(這是在命令shell中執行的操作),或將stdout重定向到文件並讀取它。 – KenS 2013-02-15 08:37:08

1

試試這個。

集使用 environmental variable

然後使用%ENVVAR%表示法,其中基於上面的鏈接的輸出文件將是這將與文件名星期五替換%TODAY%。 -f不是必需的,但不應該傷害。如果要路由輸出,請設置第二個env變量並將其路由>%outenv%。

這樣你可以做一個簡單的系統調用(見鏈接使用變量,而不是固定的字符串),

Sys.setenv(envvar= "pdf.file") 
Sys.setenv(outenv= "out.file") 
"C:/gs/gs8.64/bin/gswin32c.exe -sDEVICE=bbox -dNOPAUSE -dBATCH %envvar% >%outenv%"