1

I am using Org mode to build a website that contains my notes on various topics, which I read on my phone on the way to work. To introduce more automation into my workflow, I would like to have crontab setup so that at the stroke of midnight (say at 11:59:59 p.m). the org mode document is exported to the html (for the website) and pdf formats.

Sure, I can open up the emacs window in terminal or gui and then export it using C-c C-e but crontab cannot do this. How can I export an org file to html / pdf through the command line. Ideally the format of the command at the terminal would be

emacs <export-command-magic> notes.org -o notes.html

I know pandoc would be another solution (which allows you to convert org mode documents into HTML, but I would like to have a native Emacs solution that understands all the crufty customizations I have set up for my own org-mode, runs all the embedded source code snippets etc.)

  • I've provided an answer for the case where you are using the Org mode that is bundled with your emacs, but if you are installing Org mode from the git repo, or from MELPA, or from a tarball to a non-standard place, you will need some additional configuration. If that is the case, please edit your question and specify how you are installing Org mode and where the Org mode lisp directory is. – NickD Feb 20 at 17:41
3

If you can do it from within emacs, then you can do it from the command line with something like this for HTML:

emacs --batch --eval "(require 'org)" notes.org --funcall org-html-export-to-html

or for PDF:

emacs --batch --eval "(require 'org)" appts.org --funcall org-latex-export-to-pdf

This assumes that you use the Org mode that is bundled with emacs: otherwise, the evaluation of the (require 'org) form will fail. If you are installing Org mode from the git repo or from MELPA, you will need additional configuration.

See the emacs man page for an explanation of --batch (note that it makes emacs to NOT load your init file), --eval and --funcall.

Note also that you don't get a choice for the output file name: for the input file notes.org, it will be notes.html, just replacing the org suffix with the html suffix.

EDIT: a comment by the OP mentions that the emacs init file needed to be loaded to provide various settings and that the way to do that is to use a command like this:

emacs --batch --load ~/.emacs --eval "(require 'org)" notes.org --funcall org-html-export-to-html

My recommendation however would be to separate the settings that are needed for org export and put them in a separate file, e.g. ~/.emacs.d/my-org-export-settings.el. That way, all the export settings are in one file and only that file needs to be loaded for exporting from the command line; also ~/.emacs (or ~/.emacs.d/init.el) can use it as well by invoking (load-file "~/.emacs.d/my-org-export-settings.el"), so that there is a single well-defined place where the settings affecting export can be set.

The reason for the recommendation is that the main init file inevitably grows since it accumulates settings for all sorts of different emacs packages. Then starting up emacs takes longer (and longer and longer...): that's OK if you start emacs once and then live in it for weeks on end as many of us do, but if you want to invoke it from the command line to do just one thing and exit, it becomes more and more annoying to wait for everything to be initialized before doing that one thing and then exiting, throwing all that initialization away. If you have a small, separate file that includes just what is necessary for your Org export needs, you don't need to load the main init file: you just load the small file and you are done in a jiffy.

| improve this answer | |
  • Wow thanks so much! This works perfectly, exactly as I imagined it. I was a bit worried while posting this question, the answer might be no. Yay emacs! – smilingbuddha Feb 21 at 1:58
  • Just a minor addition. It seems I need to explicitly add a -l ~/.emacs option at the beginning of the long chain of options you mentioned. Without that flag all the settings or functions modiciations I've made for org mode don't get loaded. – smilingbuddha Feb 21 at 2:02
  • That's correct: as I said --batch explicitly does NOT load your init file, so if you need it, you need to load it manually. – NickD Feb 21 at 3:58

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.