How do I get my PDF to render properly?

I’ve just started getting my feet wet with Processing, and I’m having trouble figuring out why I’m having problems with this sketch rendering out PDFs and SVGs that are missing a lot of the picture. I’m using the beginRecord(); endRecord(); method to save them, as well as save(); to make a PNG (which works fine). It looks like the PDF and SVG have a bunch of lines missing, wherever a line crosses another. If anyone has any pointers on why this might be happening I’d be really appreciative.

On the left is the pdf, on the right is the png:


import processing.pdf.*;
import processing.svg.*;

String title = "curves";

int counter;
int seed;

void setup() {
  size(720, 720);
  noFill();
  smooth();
  seed = millis();
  seededRender();
}
  
void draw() {
}


void seededRender() {
  randomSeed(seed);
  noiseSeed(seed);
  render();
}


void render() {
  
  int points = 2;
  int lines = 200;
  float zoff = 0.03;

  float z = 0.0;
  background(255);
  stroke(0);
  for (int i = 0; i <= width; i += width / lines) {
    beginShape();
    curveVertex(i, 0);
    curveVertex(i, 0);
    for (float p = 1; p <= points; p++) {
      curveVertex(width * noise(z + p), height * (p / (points + 1)));
    }
    curveVertex(i, height);
    curveVertex(i, height);
    endShape();
    z += zoff;
  }
}

//

void keyReleased() {
  if (key == TAB) {
    newSeed();
  }
  if (key == ENTER) {
    saveImage();
  }
}

void newSeed() {
  seed = millis();
  println("rendered with new seed " + seed);
  seededRender();
}

void saveImage() {
  String filename = "data/" + title + "_" + nf(counter, 3) + "_" + seed;
  beginRecord(PDF, filename + ".pdf");
  seededRender();
  endRecord();
  beginRecord(SVG, filename + ".svg");
  seededRender();
  endRecord();
  save(filename + ".png");
  println("saved " + filename);
  counter++;
}

I believe its overriding the stroke data with a fill, add a noFill(); to the render function and it works fine mate
Edit: Not sure why it works with png and on the screen itself though

1 Like

Oh wow, that’s great thank you so much :grin:

I didn’t even consider if I’d need to add it into the function, as noFill() is there in setup(), but I’m very new to the idea of not doing things in draw().

Cheers for taking the time to help me out!

Yeah im not to sure why it needed it in the function either after seeing it in draw, may be something to do with the begin record and end record, i just tried a bunch of things until it worked lol, glad to help

Hello @jakebeamish,

I have come across such issues in the past…

I generally put all code including attributes as as stroke() and fill() inside the beginning and end of any renderer.

References:
https://processing.org/tutorials/rendering
https://processing.org/reference/libraries/pdf/index.html

:)

1 Like