-
Notifications
You must be signed in to change notification settings - Fork 1
/
nb2pdf
executable file
·37 lines (33 loc) · 916 Bytes
/
nb2pdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env wolframscript
(*
* NAME
*
* nb2pdf - convert Mathematica notebooks to PDFs.
*
* SYNOPSIS
*
* nb2pdf <file>...
*
* EXAMPLE
*
* find -type f -name '*.nb' -exec nb2pdf {} +
*
* NOTES
*
* Adapted from https://superuser.com/a/382064
*)
fileList = {};
scriptName = FileBaseName[First[$ScriptCommandLine]];
If[Length[$ScriptCommandLine] == 1,
Print[scriptName <> ": requires at least one file name"]; Exit[1]]
Do[If[FindFile[file] === $Failed,
Print[scriptName <> ": file '" <> file <> "' does not exist"],
AppendTo[fileList, FindFile[file]]],
{file, Rest[$ScriptCommandLine]}]
With[{UFE = UsingFrontEnd},
Do[nb = UFE@NotebookOpen[file];
(* Expand all cell groups. *)
UFE@SelectionMove[nb, All, Notebook];
UFE@FrontEndExecute[FrontEndToken["SelectionOpenAllGroups"]];
UFE@NotebookPrint[nb, FileBaseName[file]<>".pdf"];
UFE@NotebookClose[nb], {file, fileList}]]