# Help exporting PDF

**URL:** <https://discourse.processing.org/t/help-exporting-pdf/26416>\
**Category:** Coding Questions\
**Created:** [December 21, 2020, 2:00pm UTC](https://discourse.processing.org/t/help-exporting-pdf/26416 "2020-12-21T14:00:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucaii](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@lucaii](https://discourse.processing.org/u/lucaii)\
**Post date:** [December 21, 2020, 2:00pm UTC](https://discourse.processing.org/t/help-exporting-pdf/26416/1 "2020-12-21T14:00:56Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![cansik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cansik/32/8815_2.png) [@cansik](https://discourse.processing.org/u/cansik)\
**Post date:** [December 22, 2020, 12:52pm UTC](https://discourse.processing.org/t/help-exporting-pdf/26416/2 "2020-12-22T12:52:19Z")

</div>

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):

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

```

---

<div class="post-metadata">

**Author:** ![lucaii](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@lucaii](https://discourse.processing.org/u/lucaii)\
**Post date:** [December 22, 2020, 1:16pm UTC](https://discourse.processing.org/t/help-exporting-pdf/26416/3 "2020-12-22T13:16:43Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![cansik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cansik/32/8815_2.png) [@cansik](https://discourse.processing.org/u/cansik)\
**Post date:** [January 11, 2021, 10:36am UTC](https://discourse.processing.org/t/help-exporting-pdf/26416/4 "2021-01-11T10:36:17Z")

</div>

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

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

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

```
