# Save multiple PDFs during the sketch run

**URL:** https://discourse.processing.org/t/save-multiple-pdfs-during-the-sketch-run/13015
**Category:** Coding Questions
**Created:** [July 27, 2019, 1:47pm UTC](https://discourse.processing.org/t/save-multiple-pdfs-during-the-sketch-run/13015 "2019-07-27T13:47:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Pvag](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pvag/32/5934_2.png) [@Pvag](https://discourse.processing.org/u/Pvag)
#### Post date: [July 27, 2019, 1:47pm UTC](https://discourse.processing.org/t/save-multiple-pdfs-during-the-sketch-run/13015/1 "2019-07-27T13:47:05Z")

</div>

Hello,

I would like to add this functionality to my sketch: save the drawing to a new pdf file, each time the key ‘s’ is pressed. In order to do this, I am using a PGraphics for the pdf, with a pdf renderer. When I dispose() it, I lose its content and I don’t know how to save it to the next pdf file - if I don’t do it, whenever I dispose() the next pdf file, it will only have the part of the drawing made after saving the first one, and so on.

An example of the behaviour I would like to get:

```auto
import processing.pdf.*;

PGraphics pdf1, pdf2;

void setup()
{
  pdf1 = createGraphics(300, 300, PDF, "output1.pdf");
  noLoop();
}

void draw()
{
  pdf1.beginDraw();
  pdf1.background(128, 0, 0);
  pdf1.line(50, 50, 250, 250);
  pdf2 = createGraphics(300, 300, PDF, "output2.pdf");
  pdf2 = (PGraphics)pdf1.copy(); // --> i get a RuntimeException: No get() for PGraphicsPDF
  pdf1.dispose();
  pdf1.endDraw();
  pdf2.line(50, 50, 100, 250); // start from where I left previously, instead of losing all previous drawings
  pdf2.dispose();
  pdf2.endDraw();
}

```

Basically, what I am trying to achieve is the same behaviour you can expect from a professional program, whenever you ask it to save a file as a pdf.

Thank you for considering this post. Have a nice day 🙂

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [July 28, 2019, 1:14am UTC](https://discourse.processing.org/t/save-multiple-pdfs-during-the-sketch-run/13015/2 "2019-07-28T01:14:03Z")

</div>

if you check

> **[PDF Export / Libraries](https://processing.org/reference/libraries/pdf/index.html)**
>
> Create PDF files. These vector graphics files can be scaled to any size and printed at high resolutions.

you see that

```auto
pdf = (PGraphicsPDF) createGraphics(width, height, PDF, "my.pdf");

```

must use

```auto
beginRecord(pdf);

//and

endRecord();

```

i not see in your code,  
also see example

> **Pausing While Recording (With Screen Display)**

pls stay close to one of the 8 examples there.

---

<div class="post-metadata">

### Author: ![Pvag](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pvag/32/5934_2.png) [@Pvag](https://discourse.processing.org/u/Pvag)
#### Post date: [July 28, 2019, 3:54pm UTC](https://discourse.processing.org/t/save-multiple-pdfs-during-the-sketch-run/13015/3 "2019-07-28T15:54:49Z")

</div>

Thank you for you reply, but it doesn’t answer my question at all. Furthermore, if you take a look at the end of the documentation link you mention, you will notice that the last way suggested for the usage of a PGraphics doesn’t involve beginRecord and endRecord at all (just scroll down to the section titled **Using createGraphics() to Create a PDF File**).

Is the problem clear, from my question ? I can’t stick to the examples provided by the documentation, because my case is different and I need a different behaviour in my app: I want to be able to “Save” to pdf multiple times and, eventually, “Save As”, always as a pdf.

Questions:

1. Is it possible to render a pdf without using dispose() ?
2. Is it possible to copy/clone a pdf (PGraphics or PGraphicsPDF or anything else needed) to another object, before disposing it ? This way, I will be able to keep saving, without the need to create a new pdf, losing the old states of the canvas. Is it possible to save the pdf without using dispose(), then ?

Thanks to anyone who will consider my question.

---

<div class="post-metadata">

### Author: ![Pvag](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pvag/32/5934_2.png) [@Pvag](https://discourse.processing.org/u/Pvag)
#### Post date: [July 30, 2019, 1:30pm UTC](https://discourse.processing.org/t/save-multiple-pdfs-during-the-sketch-run/13015/4 "2019-07-30T13:30:55Z")

</div>

I have finally found a way to accomplish what I wanted: I save every point the user draws in the canvas, together with related info, in a specific array of objects of a class of mine called Point. This class has info about point’s x and y, line weight, line color and every value that the drawing parameters had at the moment the i-th point was created.

Each time the user presses ‘s’, I render the pdf completely from scratch, looping through each point, retrieving and setting the properties of the i-th line, with something like:

```auto
PGraphics pdf;
pdf = createGraphics(300, 300, PDF, "output.pdf"); // I also have some code that generates a progressive number, joined at the end of the file name
pdf.beginDraw();
  for (int i = 0; i < points.size()-1; i++)
  {
    Point p1 = points.get(i);
    Point p2 = points.get(i+1);
    pdf.stroke(p1.stroke);
    pdf.lineWeight(p1.lineWeight);
    // ... set all the other properties of this specific line here
    pdf.line(p1.x, p1.y, p2.x, p2.y); // then I draw the line
  }
pdf.endDraw();
pdf.dispose(); // and finally I dispose the pdf.

```

Thank you for considering this post.
