Export portion of sketch into PDF

edited October 2014 in How To...

Hi all,

So let's say I have a sketch that's 1000px by 250px. I know how I can export the whole sketch (1000x250) into a PDF, but what if I want to export content that starts getting drawn 350px in from the left and 25px down from the top? Is it possible to target a region of a sketch and export it as a PDF?

So far I've tried using createGraphics, which allows you to specify a width and height, but those values originate from the origin, which isn't quite what I'm looking for.

Any help would be appreciated!

Answers

  • Perhaps get() can help ya crop a part of the canvas as a PImage.
    Then you can set() or image() that in a PDF PGraphics: :-??

  • Ok, I'll give this a go. Sounds like it should work. Thanks!

  • Answer ✓

    Probably cannot work, PDF isn't good with bitmap captures, and it looses the interest of PDF: being a vectorial format.

    That said, cropping drawings to a region while exporting to PDF seems to be a hard task.

    Or, perhaps, draw on a PGraphics of the size of the area to export: that would be the part to be exported, and you draw the resulting PGraphics on the sketch area.

  • edited November 2014

    Hi PhiLho,

    Thanks for the response! Your suggestion does the trick. Below is a snippet of test code. The blue and red bars represent menu areas. The black represents the live area of the sketch. My goal was to export just the black region, which using PGraphics allowed me to accomplish. Thanks again!

    import processing.pdf.*;
    
    PGraphics pdf;
    boolean save = false;
    
    void setup() {
      size(1323, 828);
      noStroke();
      pdf = createGraphics(1024, 768, PDF, "test.pdf");
    }
    
    void draw() {
      fill(0, 0, 255);
      rect(0, 0, 1024, 60);
      fill(255, 0, 0);
      rect(1024, 0, 299, 828);
      fill(0);
      rect(0, 60, 1024, 768);
    
      if (save) {
        pdf.beginDraw();
        pdf.fill(0);
        pdf.rect(0, 0, 1024, 768);
        pdf.dispose();
        pdf.endDraw();
        save = false;
      }
    }
    
    void keyPressed() {
      if (key == 's') {
        save = true;
      }
    }
    
Sign In or Register to comment.