# PDF of P3D Processing sketch

**URL:** <https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996>\
**Category:** Coding Questions\
**Created:** [December 2, 2022, 8:52pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996 "2022-12-02T20:52:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 2, 2022, 8:52pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/1 "2022-12-02T20:52:10Z")

</div>

I’m finding it difficult to create a PDF from the PDF library when my sketch specifies P2D or P3D. What am I doing wrong?

```auto
import processing.pdf.*;

boolean record;

void setup(){
  size(1500,800,P3D);
  frameRate(1);
  background(#FF7605);
  
  stroke(2);
  strokeWeight(1);
  smooth(8);
  fill(#081EFF);
  float xstart = random(10);
  float ynoise = random(10);
  translate (width/2, height/2, 0);
  for (float y =-(height/8); y <= (height/8); y+=3){
    ynoise += 0.02;
  float xnoise = xstart;
  for (float x = -(width/8); x <= (width/8); x+=3){
    xnoise += 0.02;
    drawPoint(x,y,noise(xnoise,ynoise));
    }
  }
}

void draw(){
  if (record){
    beginRaw(PDF, "output.pdf");
  }

void drawPoint(float x, float y, float noiseFactor){
  pushMatrix();
  translate (x * noiseFactor * 6, y * noiseFactor * 6, y);
  float edgeSize = noiseFactor *26;
  ellipse(0,0, edgeSize, edgeSize);
  popMatrix();
  
  if (record){
    endRaw();
    record = false;
    }
    
    // hit 'p' to record a single frame
    void keyPressed(){
      if(key == 'p'){
        record = true;
      }
  }

}

```

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 2, 2022, 8:53pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/3 "2022-12-02T20:53:56Z")

</div>

## PDF Files from 3D Geometry (With Screen Display)

To create vectors from 3D data, use the beginRaw() and endRaw() commands. These commands will grab the shape data just before it is rendered to the screen. At this stage, your entire scene is nothing but a long list of lines and triangles. This means that a shape created with sphere() method will be made up of hundreds of triangles, rather than a single object.

When using beginRaw() and endRaw(), it’s possible to write to either a 2D or 3D renderer. For instance, beginRaw() with the PDF library will write the geometry as flattened triangles and lines.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 2, 2022, 10:20pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/4 "2022-12-02T22:20:29Z")

</div>

Hello @asymmetric ,

As per the examples in the library reference you need to do all the drawing between _beginRaw()_ and _endRaw()_.

In your code you are drawing in _setup()_ before _beginRaw()_.

You have a function inside the _draw()_ loop; this should be outside of _draw()_ loop and called within the _draw()_ loop.

You will need to also correct your opening and closing brackets in your code; there are a few errors for you to correct.

Here is a simple working example:

```auto
import processing.pdf.*;

boolean record;

int seed;

void setup() 
  {
  size(400, 400, P3D);
  noLoop();
  }

// ******************************************

void draw() 
  {
  if (record) 
    {
    beginRaw(PDF, "output.pdf");
    }

  randomSeed(seed);

  // Do all your drawing here This could be a function call!
  background(204);
  
  stroke(0);
  fill(0, 255, 0);
  
  for(int i=0; i<200; i++)
    {
    push();
    translate(random(width), random(height), -100);
    circle(0, 0, 30);
    pop();
    }
  
  if (record) 
    {
    endRaw();
    record = false;
    }
  }

// ******************************************

// Hit 'r' to record a single frame
void keyPressed()
  {
  if (key == 'r') 
    {
    record = true;
    redraw();
    }
    
  if (key == 's') 
    {
    seed = int(random(0, 2000));
    redraw();
    }    
  }

```

I generated a _randomSeed()_ so I could capture what was generated in the current sketch window.

References:

