Group Abstract Group Abstract

Message Boards Message Boards

2
|
10.2K Views
|
4 Replies
|
5 Total Likes
View groups...
Share
Share this post:

How can I programmatically print a cell to file as PDF?

Posted 4 years ago
POSTED BY: Gregory Lypny
4 Replies
Posted 4 years ago
POSTED BY: Oliver Seipel
Posted 4 years ago

Here's a code blurb I used to use that may give you ideas.

If[Head[xxx`g] == Graphics,
  printcell[file, PrintingOptions -> {printopts}, 
   PageHeaderLines -> {False, False}]];

(i wanted to print a graphic cell print because it "printed better". the above says if the previous output of the last cell was Graphics, then use printcell to print it). I have omitted "printopts" which are just print settings specific for that task.

printcell[file_String, nbopts___] := 
  Module[{nb, nbsel, newnb, cell, v, xxx},
   nb =.; newnb =.; nbsel =.;
   nb = EvaluationNotebook[];
   SelectionMove[nb, Previous, EvaluationCell, AutoScroll -> False];
   SelectionMove[nb, Previous, Cell, AutoScroll -> False];
   SelectionMove[nb, All, TextParagraph, AutoScroll -> False];
   cell = NotebookRead[nb];
   newnb =.;
   newnb = NotebookCreate[Visible -> False];
   SelectionMove[newnb, All, Notebook, AutoScroll -> False];
   (* 
   bug if opts not defined short circuits most tests
   *)
   If[Length[{nbopts}] =!= 0,
    (*SetOptions[EvaluationNotebook[],nbopts];*)
    SetOptions[newnb, nbopts];
    ];
   NotebookWrite[newnb, cell];
   NotebookPrint[newnb, file];
   SelectionMove[newnb, All, Notebook, AutoScroll -> False];
   nbsel = NotebookSelection[newnb];
   v = Options[newnb];
   NotebookClose[newnb];
   nb =.; newnb =.; nbsel =.;
   v
   ];
POSTED BY: Updating Name
Posted 4 years ago

Thanks Oliver,

Works like a charm. I added NotebookWrite in a loop to add pages on the fly.

Greg

POSTED BY: Gregory Lypny
Posted 4 years ago
POSTED BY: Yasmin Hussain
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard