arara:裁剪输出的 PDF 文件,然后使用 ImageMagick 将其转换为图像

arara:裁剪输出的 PDF 文件,然后使用 ImageMagick 将其转换为图像

我如何创建一个单身的使用以下规则arara裁剪输出 PDF 文件pdfcrop,然后以相同的方式转换输出裁剪文件CarLaTeX 的转换规则(如下图所示)?

!config
# Convert .pdf to any format file allowed by ImageMagick convert command (the default is png)
# author: CarLaTeX
# last edited by: CarLaTeX, Dicember 26th 2016
# requires arara 3.0+
#
# Sample usage: 
# - these both create a .png file
# % arara: convert
# % arara: convert: {format: png}
#
# - this creates a .gif file with red background
# % arara: convert: {format: gif, background: red}
#
# - this creates a .png file with a trimmed image 
#   (use the parameter "otheroptions" to add any option not already explicitly considered by the rule, 
#   that is any option different from -background, -alpha, -density and -quality}
# % arara: convert: {otheroptions: -trim +repage}
#
#
# This rule is really just a shortcut for commands like the following:
#
#  convert -density 300 myfile.pdf myfile.png
#
# which will output myfile.png
#
identifier: convert
name: convert
commands: 
- <arara> @{ isWindows( "cmd /c convert", "convert" ) } -background @{background} -alpha @{alpha} -density @{density} @{otheroptions} "@{ getBasename(file) }.pdf" -quality @{quality} "@{ getBasename(file) }.@{format}"
arguments:
- identifier: density
  flag: <arara> @{parameters.density}
  default: 150
- identifier: otheroptions
  flag: <arara> @{parameters.otheroptions}
- identifier: quality
  flag: <arara> @{parameters.quality}
  default: 100
- identifier: background
  flag: <arara> @{parameters.background}
  default: white
- identifier: alpha
  flag: <arara> @{parameters.alpha}
  default: remove
- identifier: format
  flag: <arara> @{parameters.format}
  default: png

答案1

我根据 Carla 的代码编写了以下规则(将其保存为pconv.yaml):

(哎哟,我不习惯 3.0 版本的语法!我迫切需要尽快发布 4.0 版本!)

!config
identifier: pconv
name: 'pdfcrop + convert'
commands:
- 'pdfcrop @{ini} @{margins} @{ getBasename(file) }.pdf @{ getBasename(file) }-tmp.pdf'
- '@{ isWindows( "cmd /c convert", "convert" ) } -background @{background} -alpha @{alpha} -density @{density} @{otheroptions} -strip @{ getBasename(file) }-tmp.pdf -quality @{quality} @{ getBasename(file) }.@{format}'
- '@{ isWindows("cmd /c del", "rm -f")} @{ getBasename(file) }-tmp.pdf'
arguments:
- identifier: ini
  flag: "@{ isTrue(parameters.ini, '--ini') }"
  default: "add here your default value"
- identifier: margins
  flag: "--margins @{parameters.margins}"
- identifier: density
  flag: "@{parameters.density}"
  default: 150
- identifier: otheroptions
  flag: "@{parameters.otheroptions}"
- identifier: quality
  flag: "@{parameters.quality}"
  default: 100
- identifier: background
  flag: "@{parameters.background}"
  default: white
- identifier: alpha
  flag: "@{parameters.alpha}"
  default: remove
- identifier: format
  flag: "@{parameters.format}"
  default: png

现在,从 TeX 文件(例如foo.tex):

% arara: pdftex
% arara: pconv
\nopagenumbers
Hello world!
\bye

更新:在您的示例中,请改用此行:

% arara: pconv: { ini: yes }

现在运行arara

$ arara foo.tex 
  __ _ _ __ __ _ _ __ __ _ 
 / _` | '__/ _` | '__/ _` |
| (_| | | | (_| | | | (_| |
 \__,_|_|  \__,_|_|  \__,_|

Running PDFTeX... SUCCESS
Running pdfcrop + convert... SUCCESS
Running pdfcrop + convert... SUCCESS
Running pdfcrop + convert... SUCCESS

我们将得到相应的foo.png图像:

富

就是这样。:)

笔记:由于某些奇怪的原因,我不得不将其包含-stripconvert程序中。我不确定原因,可能是我拥有的版本或生成的 PDF 文件的一些限制。您的里程可能会有所不同...

相关内容