Is it possible to write to a PDF while using the P2D renderer?

edited September 2016 in Library Questions

I have a programming that is using the P2D renderer and I want to save frames to a PDF at a button press. I simply want to implement this code from the PDF examples on the processing website.

import processing.pdf.*;

boolean record;

void setup() {
  size(400, 400);
}

void draw() {
  if (record) {
    // Note that #### will be replaced with the frame number. Fancy!
    beginRecord(PDF, "frame-####.pdf"); 
  }

  // Draw something good here
  background(255);
  line(mouseX, mouseY, width/2, height/2);

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

// Use a keypress so thousands of files aren't created
void mousePressed() {
  record = true;
}

Unfortunately, this implementation did not work for my code. It simply to produced blank PDF files. I tried simply switching the renderer to default, as it is in the example above and it was able to write a file with the image. However, my code uses PShapes and due to a bug in processing, it only looks correct using the P2D renderer, not the default. (this link shows why)

Is there a way around this? I really would like to save frames of my program to manipulate in Illustrator.

Answers

  • Odd. What OS / version are you on?

    I've just run both your example sketch above and the tutorial it is based on, and both correctly created PDFs with lines drawn in them in the sketch folder. So it seems like this problem might be specific to your machine setup. I'm running Processing 3.2.1 on OS X 10.10.5.

  • I'm working on the Processing 3.2.1 as well, on Windows 10, but I don't think that's the issue. I think its an issue specific to PShapes. Try this code with default and P2D renderer:

    import processing.pdf.*;
    
    boolean record;
    
    PShape s;
    
    void setup() {
      size(400, 400, P2D);
    
      s = createShape();
      s.beginShape(QUAD);
      s.fill(0);
      s.vertex(100,100);
      s.vertex(300,100);
      s.vertex(300,300);
      s.vertex(100,300);
      s.endShape();
    }
    
    void draw() {
      if (record) {
        // Note that #### will be replaced with the frame number. Fancy!
        beginRecord(PDF, "frame-####.pdf"); 
      }
    
      // Draw something good here
      background(255);
      line(0, 0, width/2, height/2);
      shape(s, 0, 0);
    
      if (record) {
        endRecord();
        record = false;
      }
    }
    
    // Use a keypress so thousands of files aren't created
    void mousePressed() {
      record = true;
    }
    
  • I found a simple workaround. I can simply use beginRaw() and endRaw() instead of beginRecord() and endRecord(). This gives me usable PDFs. This may not work in other scenarios but it works for what I'm doing.

    (By the way, how do I insert images from my computer into these posts?)

  • (The blue cloud picture in the bar above the comment box, on the far left, allows you to upload images)

  • Interesting. I can verify that, with @aNew 's most recent sketch:

    1. beginRecord + default render: correct, PDF with line + PShape
    2. beginRecord + P2D render: incorrect, PDF with line and no PShape
    3. beginRaw + default render: runtime error: "beginRaw() is not available with this renderer."
    4. beginRaw + P2D render: correct-ish, PDF with line and PShape (and extra vertex bug)

    correct output:

    frame-0200

    incorrect output:

    frame-0139.pdf

    correct-ish output with PShape extra vertex bug (from center of shape to lower right corner).

    frame-0068.pdf

Sign In or Register to comment.