- [setup() / Reference / Processing.org](https://processing.org/reference/setup_.html)
- [draw() / Reference / Processing.org](https://processing.org/reference/draw_.html)
- [Functions / Examples / Processing.org](https://processing.org/examples/functions.html)
- [Libraries / Processing.org](https://processing.org/reference/libraries)

`:)`

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 2, 2022, 10:32pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/5 "2022-12-02T22:32:30Z")

</div>

I was just about to try your solution!

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 2, 2022, 10:45pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/6 "2022-12-02T22:45:13Z")

</div>

> [@asymmetric](#):
>
> ```auto
> // hit 'p' to record a single frame
> void keyPressed(){
> if(key == 'p'){
> record = true;
> }
> 
> ```

I’m having a hard time putting the above conditional statement in the correct place… does it belong in the drawing block of code as well?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 2, 2022, 10:46pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/7 "2022-12-02T22:46:43Z")

</div>

> [@asymmetric](#):
>
> I’m having a hard time putting the above conditional statement in the correct place… does it belong in the drawing block of code as well?

The answer is in the references:

> **[keyPressed() / Reference](https://processing.org/reference/keyPressed_.html)**
>
> The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable. For non-ASCII keys, use the keyCode varia…

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 2, 2022, 10:48pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/8 "2022-12-02T22:48:26Z")

</div>

Aha! My code compiled but I don’t see the PDF anywhere in my sketch file?

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 2, 2022, 10:50pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/9 "2022-12-02T22:50:47Z")

</div>

Even when I press ‘r’ ☹

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 2, 2022, 11:09pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/14 "2022-12-02T23:09:14Z")

</div>

@glv IT WORKS!! BUT… The background is all of sudden white not black? How do I change it back to black?

 ![Screen Shot 2022-12-02 at 3.08.33 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/b/0b18fc6800f6af7ff23599ab2f78d040e4543fc1.jpeg)

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 3, 2022, 1:05am UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/15 "2022-12-03T01:05:00Z")

</div>

@glv what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 3, 2022, 8:06pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/16 "2022-12-03T20:06:48Z")

</div>

Hi @glv, I am still having issues with the PDF exporting P3D processing files… the PDF files have a white background instead of black, but I can see an off-white rendering of the image wireframe… how do I change this back to black?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 3, 2022, 8:41pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/17 "2022-12-03T20:41:06Z")

</div>

> [@asymmetric](#):
>
> the PDF files have a white background instead of black

I am not an expert on this library.

Searching the forum I found this:

> [@PDF export won't save with background for my 3d sketch](https://discourse.processing.org/t/pdf-export-wont-save-with-background-for-my-3d-sketch/10460):
>
> I have read the Processing docs on PDF export for 3d sketches. For some reason, my sketch won’t save as a PDF with the specified background. It only saves on a white background. //Stuff for saving stage import processing.pdf.\*; // global variables int DIM = 500; // height, width int N = 20; // n squares down and up int R = int(DIM/N); // radius of squares // setup drawing void setup(){ size(500, 500, P3D); rectMode(CENTER); noFill(); noLoop(); stroke(100, 100, 0); } // draw void …

You could also go here and do a search you will get more results:  
[https://processing.org/](https://processing.org/)

If you provide a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) the community can run your code and may be able to help. An MCVE is mentioned in [Guidelines - Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147/1).

You can still get some nice effects without using P2D or P3D and get your background.

This is from the PDF:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/8/78d548a87f57b2877dc0530080b523c32ae81246.png)

Remove anything that has a z component from code:

`translate (x * noiseFactor * 6, y * noiseFactor * 6);`

You may lose some perspective but it seems you are modifying size of ellipse already.

`:)`

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 3, 2022, 11:36pm UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/18 "2022-12-03T23:36:17Z")

</div>

Is there another way to save higher resolution images of P3D Processing sketches?

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [December 4, 2022, 1:32am UTC](https://discourse.processing.org/t/pdf-of-p3d-processing-sketch/39996/19 "2022-12-04T01:32:41Z")

</div>

Hi @glv, so I figured out an alternative way to go about putting the background back after converting my P3D Processing sketch to a PDF file. That alternative way is to add a background layer in Photoshop because the image turns into a vector image with a transparent background when you export your P3D file to a PDF. This way I can get the high resolution vector lines!
