2
$\begingroup$

I have a notebook with various output cells including some graphs. I like to print only the output cells in PDF format, but each output cell should be fit into a separate page. Sometimes, pictures are large to print on one page, in which case a one-page-fit is desirable.

I used the following Code taken from this forum's depository.

 cellCounter = 1;
 Map[
 Export[
 StringJoin["cell-", ToString[cellCounter++], ".pdf"],
 NotebookRead[#]] &, 
 Cells[]]; 

This code produces pdf files of all the cells in a notebook, but I cannot locate where they are saved. Any idea how to do this?

$\endgroup$
8
  • 1
    $\begingroup$ use Cells[CellStyle -> {"Output"}] instead of Cells[]? $\endgroup$
    – kglr
    Sep 8, 2020 at 9:42
  • $\begingroup$ @kglr: I run the revised code but still I cannot find the PDF files created. The code indicates the names of the PDF files but I do not know where the PDF files are store? $\endgroup$ Sep 8, 2020 at 9:48
  • 1
    $\begingroup$ does Directory[] help? $\endgroup$
    – kglr
    Sep 8, 2020 at 9:52
  • 1
    $\begingroup$ Maybe using ImageSize -> Full or ImageSize -> All in Export? $\endgroup$
    – kglr
    Sep 8, 2020 at 10:23
  • 1
    $\begingroup$ Prevent Export from overwriting files and/or Better random (unique) file name may be useful. $\endgroup$
    – kglr
    Sep 8, 2020 at 14:07

1 Answer 1

3
$\begingroup$

My answer below merely revises the Code given in the question with @kglr's comments to achieve the objective stated in the question. I thought this might be useful for others in this forum, who aim to automatically print out output cells or input cells in either PNG or PDF or any other format of interest. Below, I show the cases for PDF and PNG.

SetDirectory["(*directory address for saving the outputs created*)" ]; 
cellCounter = 1;
Map[Export[StringJoin["cell-", 
  ToString[cellCounter++], ".png"], 
  NotebookRead[#], ImageSize -> Scaled[.85]]&, 
  Cells[CellStyle -> {"Output"}]];

The above code creates png files of output cells in the notebook with Imagesize->Scaled[.80], which adjusts the size of the output files to fit one output into a single page. One may adjust the ImageSize->Scaled[...] to print out the output in the desired size.

If one wants to print out the outputs in PDF format, however, replace PNG with PDF in the above code.

The code below performs the same task for input cells to create PDF files.

Map[Export[StringJoin["cell-",
   ToString[cellCounter++], ".pdf"],
   NotebookRead[#],Imagesize->Scaled[0.8]]&,
   Cells[CellStyle\[Rule]{"Input"}]];

Watch out!!! The above Code should be placed at the very end of the notebook to pick up the already generated outputs and/or inputs. One caveat is that the outputs named as cell1, cell2, etc no matter which notebook you are using. Therefore, the outputs created from a notebook will be overwritten on the outputs created from another notebook. I will revise my answer as soon as I find a solution to this caveat.

EDIT To prevent overwriting output cells, we use recognizable names. Change cell- in the code above with cell-AA-. This can easily prevent overwriting outputs as the outputs will be saved with such names as cell-AA-1, cell-AA-2, etc.

$\endgroup$
5
  • 1
    $\begingroup$ Tugrul, why not add the name of the notebook along with the style of the cell (in vs out)? That would surely prevent overwriting. You might also try some variant of the day’s date that you are running the code. $\endgroup$ Sep 10, 2020 at 7:23
  • $\begingroup$ @CA Trevillian: You are absolutely right. I will just replace the cell- with the name of the notebook. This will solve one of the problems I have. another thing is as you said insert time of the day but I do not know how to write that code. Could you please give me a line of example for this so that I can edit my answer? Thank you. $\endgroup$ Sep 10, 2020 at 12:55
  • 1
    $\begingroup$ Tugrul, maybe something like this? StringReplace[DateString@Today, “ “ -> “-“] ? These commands should give you good inspiration, at least! $\endgroup$ Sep 10, 2020 at 13:16
  • $\begingroup$ @CA Trevillian: DateString[] works fine. Thanks. $\endgroup$ Sep 10, 2020 at 13:51
  • 1
    $\begingroup$ Tugrul, great to hear! The StringReplace may be necessary for some file systems that do not allow white-spacing of file names, in-case you may run into issues using this. $\endgroup$ Sep 10, 2020 at 17:01

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.