Message Boards Message Boards

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

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

Posted 3 years ago

Hello everyone, I have a function for grading student work. The function outputs a grade report for the student wrapped in a title cell. Here's a mocked up snippet (probably too much detail) of the output. I can select the title cell manually, choose Print Selection… under the File menu, and save the report as a nice looking PDF file. How can I select and print the cell to file programmatically?

CellPrint[TextCell["Assignment 1 Grade Report", "Title"]];
CellPrint[TextCell["Assignment Title Here", "Chapter"]];
CellPrint[TextCell["Course info here", "Subsection"]];
CellPrint[TextCell["Student ID: 12345678", "Text"]];
CellPrint[
  TextCell[DateString[{"DayName", ", ", "MonthName", " ", "Day", ", ",
      "Year", "  ", "Hour12Short", ":", "Minute", " ", "AMPM"}], 
   "Text"]];
CellPrint[TextCell["", "Text"]];
CellPrint[TextCell["Your grade is 78%", "Text"]];

That would be a big help in generating individual reports, which I can then email to my students. Any advice would be much appreciated.

Greg

POSTED BY: Gregory Lypny
4 Replies
Posted 3 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 3 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 3 years ago

First create a notebook (programmatically):

nb = CreateDocument[{TextCell["Assignment 1 Grade Report", "Title"], 
   TextCell["Assignment Title Here", "Chapter"], 
   TextCell["Course info here", "Subsection"], 
   TextCell["Student ID: 12345678", "Text"], 
   TextCell[
    DateString[{"DayName", ", ", "MonthName", " ", "Day", ", ", 
      "Year", "  ", "Hour12Short", ":", "Minute", " ", "AMPM"}], 
    "Text"], TextCell["", "Text"], 
   TextCell["Your grade is 78%", "Text"]}]

and then save it as pdf (programmatically):

Export["Assignment.pdf", nb]

I hope this is what you wanted:)

POSTED BY: Oliver Seipel
Posted 3 years ago

this is so helpful, Thank you for the sharing. It does help a lot:)

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

Group Abstract Group Abstract