empty pdf saving trouble

edited October 2014 in Questions about Code

I'm trying to save a pdf when s is pressed... but so far I get blank pages. I think my problem is that I need a redraw when keyPressed... help for a newbie?

// Press n to generate new page, press p to print pdf



import processing.pdf.*;
import java.util.Calendar;
String timestamp() {
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", Calendar.getInstance());
}

boolean savePDF = false;

void penta(int x,int y){
  strokeWeight(1);
  line(x,y,800,y);
  line(x,y+5,800,y+5);
  line(x,y+10,800,y+10);
  line(x,y+15,800,y+15);
  line(x,y+20,800,y+20);
}

void setup(){
  size(842,595);
  smooth();
  background(255);
  strokeWeight(0.5);
  penta(40,300);

  noLoop();}

void draw(){

  if(key == 'p' || key=='P'){
    savePDF = true; 
    beginRecord(PDF, timestamp()+".pdf");}
    if (savePDF) {
    savePDF = false;
    println("saving to pdf – finishing");
    endRecord();
    println("saving to pdf – done");

  } 

  if(key == 'n' || key== 'N'){
    background(255);
    strokeWeight(1);
    penta(40,300);

    float Var= random(0,127);
    int v=int(Var);
    float ef= random(0,127);
    int eff=int(ef);
    println(v);
    ;

   if ((v <= 127) && (v >=0)){
      float j= random(1,4);
    for (int i=0; i <= j; i++){
   float x = random(40, 800);
   float y = random(120, 380);
   float r = random(2, 200);
   fill(0);
   ellipse (x,y,r,r);}
     }
  }
}
    void keyPressed(){
  redraw();}
Tagged:

Answers

  • edited September 2019

    A working PDF example I've got here. Hope it helps:
    http://forum.processing.org/two/discussion/4275/how-to-compile-a-series-of-functions-into-one-variable#Item_13

    /**
     * Letters-X (v3.1)
     * by  DiegoOriani (2014/Apr)
     * mod Chrisir & GoToLoop
     *
     * Forum.Processing.org/two/discussion/4275/
     * how-to-compile-a-series-of-functions-into-one-variable#Item_13
     */
    
    import processing.pdf.PGraphicsPDF;
    
    interface DataX {
      int ROWS = 8, COLS = 10, GAP = 030, MARGIN = 020;
      int LEN = 030, WEIGHT = 5, CAP = SQUARE;
    
      int HSIZE = COLS*(LEN+GAP) + MARGIN*2 - GAP;
      int VSIZE = ROWS*(LEN+GAP) + MARGIN*2 - GAP;
    
      color BG = 0300;
    
      color[] inks = {
        //#FFCC00, #FF9900, #FF35EE
        #FF0000, #008000, #0000FF
      };
    
      String PDF_FILE = "lettersX-####.pdf";
    }
    
    final LetterX[] xxx = new LetterX[DataX.ROWS * DataX.COLS];
    boolean savePDF;
    
    void settings() {
      size(DataX.HSIZE, DataX.VSIZE);
    }
    
    void setup() {
      if (width != DataX.HSIZE | height != DataX.VSIZE)  settings();
    
      noLoop();
      frameRate(10);
    
      initCanvas();
      instantiateLettersX();
    
      println("Width: " + width + "\tHeight: " + height);
      println("# of Xs: " + xxx.length + "\n");
    }
    
    void initCanvas() {
      smooth();
      background(DataX.BG);
      strokeWeight(DataX.WEIGHT);
      strokeCap(DataX.CAP);
    }
    
    void draw() {
      if (savePDF) {
        beginRecord(PDF, dataPath(DataX.PDF_FILE));
        initCanvas();
      }
    
      for (LetterX currentX : xxx)  currentX.display();
    
      if (savePDF) {
        endRecord();
        savePDF = false;
        println("Frame #" + frameCount + " saved!");
      }
    }
    
    void keyPressed() {
      reshuffleInks();
      redraw = true;
      if (keyCode == 'S')  savePDF = true;
    }
    
    void mousePressed() {
      keyCode = 0;
      keyPressed();
    }
    
    void reshuffleInks() {
      for (final LetterX currentX : xxx)
        currentX.c = DataX.inks[(color) random(DataX.inks.length)];
    }
    
    void instantiateLettersX() {
      for (int r = 0; r < DataX.ROWS; ++r)  for (int c = 0; c < DataX.COLS; ++c) {
        final int x = c*(DataX.LEN + DataX.GAP) + DataX.MARGIN;
        final int y = r*(DataX.LEN + DataX.GAP) + DataX.MARGIN;
        final color ink = DataX.inks[(color) random(DataX.inks.length)];
    
        xxx[r*DataX.COLS + c] = new LetterX(x, y, ink);
      }
    }
    
    class LetterX implements DataX {
      final short x, y;
      color c;
    
      LetterX(int xx, int yy, color cc) {
        x = (short) xx;
        y = (short) yy;
        c = cc;
      }
    
      void display() {
        stroke(c);
        line(x, y, x+LEN, y+LEN);
        line(x+LEN, y, x, y+LEN);
      }
    }
    
  • edited October 2014

    GoToLoop thank you, I'm not that good to see at first sight what I'm doing wrong and how your kind answer could help me.. after all it's just my second week with processing

    if somebody could make me see what is the problem with my code it would be more helpful. ^_^

  • edited October 2014 Answer ✓

    I guess main diff. between my example & yours is the initCanvas() function.
    You know, beginRecord() doesn't pick past procedures like fill(), stroke(), background(), etc.

    P.S.: Hmm... I guess there are more to it! As aforementioned above, beginRecord() isn't retroactive!
    You gotta start drawing after beginRecord()!

  • edited October 2014

    You gotta start drawing after beginRecord()!

    DOH!!

  • GoToLoop I've made it... I just have some problems with the fonts.. I still make some confusion.. I thought the best place to load font was void setup(){} am I wrong?

  • edited October 2014 Answer ✓

    That's right! Load all resources inside setup() if possible! As you should know, textFont() sets which font to use:
    https://processing.org/reference/textFont_.html

    However I fear perhaps beginRecord() doesn't pick up the textFont() previously set for the canvas, just like other things like fill() & stroke()!

    That's why I've made a separate initCanvas() in order to initialize the PGraphicsPDF after a beginRecord()!

    Just make your own custom function for that task. Notice you won't re-loadFont() nor re-createFont() in there.
    Merely re-use the PFont object you already got! *-:)

  • thank you sir for your patience...

  • I've made it!!!

    the way was to use createFont() inside your idea of the initCanvas now exports perfectly YES!

  • edited October 2014

    The way was to use createFont() inside your idea of the initCanvas()...

    More specifically the idea was to use textFont() inside initCanvas()!
    While createFont() or loadFont() inside setup() instead!
    That is so in order to avoid needless instantiation of PFont objects!
    Thus, create/load once, reuse many! :P

    Nonetheless, I'm glad it's already worked for ya! O:-)

Sign In or Register to comment.