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
Diaa
  • 9,599

1 Answers1

4

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...

Paulo Cereda
  • 44,220
  • 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 https://pastebin.com/VHnn4mey. – Diaa Jan 22 '18 at 16:21
  • @DiaaAbidou: no worries. :) It's odd, I've never seen pdfcrop fail before. What's the output when you run perl.exe c:\texlive\2016\texmf-dist\scripts\pdfcrop\pdfcrop.pl test.pdf test-tmp.pdf in the command line? Does it fail too? – Paulo Cereda Jan 22 '18 at 16:24
  • Yes, it fails. However, I have to add --ini to make it run successfully. – Diaa Jan 22 '18 at 16:28
  • @DiaaAbidou: interesting. I will update the code and add an ini flag too. – Paulo Cereda Jan 22 '18 at 16:29
  • Sorry for bothering you. But is it necessary for me to understand the meaning of different pdfcrop options? or the output quality would be the same in either case? – Diaa Jan 22 '18 at 16:31
  • 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
  • @DiaaAbidou: my pleasure, poke me at any time! – Paulo Cereda Jan 22 '18 at 16:38
  • Use \nopagenumbers instead of \def\folio{}. – Henri Menke Jan 23 '18 at 01:08
  • @PauloCereda How can I set a default value for the true-false condition of --ini? Additionally, in the help file of pdfcrop, for example, margins can be set as --margins '5 10 5 20', so how can I set it here using these values? – Diaa Jan 23 '18 at 12:59
  • 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
  • Hello! Is it possible to add a rule compatible with arara v5? – Diaa Nov 29 '20 at 22:25
  • @Diaa We wrote rules for convert and pdfcrop in the official repo: https://gitlab.com/islandoftex/arara/-/blob/development/rules/arara-rule-convert.yaml and https://gitlab.com/islandoftex/arara/-/blob/development/rules/arara-rule-pdfcrop.yaml They are targeted for the release of version 6, but are compatible with version 5 as well. Hope it helps! :) – Paulo Cereda Dec 01 '20 at 18:53