We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Error writing text to PDF using begin/endRecord
Page Index Toggle Pages: 1
Error writing text to PDF using begin/endRecord (Read 1847 times)
Error writing text to PDF using begin/endRecord
Mar 19th, 2010, 1:28pm
 
Hi, I'm new to processing, and I'm running v1.1 on OSX10.5.  I've run into a snag trying to output text to a PDF file and I'm looking for some help.  Things seem to work if I use the size() method to write to PDF.  For example, both of these code sections work great:

Code:

import processing.pdf.*;
size(512, 512, PDF, "test.pdf");
background(255);
PFont arial = createFont("Arial", 48);
textFont(arial);
fill(0);
text("Hello!",100,50);
exit();
Code:

import processing.pdf.*;
PFont font;
void setup() {
 size(512,512,PDF,"test.pdf");
 background(255);
 font = createFont("Arial",48);
 textFont(font);
 fill(0);
 noLoop();
}
void draw() {
 text("Hello!",200,200);
 exit();
}


But if I move to using the beginRecord/endRecord solution to capture a single frame I get an export error message.  The code that's producing the error is:
Code:

import processing.pdf.*;
boolean bool;
PFont font;
void setup() {
 size(512,512,PDF,"test.pdf");
 background(255);
 bool = true;
 font = createFont("Arial",48);
 textFont(font);
 fill(0);
}
void draw() {
 if (bool) {
   beginRecord(PDF, "test.pdf");
 }
 text("Hello!",200,200);
 if (bool) {
   endRecord();
   bool = false;
 }
}


The error trace that this produces for me is:
Code:

alias for Arial = Arial
alias for SansSerif = null
Use PGraphicsPDF.listFonts() to get a list of fonts that can be used with PDF.
processing.app.debug.RunnerException: RuntimeException: The font “SansSerif” cannot be used with PDF Export.
at processing.app.Sketch.placeException(Sketch.java:1565)
at processing.app.debug.Runner.findException(Runner.java:568)
at processing.app.debug.Runner.reportException(Runner.java:543)
at processing.app.debug.Runner.exception(Runner.java:498)
at processing.app.debug.EventThread.exceptionEvent(EventThread.java:367)
at processing.app.debug.EventThread.handleEvent(EventThread.java:255)
at processing.app.debug.EventThread.run(EventThread.java:89)
Exception in thread "Animation Thread" java.lang.RuntimeException: The font “SansSerif” cannot be used with PDF Export.
at processing.pdf.PGraphicsPDF.textLineImpl(PGraphicsPDF.java:414)
at processing.core.PGraphics.textLineAlignImpl(PGraphics.java:3509)
at processing.core.PGraphics.text(PGraphics.java:3208)
at processing.core.PGraphics.text(PGraphics.java:3163)
at processing.core.PApplet.text(PApplet.java:8632)
at sketch_mar19a.draw(sketch_mar19a.java:37)
at processing.core.PApplet.handleDraw(PApplet.java:1594)
at processing.core.PApplet.run(PApplet.java:1496)
at java.lang.Thread.run(Thread.java:613)


So it looks like using begin/endRecord is expecting the SansSerif font, rather than Arial, and gets confused.  I'm probably doing something wrong or missing something.  Any help would be great!

Thanks!
--mah
Re: Error writing text to PDF using begin/endRecord
Reply #1 - Mar 20th, 2010, 2:33am
 
it gives you a clue as to how to find out available fonts in the error message:

"Use PGraphicsPDF.listFonts() to get a list of fonts that can be used with PDF."

there's additional font stuff at the bottom here:
http://processing.org/reference/libraries/pdf/index.html
Re: Error writing text to PDF using begin/endRecord
Reply #2 - Mar 20th, 2010, 4:08am
 
Since 1.1, PGraphics creates a default font if none is specified.
It defaults to SansSerif, which is OK in most modes because Java always supplies it. But it is not usable in PDF mode, which doesn't know this font. Perhaps it should override the createDefaultFont method.

Solution on your side: mmm, I am not sure, perhaps call textFont after the beginRecord call.

[EDIT] Tested: it works!
I wish the authors hasn't forgotten to remove the println() (debug information) repeating alias for Arial = Arial (or similar) each time we use text() in PDF mode. Next version, probably...
Re: Error writing text to PDF using begin/endRecord
Reply #3 - Mar 22nd, 2010, 5:39am
 
Awesome -- Thanks!  Adding an extra "textFont(font);" right after beginRecord did the trick.
Page Index Toggle Pages: 1