# Printing P3D sketch to PDF

**URL:** <https://discourse.processing.org/t/printing-p3d-sketch-to-pdf/10913>\
**Category:** Libraries\
**Created:** [May 4, 2019, 4:35pm UTC](https://discourse.processing.org/t/printing-p3d-sketch-to-pdf/10913 "2019-05-04T16:35:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TobiasReber](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TobiasReber](https://discourse.processing.org/u/TobiasReber)\
**Post date:** [May 4, 2019, 4:35pm UTC](https://discourse.processing.org/t/printing-p3d-sketch-to-pdf/10913/1 "2019-05-04T16:35:17Z")

</div>

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](https://processing.org/tutorials/print/) 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

```auto
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");
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 5, 2019, 12:32am UTC](https://discourse.processing.org/t/printing-p3d-sketch-to-pdf/10913/2 "2019-05-05T00:32:36Z")

</div>

please format your code:

- in processing IDE use [ctrl][t]
- in forum editor press the

```auto
</> code tag

```

and get the

> ```  
> paste your code in here  
> ```

* * *

for the PDF, did you read  
[PDF Export / Libraries / Processing.org](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.

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![TobiasReber](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TobiasReber](https://discourse.processing.org/u/TobiasReber)\
**Post date:** [May 10, 2019, 4:58pm UTC](https://discourse.processing.org/t/printing-p3d-sketch-to-pdf/10913/3 "2019-05-10T16:58:11Z")

</div>

@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.
