The following command executes ghostscript on a pdf file. (the pdf_file
variable contains the path to that pdf)
bbox <- system(paste( "C:/gs/gs8.64/bin/gswin32c.exe -sDEVICE=bbox -dNOPAUSE -dBATCH -f", pdf_file, "2>&1" ), intern=TRUE)
After execution bbox
includes the following character string.
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
This string is then manipulated in order for the BoundingBox dimensions (36 2544 248 2825) to be isolated and used for cropping the pdf file. So far everything works ok.
However, when I schedule this script in Task Manager (using Rscript.exe or Rcmd.exe BATCH), or when the script is inside an R chunk and I press knit HTML
, bbox gets the following character string which lacks the BoundingBox information, and makes it unusable:
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
How can I get over this problem and have the script run automated?
(The script comes from the accepted answer to that question)
Schedule R scripts/processes with the Windows task scheduler. This allows R users working on Windows to automate R processes on specific timepoints from R itself.
In RStudio, select "New Script" from the "File" menu. You'll now see a "Script" panel appear. Try typing a command into this panel and then press the "Run" button shown below. You should see the results of running the script appear in the console panel.
To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).
The 2>&1
you add at the end of the command is sent to the ghostscript interpreter, not the shell. Ghostscript interprets it a file, hence the error. I used procmon to look at the process creation:
To make the shell interpret it, you must prefix the command with cmd /c
, like this
> 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"
The output of the device is going to stdout, the error is going to stderr. In the terminal these are obviously both sent to the terminal and displayed together,in the second case they clearly aren't and the stdout is going missing.
This isn't too surprising since you are getting en error message on (2>&1). This looks like it is redirecting stdout to a file, but there are 2 problems. Firstly you haven't supplied a filename for the output to be sent to, and secondly, you aren't running in the command shell, so the command processor doesn't do the redirection.
I know nothing about R, so I can't tell you how to do that, but you should start by removing the '2>&1' from the command line anyway. You might also like to consider using a version of Ghostscript less than 4 years old. The current version is 9.07 and has just been released.
try this.
Set the output file using an environmental variable
Then use the %envvar% notation, which based on the link above would be %TODAY% which would be replaced with the file name friday. The -f isn't needed, but shouldn't hurt. If you want to route the output, set a second env variable and route it >%outenv%.
This way you can make a simple system call (see link for using variable rather than fixed strings),
Sys.setenv(envvar= "pdf.file")
Sys.setenv(outenv= "out.file")
"C:/gs/gs8.64/bin/gswin32c.exe -sDEVICE=bbox -dNOPAUSE -dBATCH %envvar% >%outenv%"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With