How to export as a PDF?

edited March 2017 in Library Questions

I've created a styled map using tilemill and unfolding maps and now want to export/save it as a pdf. Here's the code I've tried, however the pdf is saving as blank. Can someone help me as to where I am going wrong?

import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.core.*;
import de.fhpotsdam.unfolding.data.*;
import de.fhpotsdam.unfolding.events.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.interactions.*;
import de.fhpotsdam.unfolding.mapdisplay.*;
import de.fhpotsdam.unfolding.mapdisplay.shaders.*;
import de.fhpotsdam.unfolding.marker.*;
import de.fhpotsdam.unfolding.providers.*;
import de.fhpotsdam.unfolding.texture.*;
import de.fhpotsdam.unfolding.tiles.*;
import de.fhpotsdam.unfolding.ui.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.utils.*;
import processing.pdf.*;

UnfoldingMap currentmap;


String[] paths;

void setup() {
 size(500,500,PDF, "filename.pdf");


  processData();

  MapUtils.createDefaultEventDispatcher(this, currentmap);
currentmap.zoomAndPanTo(891f, 234f,7);

}

void processData() {
  String mbTilesString = sketchPath("data/small.mbtiles");
  currentmap = new UnfoldingMap(this, new MBTilesMapProvider(mbTilesString));
}



void draw() {
  currentmap.draw();
  // Exit the program 
  println("Finished.");
  exit();
}

Answers

  • pdf writer might not be aware of unfolding's output.

    in my experience it's great at lines and shapes, things you'd expect to be in a vector-based file format, not so much pixels or lighting effects.

  • AFAIK, PDF is only useful if you're using vector graphics, and in your case a normal image file should be enough.

    Perhaps you could save an image first and convert it to a PDF.

  • Ah ok thanks both! How do I save it as an image instead?

  • Thats easy enough. Use the default renderer (size(500, 500)) an inside draw, after calling currentmap.draw() use save(filename). See reference - https://processing.org/reference/save_.html

  • Thank you!

  • Sorry I have another question, I hope you don't mind...I'm fairly new to proccessing. I'm fine with saving an image now, but am trying to save it as a high res image. I want to scale it up to an A2 or A1 sheet and it's not a high enough quality at the moment.

    Here's the code I've tried ... the normal image is coming out fine, but the high res is coming out blank. I don't know what I am doing wrong :(

      import de.fhpotsdam.unfolding.*;
      import de.fhpotsdam.unfolding.core.*;
      import de.fhpotsdam.unfolding.data.*;
      import de.fhpotsdam.unfolding.events.*;
      import de.fhpotsdam.unfolding.geo.*;
      import de.fhpotsdam.unfolding.interactions.*;
      import de.fhpotsdam.unfolding.mapdisplay.*;
      import de.fhpotsdam.unfolding.mapdisplay.shaders.*;
      import de.fhpotsdam.unfolding.marker.*;
      import de.fhpotsdam.unfolding.providers.*;
      import de.fhpotsdam.unfolding.texture.*;
      import de.fhpotsdam.unfolding.tiles.*;
      import de.fhpotsdam.unfolding.ui.*;
      import de.fhpotsdam.unfolding.utils.*;
      import de.fhpotsdam.utils.*;
      import processing.pdf.*;
    
      UnfoldingMap currentmap;
    
      String[] paths;
    
      void setup() {
       fullScreen();
     processData();
    
     MapUtils.createDefaultEventDispatcher(this, currentmap);
     currentmap.zoomAndPanTo(891f, 234f, 7);
      }
    
      void processData() {
        String mbTilesString = sketchPath("data/small.mbtiles");
        currentmap = new UnfoldingMap(this, new MBTilesMapProvider(mbTilesString));
      }
    
    
      void draw() {
       smooth(4);
     currentmap.draw();
     //save("filename.png"); 
    
      }
      void keyPressed() {  
    
     if (key == 's') {
       save("normal.png");
       saveHiRes(5);
       exit();
     }
      }  
    
      void saveHiRes(int scaleFactor) {
        PGraphics hires = createGraphics(width*scaleFactor, height*scaleFactor, JAVA2D);
        beginRecord(hires);
        hires.scale(scaleFactor);
        draw();
        endRecord();
        hires.save("hires.png");
      }
    
  • Your draw() in line 55 refers to the main draw?

    To save your scaled PGraphics, you need to draw on it. also use resize(): https://processing.org/reference/PImage_resize_.html

    This next is untested code and I am only referring to the relevant functions:

    Kf

    PImage img;   //Global access
    
     void draw() {
      smooth(4);
      currentmap.draw();
      img=get();   //Stores current canvas into iamg object
    
     }
    
     void saveHiRes(int scaleFactor) {
       PGraphics hires = createGraphics(width*scaleFactor, height*scaleFactor, JAVA2D);
       beginRecord(hires);
       hires.image(img.resize(img.width*5,0),0,0);
       endRecord();
       hires.save("hires.png");
     }
    
Sign In or Register to comment.