WordCram now outputs to PDF. It’ll be in the 0.5 release, but you can grab the new daily build right now (zip or tar) and try it out. The only new code you’ll need is normal Processing PDF stuff, all the WordCram code stays the same. Just import processing.pdf.*, and use size(width, height, PDF, "path/to/output.pdf").
Here’s an example, a PDF word cloud from the Wikipedia page on PDF. Here’s the code to produce it:
import processing.pdf.*;
import wordcram.*;
void setup() {
size(700, 700, PDF, "wordcram.pdf");
background(255);
new WordCram(this)
.fromWebPage("http://en.wikipedia.org/wiki/Portable_Document_Format")
.withColors(#000000, #777777, #ff0000)
.withFonts("LiberationSans")
.sizedByWeight(1, 200)
.withWordPadding(1)
.angledAt(0)
.withPlacer(new WordPlacer() {
public PVector place(Word w, int r, int c, int ww, int wh, int fw, int fh) {
float xScatter = (1-w.weight);
float x = map(random(-xScatter, xScatter), -1, 1, 30, fw-ww-30);
float y = (1-pow(w.weight, 0.25)) * (fh - wh) + 30;
return new PVector(x, y);
}
})
.drawAll();
}
I’m pretty excited about this. PDF output was one of the first features asked for, and it’s been a goal of mine to finish it. But more importantly, we can now print some really nice, high-res word clouds!
I am pretty new to WordCram and already excited.
One thing I was wondering about: Isn’t WordCram rendering on a “normal” Java2D Graphics?
If so, we should be easily able to substitute that by Batiks SVGGraphics2D implementation which renders directly to SVG.
Hello Bastian,
That’s an interesting idea. I know Batiks only from poking around the Geomerative source (if I remember right, it uses Batiks to turn .ttfs into paths), but I’ll take a look. If I can find a way to make it fit with the Processing stuff, I’m all for it.
Hi, thanks for this, it’s a superb library and very thorough. I have been trying to get the record pdf function to work, I can get it to work in the way described above however, the beginRecord() and endRecord() don’t seem to capture the output. Any advice on this would be much appreciated, thanks!
Sher, you shouldn’t need beginRecord() and endRecord(). Does the above code work for you? Does the PDF example in the download run for you? (You can find it via File > Examples…, then looking under Contributed Libraries > WordCram > OtherExamples > renderToPdf.)
Thanks for the quick reply. I can get it to output a pdf in this way, yes. I just wanted to output more than one! If I save a png during the loop then I can reloop and save different versions, but I can’t seem to do this with pdfs if that makes sense? Thanks again.
Oh! Ok, that makes sense. Though I’m not sure whether Processing supports the draw-loop for PDF output…