Printing P3D sketch to PDF

Hi all,
first time poster with a newbie question: I’d like to print to PDF the results of some sketches I’m doing with random walkers drawing lots spheres. I took Example 4 from the “Print” tutorial and modified it slighty so it begins recording during setup() and stops recording once I hit the E key during draw(), but continues running. However, the resulting PDF is empty. Is that simply because P3D can’t be written to PDF or am I missing something else? Below I’m including a much simplified example to illustrate the problem.

Thank you for any hints!

Tobias

import processing.pdf.*;

int nr = 1;
float r = 127;
float g = 127;
float w = 400;
float h = 400;
float d = 0;

void setup() {
  size(800, 800, P3D);
  pixelDensity(displayDensity());
  beginRecord(PDF, "lines"+nr+".pdf"); 
  background(255);
}

void draw() {
  lights();
  strokeWeight(0);
  stroke(0, 100);
  fill(r, g, 28);
  translate(w, h, d);
  sphere(20);

  // Change color, position and translation slightly every frame
  r += random(-10, 10);
  g += random(-10, 10);
  w += random(-5, 5);
  h += random(-5, 5);
  d--; // <-- this should be "d minus minus" - the forum apparently makes one dash out of them
}

void keyPressed() {
  if (key == 'E' || key == 'e') {
    endRecord(); 
    nr++;
    beginRecord(PDF, "lines"+nr+".pdf");
  }
}

please format your code:

  • in processing IDE use [ctrl][t]
  • in forum editor press the
</> code tag

and get the

```
paste your code in here
```


for the PDF, did you read
https://processing.org/reference/libraries/pdf/index.html this?


you must say what part you want print to pdf,
if you want at every keypress all drawing from beginning to that moment
in the pdf you must use the PGraphic example,
here one that only prints all what is drawn between 2 keypress.

import processing.pdf.*;
boolean savePDF = false;
String filename;

int nr = 1;
float r = 127;
float g = 127;
float w = 400;
float h = 400;
float d = 0;

void setup() {
  size(500, 500, P3D);
  background(255);
}

void draw() {
  lights();
  strokeWeight(0);
  stroke(0, 100);
  fill(r, g, 28);
  translate(w, h, d);
  sphere(20);

  // Change color, position and translation slightly every frame
  r += random(-10, 10);
  g += random(-10, 10);
  w += random(-5, 5);
  h += random(-5, 5);
  d--;
}

void keyPressed() {
  if (key == 'E' || key == 'e') {
    toPDF();
    filename = "data/lines"+nr+".pdf";
    beginRaw(PDF, filename);
    savePDF = true;
    println("recording started ");
    nr++;
  }
}

void toPDF() {    
  if ( savePDF ) {
    endRaw();
    println("saved to "+filename);
    savePDF = false;
  }
}

2 Likes

@kll, thank you very much, both for the forum/formatting tip and the hints regarding my question. I’ve followed this up and managed to do most of what the tutorials say. But I’m starting to realize that what I’m doing - using P3D to draw on a screen probably can’t be vectorized - you’d need a vector file for every single frame overlaid on top of the others I guess. My solution has just been to increase the size() and render higher resolution jpegs for now. Again, thank you for your help.