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 › Exporting text to a PDF
Page Index Toggle Pages: 1
Exporting text to a PDF (Read 1057 times)
Exporting text to a PDF
Feb 20th, 2008, 3:11pm
 
I am trying to export text to a PDF file, but all I get are little blocks of white, gray, or black, instead of the actual letterforms.  (It looks like all the letters have been obscured, as in a confidential government document released to the public.)  I am sure I'm missing something obvious, and would appreciate your suggestions.  Here's my story.  I'm using OS X 10.5.2.

I started out trying beginRecord(), and have also tried beginRaw(), and createGraphics(), all with the same issue.  I see this bug for createGraphics():

http://dev.processing.org/bugs/show_bug.cgi?id=641

...which would explain the problem I'm seeing, but I don't think that affects the other two methods.

I started out using a .vlw font file:  

Code:
font = loadFont("Monaco-24.vlw"); 



Then, thinking that it would be better to use outlines rather than the bitmapped VLW format, I tried using a native font:

Code:
hint(ENABLE_NATIVE_FONTS);
font = loadFont("Monaco.ttf");


This gives me an error:  "Could not load font Monaco.ttf. Make sure that the font has been copied to the data folder of your sketch."  Of course, I have verified that the TTF file is in the data folder, so I'm not sure why it can't find it.

Then, I tried using createFont():

Code:
hint(ENABLE_NATIVE_FONTS);
font = createFont("Monaco.ttf", 24);


This does not give an error, but simply hangs the applet.

Also, this note in the PDF library seems relevant:

Quote:
Starting in release 0120, text is no longer treated as shape data by default, meaning that the font will need to be installed to view the PDF that's created. The upside is that the PDF will render better (particularly in light of the Mac OS X bug noted here). To force text to be treated as shape data, use textMode(SHAPE), immediately after size(). See the developer reference for textMode() inside PGraphicsPDF for more specifics.


...but when I use textMode(SHAPE), I get an error that SHAPE is not available with the P3D renderer.  So I switch to OPENGL (fine for this project), and instead of little blocks in my PDF, I get a gray canvas with one diagonal gray line through it.

Thanks in advance for your thoughts!
Re: Exporting text to a PDF
Reply #1 - Feb 25th, 2008, 3:05pm
 
The issue has been resolved.  I ended up using the P3D renderer with createFont() (with a .ttf file) and createGraphics().  I believe this is the recommended way to get vector text into a PDF.  

The primary hiccup then was getting the right format for the TrueType font file, which I could not find documented anywhere.  I'll document the font generation settings I used in case anyone else has the same problem.

Using your font conversion tool of choice, try these settings that worked for me:

Encoding: PC, Adobe Standard
Outlines: TrueType format (no bitmap needed)
All other options should be set to defaults

You may experiment with other encodings, but that was the one that worked for me.
Re: Exporting text to a PDF
Reply #2 - May 19th, 2008, 11:57pm
 
Hi!
Could you please post the solution-code?
I'm having the same problem.
Re: Exporting text to a PDF
Reply #3 - May 20th, 2008, 4:22pm
 
Sure thing.  I stripped out all the irrelevant stuff specific to my project.  Here's the code relevant for using a TrueType font and exporting to PDF:

Code:
import processing.pdf.*;

boolean triggerPDF = false;
int docNumber = 1;

public void setup() {
size(screen.width, screen.height, P3D);
hint(ENABLE_NATIVE_FONTS);
font = createFont("MONACO__.TTF", 24);
}


void draw() {
// Your draw loop code here, for what you want put on-screen
// If mouse was clicked, then PDF is generated.
if (triggerPDF) { // If trigger is true,
exportPDF(); // create PDF.
triggerPDF = false; // And this, too.
}
}


void mousePressed() { // When mouse is clicked,
triggerPDF = !triggerPDF; // trigger PDF
}


public void exportPDF() {
PGraphics pdf = createGraphics(550, 720, PDF, "Document_Name" + docNumber + ".pdf");
pdf.beginDraw();
pdf.background(#FFFFFF);
pdf.fill(0);
pdf.scale(4.5);
pdf.textFont(font, fontSize);
// Add your code for "drawing" to the PDF here, e.g. draw text like this:
pdf.text("Draw this text in the PDF", 0, 0);
pdf.dispose();
pdf.endDraw();
docNumber++; // Increment docNumber for the next time around
}
Re: Exporting text to a PDF
Reply #4 - May 20th, 2008, 9:35pm
 
hmmm. that does not work.
the problem is, that i want to export a frame of what i draw on the screen. that is letters, positioned in 3d space according to the coordinates of an imported 3d object.

or did i miss something...?
Re: Exporting text to a PDF
Reply #5 - May 20th, 2008, 10:19pm
 
The way Processing works, you can either "draw" something to the screen OR to a PDF.  So, in the code I posted, replace the line that says "Add your code for "drawing" to the PDF here" with the code you're currently using to draw your image.  Note, however, how instead of using text(), you should use pdf.text() to indicate that the characters should be drawn to the PDF (and not the screen).

There may be an easier way, if all you want to do is essentially take a screen shot of the Processing window and stick that in a PDF, but that would result in a bitmapped image.  For my purposes, I was trying to keep the PDF in a vector (high-res) format.

Does that help?
Re: Exporting text to a PDF
Reply #6 - May 22nd, 2008, 5:43pm
 
I think, i have to do it in another way because i can't use
3D - coordinates with pdf.text()

Now I will try to use imported .obj s and place them. then export a pdf using
beginRaw() and endRaw().

But i suppose, that might be too memory consuming.
Re: Exporting text to a PDF
Reply #7 - May 22nd, 2008, 6:53pm
 
As far as I can tell there's no way to place text in 3D and record it to PDF with nice fonts.

With beginRaw(PDF...); the text is blocky. With beginRecord(RDF...); you can't do 3D translates, and with createGraphics(...PDF...);  you can't do a 3D translate either.
Re: Exporting text to a PDF
Reply #8 - May 22nd, 2008, 8:09pm
 
But if the letters are 3d objects (.obj),
then it should go with beginRaw()
Page Index Toggle Pages: 1