How to export a PDF from a part of the screen only ?

I'm working on automatic books generated with processing, and for one of these books I need to export the left of my screen to one PDF page, and the right to the next page and so on. This is a static and simplified version of my sketch:

import processing.pdf.*;
PGraphicsPDF pdf;

void setup() {
  size(1218,870);
  pdf = (PGraphicsPDF)beginRecord(PDF, "test.pdf");
  PImage img = loadImage("test.jpg");
  image(img, 0,0);
  pdf.nextPage();
  get(595,0,623,870).save("left.jpg");
  get(0,0,623,870).save("right.jpg");
  endRecord();
}

Obviously, this doesn't work at all. I get how I can separate the screen in two — with get() —, but is there any way to make that work with the pdf nextPage() function ? And is there any way at all to export a pdf that's not the size of your sketch? Thanks !

Answers

  • This kind of works, but not the way you want it to. You can only record one half of the screen.

    import processing.pdf.*;
    
    void setup(){
      size(400,400, P2D);
      beginRaw(PDF,"raw.pdf",200,height);
    }
    void draw() {
      line(pmouseX, pmouseY,  mouseX,mouseY);
    }
    void keyPressed(){
      if (key == ' '){
        endRaw();
        exit();
      }
    }
    
     PGraphics beginRaw(String renderer, String filename, int x, int y) {
        filename = insertFrame(filename);
        PGraphics rec = createGraphics(x, y, renderer, filename);
        g.beginRaw(rec);
        return rec;
      }
    
  • Thank you Eeyorelife I didn't think of that. But I don't get how you can generate multiple pages with the beginRaw function.

  • I don't think you can. I went to the source code of processing and found the raw function zero edited it a bit so you could change the few parameters that createGraphics took.

    If you go to processing ->libraries-> PDF and then click the link to the source code of the library you might be able to find a function you can recreate.

  • I really don't know how to do that. What I did was record all the pages to different jpg files in a directory. Is there a way I can access these file and organize them in the right order in a pdf file, at the end of my sketch ?

  • I finally got it, with a resize at the end of my sketch, to reorganize all the pages in the right order :

    void keyPressed() { 
      if (key == 'q') {
    
        background(255);
        surface.setResizable(true);
        surface.setSize(624, 870);
        background(255);
        pdf = (PGraphicsPDF)beginRecord(PDF, htag+dateA+".pdf");
        for (int i = 1; i <= (pagecount); i++) {
          PImage recap = loadImage("data/"+htag+dateA+"/"+i+".png");
          image(recap, 0, 0, 624, 870);
          if (i != pagecount) {
            pdf.nextPage();
          }
        }
        endRecord();
        exit();
      }
    }
    

    Thank you for your response Eeyorelife !

Sign In or Register to comment.