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

**URL:** <https://discourse.processing.org/t/pdf-saving-how-to-open-a-window-to-choose-path-and-specify-a-name/42289>\
**Category:** Libraries\
**Created:** [June 25, 2023, 9:17am UTC](https://discourse.processing.org/t/pdf-saving-how-to-open-a-window-to-choose-path-and-specify-a-name/42289 "2023-06-25T09:17:08Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![jeykech](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jeykech](https://discourse.processing.org/u/jeykech)\
**Post date:** [June 25, 2023, 10:53pm UTC](https://discourse.processing.org/t/pdf-saving-how-to-open-a-window-to-choose-path-and-specify-a-name/42289/22 "2023-06-25T22:53:17Z")

</div>

@svan your code is working fine 👍  
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 !

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 26, 2023, 12:09am UTC](https://discourse.processing.org/t/pdf-saving-how-to-open-a-window-to-choose-path-and-specify-a-name/42289/23 "2023-06-26T00:09:29Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![jeykech](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jeykech](https://discourse.processing.org/u/jeykech)\
**Post date:** [June 26, 2023, 8:36am UTC](https://discourse.processing.org/t/pdf-saving-how-to-open-a-window-to-choose-path-and-specify-a-name/42289/24 "2023-06-26T08:36:59Z")

</div>

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.

> [@svan](#):
>
> ```auto
> 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+".pdf");
> }
> 
> 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");
> }
> 
> ```

[Previous page](https://discourse.processing.org/t/pdf-saving-how-to-open-a-window-to-choose-path-and-specify-a-name/42289.md?page=1)
