Save multiple PDFs during the sketch run

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:

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

1 Like

if you check

you see that

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

must use

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.

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.

1 Like

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:

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.

3 Likes