Help exporting PDF

Hi,

Around four or five years ago I came up with this code which I now slightly edited. I would like to export a PDF on keypressed but can’t seem to make it work… everytime the pdf exports it’s empty. Any ideas or help would be appreciated.

import fisica.*;

//create a new world
FWorld world;
FBox obstacles[];

void setup() {
size(1400, 820);

//initialize Fisica
Fisica.init(this); 
obstacles = new FBox[15];

//world constructor
world = new FWorld();

//create edges (walls) on the perimeter of our world
//world.setEdges();
//world.remove(world.left);
//world.remove(world.right);
//world.remove(world.top);
//world.setGravity(0,200);

  for (int i=0; i<obstacles.length; i++) {
    FBox b;
    b = new FBox(random(10,300), 10);
    b.setPosition(random(width, width*2), random(height));
    b.setStroke(random(255),random(255),random(255));
    b.setStatic(true);
    b.setFill(random(255),random(255),random(255));
    b.setRotation(random(360));
    b.setRotatable(true);

    obstacles[i] = b;
    
    world.add(b);
  }
}

void draw() {
//background(255);
  for (int i=0; i<obstacles.length; i++) {
    FBox o = obstacles[i];    
    o.setPosition(o.getX() - 2, o.getY());
  if (o.getX() < -o.getWidth() / 2) {
    o.setPosition(width+o.getWidth() / 2, random(height));
    }
  float rotSpeed = radians(1);
    o.setRotation(o.getRotation() + rotSpeed);
  }

//these 2 lines are used to advance the animation,
//just leave them here at the end of the draw()
world.draw();
world.step();
}

I don’t see any pdf export method in your sketch?

Here is a method I wrote to export the drawing of processing (could be on keypress):

import processing.pdf.*;

// is true as long as no pdf export happens
// can be used in draw method to exclude stuff from export
boolean isProcessing = true;

// mm and px convertions
static final float MM_TO_PIXEL_RATIO = 0.3527778f;

// export methods
void createPDF(String pdfName) {
  // save to pdf
  PGraphics pdf = (PGraphicsPDF)beginRecord(PDF, pdfName);
  pdf.setSize(round(px(width) * pixelDensity), round(px(height)) * pixelDensity);

  pdf.scale(1.0 / MM_TO_PIXEL_RATIO);
  isProcessing = false;
  draw();
  isProcessing = true;
  endRecord();

  println("pdf " + pdfName + " saved!");
}

Hi, thanks for your reply!
I didn’t include any PDF export code in the one shared as I thought it was completely wrong anyhow. I tried the one above but it’s giving me the following error

"the function “px(int)” does not exist

import fisica.*;
import processing.pdf.*;

// is true as long as no pdf export happens
// can be used in draw method to exclude stuff from export
boolean isProcessing = true;

// mm and px convertions
static final float MM_TO_PIXEL_RATIO = 0.3527778f;

//create a new world
FWorld world;
FBox obstacles[];


void setup() {
size(1400, 820);

//initialize Fisica
Fisica.init(this); 
obstacles = new FBox[15];

//world constructor
world = new FWorld();

//create edges (walls) on the perimeter of our world
//world.setEdges();
//world.remove(world.left);
//world.remove(world.right);
//world.remove(world.top);
//world.setGravity(0,200);

  for (int i=0; i<obstacles.length; i++) {
    FBox b;
    b = new FBox(random(10,400), 10);
    b.setPosition(random(width, width*2), random(height));
    b.setStroke(random(255),random(255),random(255));
    b.setStatic(true);
    b.setFill(random(255),random(255),random(255));
    b.setRotation(random(360));
    b.setRotatable(true);

    obstacles[i] = b;
    world.add(b);
  }
}

void draw() {
//background(255);
  for (int i=0; i<obstacles.length; i++) {
    FBox o = obstacles[i];    
    o.setPosition(o.getX() - 2, o.getY());
  if (o.getX() < -o.getWidth() / 2) {
    o.setPosition(width+o.getWidth() / 2, random(height));
    }
  float rotSpeed = radians(1);
    o.setRotation(o.getRotation() + rotSpeed);
  }

 
//these 2 lines are used to advance the animation, just leave them here at the end of the draw
world.draw();
world.step();
}


// export methods
void createPDF(String pdfName) {
  // save to pdf
  PGraphics pdf = (PGraphicsPDF)beginRecord(PDF, pdfName);
  pdf.setSize(round(px(width) * pixelDensity), round(px(height)) * pixelDensity);

  pdf.scale(1.0 / MM_TO_PIXEL_RATIO);
  isProcessing = false;
  draw();
  isProcessing = true;
  endRecord();

  println("pdf " + pdfName + " saved!");
}

Yeah sorry, forgot to add them. They are just two helper methods to convert from mm to pixels.

public float mm(float wantedMM) {
  return wantedMM / MM_TO_PIXEL_RATIO;
}

public float px(float wantedPX) {
  return wantedPX * MM_TO_PIXEL_RATIO;
}