PDF saving: how to open a window to choose path and specify a name

@svan your code is working fine :+1:
I am wondering now if there is a way to make it in one step instead of Two. maybe instead of pressing ‘p’, using filePathStr != null to set recordPDF= true ?
thank you for your time and your efforts !

The following also seems to work; keyPresses are abandoned. For some reason println statements in certain locations can derail it, so probably best to leave them out. RecordPDF is set to true right after the fileName and location are set:

import processing.pdf.*;
import controlP5.*;

ControlP5 cp5;

int cols, rows;
int spacing = 30;

boolean recordPDF = false;
String filePathStr = "";

void drawLines() {
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      int x = i*spacing;
      int y = j*spacing;
      line(x, y, x+30, y+30);
    }
  }
}

void setup() {
  size(800, 800);
  cp5 = new ControlP5(this);
  cp5.addButton("saveToPDF")
    .setPosition(30, 10)
    .setSize(80, 30)
    .setLabel("Save...")
    ;

  cols = width/spacing;
  rows = height/spacing;  
}

void draw() {
  if (recordPDF) {
    beginRecord(PDF, filePathStr);
  }

  background(255);
  strokeWeight(2.0);
  drawLines(); 

  if (recordPDF) {
    endRecord();
    recordPDF = false;
  }
}

void fileSelected(File selection) {
 if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    filePathStr = selection.getAbsolutePath();
    recordPDF = true;
  }
}

void saveToPDF(){
  selectOutput("Select a file to write to:", "fileSelected");
}

1 Like

this working fine, thanx.
just had to add the extension “.PDF” to beginRecord(PDF, filePathStr+“.pdf”) so the file is correctly named and can be opened directly.