3

I am on Linux and would like to convert a bunch of Mathematica 8 Notebooks to PDF.

Is there some way to convert them on the command line? I'd like to write a makefile rule for the conversion, so that I can batch convert many of them.

Martin Ueding
  • 2,365
  • 8
  • 28
  • 42

2 Answers2

6

Basically, there is no way to convert Mathematica notebooks into PDFs without invoking the frontend. To print or convert it, you first need to open it and a naive attempt to open a notebook from the Mathematica command line yields the error FrontEndObject::notavail

In[1]:= NotebookOpen["file.nb"]

FrontEndObject::notavail: 
   A front end is not available; certain operations require a front end.

This means that you could either make a notebook to do the conversion or call the frontend from the command line. Here's a solution in the form of a Mathematica script - it can easily be turned into a Notebook or package file.

Save the following code as nb2pdf, make it executable and place it the directory with the files you want to convert or somewhere in your path.

#!/usr/local/bin/MathematicaScript -script

(* Convert Mathematica notebooks to PDFs                              *)
(*   usage: nb2pdf file1.nb file2.nb etc...                           *)
(* outputs: file1.pdf file2.pdf etc...  into the current directoy     *)
(* If called with no filenames, this script                           *)
(*    will convert all notebook files in the current directory        *)

dir = Directory[];
files = {};
expandNb = False; (* Expand all cell groups in the Notebook *)

If[Length[$ScriptCommandLine] > 1, 
  Do[If[FileExistsQ[file], 
    AppendTo[files, file], 
    Print["File " <> file <> " does not exist"]],
    {file, Rest[$ScriptCommandLine]}],
  files = FileNames["*.nb"]];

With[{UFE = UsingFrontEnd},
 Do[nb = UFE@NotebookOpen[FileNameJoin[{dir, file}]];
  If[expandNb, UFE@SelectionMove[nb, All, Notebook]; 
               UFE@FrontEndExecute[FrontEndToken["SelectionOpenAllGroups"]]];
  UFE@NotebookPrint[nb, FileNameJoin[{dir, FileBaseName[file]<>".pdf"}]];
  UFE@NotebookClose[nb], {file, files}]]
Simon
  • 675
  • 4
  • 12
  • That looks promising. I tried it out, but I got `error: 8: Exec format error`. Any idea what that could mean? – Martin Ueding Jan 25 '12 at 19:53
  • @queueoverflow: I haven't seen that error before. Do any Mathematica scripts work for you? Try this ["hello world" script](http://mathematica.stackexchange.com/q/648/34). Or is it that the nb2pdf script just fails on some files? – Simon Jan 25 '12 at 23:26
  • Exact same error with the two line hello world script … – Martin Ueding Jan 26 '12 at 08:37
  • @queueoverflow: ok, try running the "hello world" program using `math -script hello` if that doesn't work, delete the shebang (#!) line and try again. – Simon Jan 26 '12 at 09:09
  • 1
    `math -script ~/bin/nb2pdf filename` works for me now. Thanks for the script! – Martin Ueding Jan 26 '12 at 15:34
  • Can I use your script in a an open source project? – Martin Ueding Jan 28 '12 at 12:00
  • @queueoverflow: Sure. You don't even have to cite the origin of the code if you don't want to. – Simon Jan 28 '12 at 14:10
1

Something that works with Mathematica 13: https://knanagnostopoulos.blogspot.com/2023/01/convert-many-mathematica-notebooks-to.html

nb2pdf (){ for f in $@;do echo -n "Converting $f to pdf ... "; wolframscript -code nb=\"$f\"';FileConvert[nb, "PDF"];' ;done; } 

nb2pdf *.nb