2

How can I create a single rule for arara that crops the output PDF file using pdfcrop command then convert the output cropped file the same way done by CarLaTeX's convert rule (shown below)?

!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
3

I wrote the following rule based on Carla's code (save it as pconv.yaml):

(Ouch, I am not used to version 3.0 syntax! I desperately need to publish version 4.0 as soon as possible!)

!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

Now, from the TeX file (e.g, foo.tex):

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

Update: In your example, use this line instead:

% arara: pconv: { ini: yes }

Now, running arara:

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

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

We will have a corresponding foo.png image:

foo

And that's it. :)

Note: For some odd reason, I had to include -strip to the convert program. I am not sure the reason, it may be the version I have or some restriction of the generated PDF file. Your mileage may vary...

  • Thanks for help. But this MWE doesn't work. It fails in the conversion. – Diaa Jan 22 '18 at 16:06
  • @DiaaAbidou I need more info, then. :) Output of arara -v foo.tex and arara.log will help. – Paulo Cereda Jan 22 '18 at 16:15
  • My bad. Here it is pastebin.com/VHnn4mey. – Diaa Jan 22 '18 at 16:21
  • 1
    @DiaaAbidou: I updated the rule to include the --ini flag for pdfcrop. I really don't know too much of pdfcrop, as I am really surprised it failed with you. I didn't even know about the --ini flag! I think your question is beyond my humble comprehension of those tools, perhaps a follow-up question might enlight us all (regarding pdfcrop, of course). :) – Paulo Cereda Jan 22 '18 at 16:35
  • 1
    @DiaaAbidou: I updated the rule and added the default key to the ini parameter, please edit the value and add the default fallback. Regarding --margins, you will only be able to set up just one value (which will be applied to all margins) because of a bug/restriction of arara 3.0 (solved in the unreleased version 4.0); this is achieved by setting % arara: pconv: { margins: 10 }. Hope it helps! – Paulo Cereda Jan 23 '18 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.