Skip to content

Instantly share code, notes, and snippets.

@Sciss
Created March 2, 2014 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sciss/9310976 to your computer and use it in GitHub Desktop.
Save Sciss/9310976 to your computer and use it in GitHub Desktop.
~jar = "itextpdf-5.4.0.jar";
~url = "http://search.maven.org/remotecontent?filepath=com/itextpdf/itextpdf/5.4.0/" ++ ~jar;
~tmp = PathName.tmp ++ ~jar
("curl" + ~url + ">" ++ ~tmp).systemCmd;
g = SwingOSC.default;
g.reboot;
g.addClasses("file:" ++ ~tmp);
GUI.swing;
x = Array.fill(1000, { 1.0.bilinrand });
GUI.skin[\plot].background = Color.white;
p = x.plot("Plot", Rect(0, 0, 1000, 500));
p.resolution = 0.1;
v = p.interactionView;
(
~pdfScreenShot = { arg view, fileName, width, height;
var g, bounds, pageSize, doc, writerC, stream, writer, cb, tp, g2, viewJ, vw, vh;
g = view.server;
bounds = view.bounds;
vw = bounds.width;
vh = bounds.height;
width = width ? vw;
height = height ? vh;
pageSize = JavaObject("com.itextpdf.text.Rectangle", g, 0, 0, width, height);
doc = JavaObject("com.itextpdf.text.Document", g, pageSize, 0, 0, 0, 0);
writerC = JavaObject.getClass("com.itextpdf.text.pdf.PdfWriter", g);
stream = JavaObject("java.io.FileOutputStream", g, fileName);
writer = writerC.getInstance__( doc, stream);
doc.open;
cb = writer.getDirectContent__;
tp = cb.createTemplate__(vw, vh);
g2 = tp.createGraphics__(vw, vh);
viewJ = JavaObject.basicNew(view.id, g);
viewJ.paint(g2);
g2.dispose;
cb.addTemplate(tp, (width - vw) / 2, (height - vh) / 2); // centered
doc.close;
doc.destroy; cb.destroy; g2.destroy; tp.destroy;
writer.destroy; stream.destroy; writerC.destroy; pageSize.destroy;
};
)
~pdf = "~/Desktop/test.pdf".standardizePath;
~pdfScreenShot.(v, ~pdf);
("open" + ~pdf).unixCmd; // this works on OS X only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment