Initial Commit
This commit is contained in:
@@ -0,0 +1,377 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<title>Pdf \ Libraries \ Processing 2+</title>
|
||||
|
||||
<link rel="icon" href="./img/favicon.ico" type="image/x-icon" />
|
||||
<link rel="shortcut icon" href="./img/favicon.ico" type="image/x-icon" />
|
||||
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="Author" content="Processing Foundation" />
|
||||
<meta name="Publisher" content="Processing Foundation" />
|
||||
<meta name="Keywords" content="Processing, Sketchbook, Programming, Coding, Code, Art, Design" />
|
||||
<meta name="Description" content="Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology." />
|
||||
<meta name="Copyright" content="All contents copyright the Processing Foundation, Ben Fry, Casey Reas, and the MIT Media Laboratory" />
|
||||
|
||||
<script src="../../javascript/MM_functions.js" type="text/javascript"></script>
|
||||
|
||||
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body id="Libraries" onload="" >
|
||||
|
||||
<!-- ==================================== PAGE ============================ -->
|
||||
<div id="container">
|
||||
|
||||
<!-- ==================================== HEADER ============================ -->
|
||||
<div id="header">
|
||||
<a href="http://processing.org/" title="Processing.org"><div class="processing-logo" alt="Processing cover"></div></a>
|
||||
<form name="search" action="//www.google.com/search" method="get" >
|
||||
<!--<label>Search processing.org:</label>-->
|
||||
<p><input type="hidden" name="as_sitesearch" value="processing.org" />
|
||||
<input type="text" name="as_q" value="" size="20" class="text" />
|
||||
<input type="submit" value=" " /></p>
|
||||
</form>
|
||||
</div>
|
||||
<div id="navigation">
|
||||
<div class="navBar" id="mainnav">
|
||||
<a href="../../index.html">Language</a><br>
|
||||
<a href="../../libraries/index.html" class="active">Libraries</a><br>
|
||||
<a href="../../tools/index.html">Tools</a><br>
|
||||
<a href="../../environment/index.html">Environment</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<a id="TOP" name="TOP"></a>
|
||||
|
||||
<!-- ==================================== CONTENT - Headers ============================ -->
|
||||
<div class="content">
|
||||
|
||||
|
||||
<div class="librarytitle"><h3>PDF Export</h3></div>
|
||||
|
||||
<div class="full-alt">
|
||||
<p>The PDF library makes it possible to write PDF files directly from Processing. These vector graphics files can be scaled to any size and output at very high resolutions. The PDF library can flatten 3D data into a 2D vector file, but to export 3D data, use the DXF library. The source code is available on the <a href="https://github.com/processing/processing/tree/master/java/libraries/pdf">Processing</a> GitHub repository. Please report bugs <a href="https://github.com/processing/processing/issues">here</a>.<br />
|
||||
<br />
|
||||
This library is frequently used with the core Processing function <strong>size()</strong>, with a combination of <strong>beginRecord()</strong> and <strong>endRecord()</strong>, or with <strong>beginRaw()</strong> and <strong>endRaw()</strong>. The <strong>createGraphics()</strong> function can also be useful. See the examples below for different techniques.</p>
|
||||
</div>
|
||||
|
||||
<div class="full">
|
||||
<p>
|
||||
<strong>Single Frame (No Screen Display)</strong><br />
|
||||
<br />
|
||||
This example draws a single frame to a PDF file and quits. (Note that no display window will open; this helps when you're trying to create massive PDF images that are far larger than the screen size.)</p>
|
||||
|
||||
<pre>import processing.pdf.*;
|
||||
|
||||
void setup() {
|
||||
size(400, 400, PDF, "filename.pdf");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// Draw something good here
|
||||
line(0, 0, width/2, height);
|
||||
|
||||
// Exit the program
|
||||
println("Finished.");
|
||||
exit();
|
||||
}</pre>
|
||||
|
||||
|
||||
<p><strong>Multiple Pages (No Screen Display)</strong><br />
|
||||
<br />
|
||||
It's possible to write every frame as a new page in the PDF document.
|
||||
This example creates a 100 page document:</p>
|
||||
|
||||
<pre>import processing.pdf.*;
|
||||
|
||||
void setup() {
|
||||
size(400, 400, PDF, "filename.pdf");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// Draw something good here
|
||||
line(0, 0, frameCount * 4, height);
|
||||
|
||||
PGraphicsPDF pdf = (PGraphicsPDF) g; // Get the renderer
|
||||
|
||||
// When finished drawing, quit and save the file
|
||||
if (frameCount == 100) {
|
||||
exit();
|
||||
} else {
|
||||
pdf.nextPage(); // Tell it to go to the next page
|
||||
}
|
||||
}</pre>
|
||||
|
||||
|
||||
<p><strong>Single Frame (With Screen Display)</strong><br />
|
||||
<br />
|
||||
To draw to the screen while also saving a PDF, use the beginRecord()
|
||||
and endRecord() functions. This is slower, but is useful when you need to
|
||||
see what you're working on as it saves.</p>
|
||||
|
||||
<pre>import processing.pdf.*;
|
||||
|
||||
void setup() {
|
||||
size(400, 400);
|
||||
noLoop();
|
||||
beginRecord(PDF, "filename.pdf");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// Draw something good here
|
||||
line(0, 0, width/2, height);
|
||||
|
||||
endRecord();
|
||||
}</pre>
|
||||
|
||||
|
||||
<p><strong>Single Frame from an Animation (With Screen Display)</strong><br />
|
||||
It's also possible to save one frame from a program with moving elements.
|
||||
Create a boolean variable to turn the PDF recording process on and off</p>
|
||||
|
||||
<pre>import processing.pdf.*;
|
||||
|
||||
boolean record;
|
||||
|
||||
void setup() {
|
||||
size(400, 400);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (record) {
|
||||
// Note that #### will be replaced with the frame number. Fancy!
|
||||
beginRecord(PDF, "frame-####.pdf");
|
||||
}
|
||||
|
||||
// Draw something good here
|
||||
background(255);
|
||||
line(mouseX, mouseY, width/2, height/2);
|
||||
|
||||
if (record) {
|
||||
endRecord();
|
||||
record = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Use a keypress so thousands of files aren't created
|
||||
void mousePressed() {
|
||||
record = true;
|
||||
}</pre>
|
||||
|
||||
|
||||
<p><strong>Many Frames Into One File (With Screen Display)</strong><br />
|
||||
<br />
|
||||
The following example records absolutely everything that happens
|
||||
while a sketch is running. Hitting the 'q' key will quit the sketch.
|
||||
The sketch calls exit(), which is necessary to make sure that the file
|
||||
is properly written when complete.</p>
|
||||
|
||||
<pre>import processing.pdf.*;
|
||||
|
||||
void setup() {
|
||||
size(400, 400);
|
||||
beginRecord(PDF, "everything.pdf");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// Be sure not to call background, otherwise your file
|
||||
// will just accumulate lots of mess, only to become invisible
|
||||
|
||||
// Draw something good here
|
||||
line(mouseX, mouseY, width/2, height/2);
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
if (key == 'q') {
|
||||
endRecord();
|
||||
exit();
|
||||
}
|
||||
}</pre>
|
||||
|
||||
|
||||
<p><strong>Pausing While Recording (With Screen Display)</strong><br />
|
||||
<br />
|
||||
It's also possible to pause/resume recording along the way.
|
||||
The following example toggles recording on/off each time you hit the 'r' key.
|
||||
Pressing the 'q' key will quit the sketch.</p>
|
||||
|
||||
<pre>import processing.pdf.*;
|
||||
|
||||
boolean recording;
|
||||
PGraphicsPDF pdf;
|
||||
|
||||
void setup() {
|
||||
size(400, 400);
|
||||
pdf = (PGraphicsPDF) createGraphics(width, height, PDF, "pause-resume.pdf");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// Be sure not to call background, otherwise your file
|
||||
// will just accumulate lots of mess, only to become invisible
|
||||
|
||||
// Draw something good here
|
||||
if (mousePressed) {
|
||||
line(pmouseX, pmouseY, mouseX, mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
if (key == 'r') {
|
||||
if (recording) {
|
||||
endRecord();
|
||||
println("Recording stopped.");
|
||||
recording = false;
|
||||
} else {
|
||||
beginRecord(pdf);
|
||||
println("Recording started.");
|
||||
recording = true;
|
||||
}
|
||||
} else if (key == 'q') {
|
||||
if (recording) {
|
||||
endRecord();
|
||||
}
|
||||
exit();
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
|
||||
|
||||
<p><strong>PDF Files from 3D Geometry (With Screen Display)</strong><br />
|
||||
<br />
|
||||
To create vectors from 3D data, use the beginRaw() and endRaw() commands.
|
||||
These commands will grab the shape data just before it is rendered to the screen.
|
||||
At this stage, your entire scene is nothing but a long list of lines and triangles.
|
||||
This means that a shape created with sphere() method will be made up of hundreds of
|
||||
triangles, rather than a single object.</p>
|
||||
|
||||
<p>When using beginRaw() and endRaw(), it's possible to write to either a 2D or 3D renderer.
|
||||
For instance, beginRaw() with the PDF library will write the geometry as flattened triangles
|
||||
and lines.</p>
|
||||
|
||||
<pre>import processing.pdf.*;
|
||||
|
||||
boolean record;
|
||||
|
||||
void setup() {
|
||||
size(500, 500, P3D);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (record) {
|
||||
beginRaw(PDF, "output.pdf");
|
||||
}
|
||||
|
||||
// Do all your drawing here
|
||||
background(204);
|
||||
translate(width/2, height/2, -200);
|
||||
rotateZ(0.2);
|
||||
rotateY(mouseX/500.0);
|
||||
box(200);
|
||||
|
||||
if (record) {
|
||||
endRaw();
|
||||
record = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Hit 'r' to record a single frame
|
||||
void keyPressed() {
|
||||
if (key == 'r') {
|
||||
record = true;
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p><strong>Using createGraphics() to Create a PDF File</strong><br />
|
||||
<br />
|
||||
To write a PDF file using only the createGraphics() command, rather than as
|
||||
part of a sketch, it's necessary to call dispose() on the PGraphicsPDF object.
|
||||
This is the same as calling exit(), but it won't quit the sketch.</p>
|
||||
|
||||
<pre>import processing.pdf.*;
|
||||
|
||||
PGraphics pdf = createGraphics(300, 300, PDF, "output.pdf");
|
||||
pdf.beginDraw();
|
||||
pdf.background(128, 0, 0);
|
||||
pdf.line(50, 50, 250, 250);
|
||||
pdf.dispose();
|
||||
pdf.endDraw();</pre>
|
||||
|
||||
|
||||
<p>
|
||||
<strong>Additional notes for the PDF renderer:</strong>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>If you want 3D data, use the DXF recording library instead.
|
||||
|
||||
<li>Using hint(ENABLE_DEPTH_SORT) can improve the appearance of 3D geometry drawn to 2D file formats.
|
||||
|
||||
<li>Images don't look great, mostly because of the difference of
|
||||
expectations in how a PDF should look (scalable and high res) versus
|
||||
what happens when image data is written to it at 72 dpi.
|
||||
|
||||
<li>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. To force text
|
||||
to be treated as shape data, use textMode(SHAPE), immediately after size().
|
||||
<!--See the developer reference for textMode() inside
|
||||
<A HREF="http://dev.processing.org/reference/everything/javadoc/processing/pdf/PGraphicsPDF.html">
|
||||
PGraphicsPDF</A> for more specifics.-->
|
||||
|
||||
<li>Another option for type, is to use createFont() with a TrueType font
|
||||
(some OpenType fonts also work). Any font that shows up in PFont.list()
|
||||
should work, or if not, adding a .ttf file to the data directory and
|
||||
calling createFont("fontname.ttf") will also work.
|
||||
|
||||
<li>If loadFont() is used instead of createFont(), the text may show up
|
||||
bitmapped and ugly. In some cases, hint(ENABLE_NATIVE_FONTS)
|
||||
may improve the situation if the font is installed on the machine.
|
||||
|
||||
<!-- Removed by REAS, 1 September 2011, couldn't find reference in the current Issue database-->
|
||||
<!--<li>On Mac OS X, the seems to be a bug in Apple's Java implementation
|
||||
that confuses cubic vs. quadric splines. This makes text look jagged,
|
||||
even though it's not bitmapped.
|
||||
(<A HREF="http://dev.processing.org/bugs/show_bug.cgi?id=404">Bug 404</A>)-->
|
||||
|
||||
<!-- Removed by REAS, 1 September 2011, couldn't find reference in the current Issue database-->
|
||||
<!--<li>If the results with beginRaw() from P3D to PDF look bizarre,
|
||||
it's probably because the polygons are not sorted.
|
||||
To attempt sorting of 3D shapes before they're written to a file,
|
||||
use hint(ENABLE_DEPTH_SORT). This method is still
|
||||
not particularly good, but will help in some situations.
|
||||
See <a href="http://dev.processing.org/bugs/show_bug.cgi?id=176">Bug
|
||||
176</a> for status of the implementation.</p>-->
|
||||
|
||||
<li>Starting a new page is a matter of calling nextPage() like so:
|
||||
<pre>
|
||||
PGraphicsPDF pdf = (PGraphicsPDF) g;
|
||||
pdf.nextPage();
|
||||
</pre>
|
||||
(This example is also shown above)
|
||||
|
||||
<li> Again, exit() is really important when using PDF with size().
|
||||
|
||||
</UL>
|
||||
|
||||
</p>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<!-- ==================================== FOOTER ============================ -->
|
||||
<div id="footer">
|
||||
<div id="copyright">Processing was initiated by <a href="http://benfry.com/">Ben Fry</a> and <a href="http://reas.com">Casey Reas</a>. It is developed by a <a href="../../about/people.html">small team of volunteers</a>.</div>
|
||||
<div id="colophon">
|
||||
|
||||
<a href="../../copyright.html">© Info</a>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user