Loading...
Logo
Processing Forum
Sometimes i have nice unwanted progress (serendipity).
The fastest for me is making a screenshot but sometimes i like vectors, it's only a bit more work to get a pdf instead of using the screenshot shortcut on my mac.

I know it's not much code to get a pdf from processing but i want it to make it even a litle bit more easier for me.

how can i make a library save a pdf?

I think i'm pretty close, i only can't find the file :S

processing:

Copy code
  1. import processing.pdf.*;
  2. import doeke.methods.*;
  3. PDFSaver pdfSaver;

  4. void setup() {
  5.   pdfSaver = new PDFSaver(this);
  6. }

  7. void draw() {
  8.   background(255);
  9.   for(int i = 0; i < 40; i++) {
  10.     ellipse(random(width), random(height), random(50), random(50));
  11.   }
  12. }

  13. void keyPressed() {
  14.     pdfSaver.save(); 
  15. }

The library:

Copy code
  1. import processing.core.PApplet;


  2. /*
  3.  * Save mode!! name, date, counter etc.
  4.  * 
  5.  * 
  6.  */

  7. public class PDFSaver {
  8. private PApplet p;
  9. private boolean save = false;
  10. private String fileSeparator;
  11. public PDFSaver(PApplet p) {
  12. this.p = p;
  13. fileSeparator = System.getProperty("file.separator");
  14. p.registerPre(this);
  15. p.registerDraw(this);
  16. }
  17. public void save() {
  18. save = true;
  19. }
  20. public void pre() {
  21. if(save) {
  22. // i checked, pre does run
  23. // only in this state a println is not possible.
  24. System.out.println("start save");
  25. p.beginRecord(PApplet.PDF, p.sketchPath+fileSeparator+"test.pdf");
  26. }
  27. }
  28. public void draw() {
  29. if(save) {
  30. System.out.println("end save");
  31. System.out.println(p.sketchPath+fileSeparator+"test.pdf");
  32. p.endRecord();
  33. save = false;
  34. }
  35. }

  36. }

Replies(3)

which jar is used for exporting pdf?
\modes\java\libraries\pdf\library\pdf.jar

or what do you mean? 
Its based on the  iText PDF library  http://itextpdf.com/
damnit, just typed a long story posted and nothing appears... Had to log in again

Anyway, long story short:
this works
(note that the text "nothing" won't show up)


Copy code
  1. public void draw() {
  2. if(saveStep == 1) {
  3. p.beginRecord(PApplet.PDF, p.sketchPath+fileSeparator+"test2.pdf");
  4. p.fill(0);
  5. p.text("nothing", 20, 20);
  6. saveStep = 2;
  7. } else if(saveStep == 2) {
  8. p.fill(0);
  9. p.text("something", 20, 20);
  10. p.endRecord();
  11. saveStep = 0;
  12. }
  13. }