<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    >

<channel>
    <title>Webmonkey &#187; pdf</title>
    <atom:link href="http://www.webmonkey.com/tag/pdf/feed/" rel="self" type="application/rss+xml" />
    <link>http://www.webmonkey.com</link>
    <description>The Web Developer&#039;s Resource</description>
    <lastBuildDate>Mon, 06 May 2013 17:29:19 +0000</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <generator>http://wordpress.org/?v=3.4.2</generator>
    
    <item>
        <title>Generate PDFs Dynamically With PHP</title>
        <link>http://www.webmonkey.com/2010/02/generate_pdfs_dynamically_with_php/</link>
        <comments>http://www.webmonkey.com/2010/02/generate_pdfs_dynamically_with_php/#comments</comments>
        <pubDate>Tue, 16 Feb 2010 01:45:47 +0000</pubDate>

                <dc:creator>Webmonkey Staff</dc:creator>

        <guid isPermaLink="false">http://stag.wired.com/primate/?p=731</guid>
        		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wiki]]></category>
        <description><![CDATA[PDF is the Portable Document Format developed by Adobe. It&#8217;s an open standard implemented by Adobe in their Acrobat series of software, but implementable and extensible by anybody who&#8217;s got the time, inclination, and knack. One trick that&#8217;s got a lot of potential is using PHP to dynamically generate PDF files and serve them via [...]]]></description>

            <content:encoded><![CDATA[<!-- wpautop disabled --><p>PDF is the Portable Document Format developed by Adobe. It&#8217;s an <a href="http://www.adobe.com/devnet/acrobat/" class="external text" title="http://www.adobe.com/devnet/acrobat/" rel="nofollow">open standard</a> implemented by Adobe in their Acrobat series of software, but implementable and extensible by anybody who&#8217;s got the time, inclination, and knack. One trick that&#8217;s got a lot of potential is using PHP to dynamically generate PDF files and serve them via the web.

</p><p>PHP can do a lot for your web operation (read our <a href="/2010/02/PHP_Tutorial_for_Beginners" title="Tutorial:PHP Tutorial for Beginners">PHP Tutorial for Beginners</a> tutorial). You can generate nice-looking printable receipts, invoices, and brochures. <a href="http://www.vanhemert.co.uk/disc-cover.html" class="external text" title="http://www.vanhemert.co.uk/disc-cover.html" rel="nofollow">Disc-Cover</a> has a test site that looks up info about a CD automatically and then generates a PDF label for the CD box that you can print, cut out, and use. And there are literally one billion other possible uses for dynamically generated PDFs.

</p><p>So what are you waiting for?

</p><p>You have a variety of PDF-generation options. The standard, classic way of doing it is with <a href="http://www.pdflib.com/" class="external text" title="http://www.pdflib.com/" rel="nofollow">PDFlib</a>. Because it&#8217;s so widely used and well-integrated into PHP, that&#8217;s the library I&#8217;ll go over today. But it&#8217;s by no means the only way of doing things. PDFlib is source-available, but not free. The license specifies that PDFlib can be used and redistributed without charge for non-commercial projects, but commercial use carries a fee.



</p><p>There are also a number of completely free options. These include <a href="http://www.ros.co.nz/pdf/" class="external text" title="http://www.ros.co.nz/pdf/" rel="nofollow">R&amp;OS</a> and <a href="http://www.fpdf.org/en/home.php" class="external text" title="http://www.fpdf.org/en/home.php" rel="nofollow">FPDF</a>, <a href="http://www.stillhq.com/opensource.html" class="external text" title="http://www.stillhq.com/opensource.html" rel="nofollow">Panda</a>. The choice is yours. (I haven&#8217;t had a chance to test these free packages very thoroughly. If you have had negative or positive experiences with them, please do <a href="mailto:adams@pote.com" class="external text" title="mailto:adams@pote.com" rel="nofollow">let me know</a>.)

</p><span id="more-731"></span><p><br />

</p>

<table id="toc" class="toc" summary="Contents"><tbody><tr><td><div id="toctitle"><h2>Contents</h2> </div>



<ol>

<li><a href="#Installing_It">Installing It</a></li>

<li><a href="#Using_It">Using It</a></li>

<li><a href="#Displaying_Text">Displaying Text</a></li>

<li><a href="#Wrapping_Up">Wrapping Up</a></li>



</ol>

</td></tr></tbody></table>



<a name="Installing_It"></a><h3> <span class="mw-headline">Installing It</span></h3>

<p>In order to install PDFlib, you first have to download the <a href="http://www.pdflib.com/download/" class="external text" title="http://www.pdflib.com/download/" rel="nofollow">PDFlib</a> source (precompiled binaries are available but they watermark their output). Uncompress the package and compile and install it according to the instructions for your platform. We are assuming that you already have a working build of PHP. Instructions for installing PDFlib are included with the download, but I&#8217;ll go over the instructions for Unixy systems (including Mac OS X) anyway.

</p><p>This article may become dated in terms of version and patch information. In general, it&#8217;s a good idea to download the <a href="http://www.pdflib.com/fileadmin/pdflib/pdf/support/PDFlib-in-PHP-HowTo.pdf" class="external text" title="http://www.pdflib.com/fileadmin/pdflib/pdf/support/PDFlib-in-PHP-HowTo.pdf" rel="nofollow">PDFlib on PHP manual</a> (PDF link) and read the most current instructions before installing PDFlib.

</p><p>Install all the files in their appropriate places according to the instructions.

</p><p>In your PHP directory, rebuild the configure file:<tt>./buildconf</tt>



</p><p>Rebuild PHP, adding the switch <tt>--with-pdflib=PDFDIR</tt> (change PDFDIR to the name of the directory where PDFlib is installed).

</p><p>Restart Apache, and there you go.

</p><p><br />

</p>

<a name="Using_It"></a><h3> <span class="mw-headline">Using It</span></h3>

<p>PDFlib, in conjunction with PHP, has over a hundred functions available for creating PDFs. To conserve time, space, and sanity, I&#8217;ll only cover a few of these. The full gamut is explained both on the <a href="http://www.php.net/manual/en/ref.pdf.php" class="external text" title="http://www.php.net/manual/en/ref.pdf.php" rel="nofollow">PHP site</a> and in the documentation that accompanies the PDFlib download.

</p><p>Let&#8217;s zip through a step-by-step explanation of creating a new PDF document. Nothing fancy, just a couple of lines of text on a page. To start with, we need to initialize a new PDF object with the pdf_new() function. We will assign the object to the variable $pdf. This object handle is how we&#8217;ll refer to the PDF we&#8217;re creating from now on.

</p>

<pre class="brush: js">  $pdf = pdf_new();



</pre>

<p>Next we want to create a new blank PDF file. This is done with the <a href="http://www.php.net/manual/en/function.pdf-begin-document.php" class="external text" title="http://www.php.net/manual/en/function.pdf-begin-document.php" rel="nofollow">PDF_begin_document()</a> function. We tell it the name of the PDF object we&#8217;re working with:

</p>

<pre class="brush: js">  PDF_begin_document($pdf);

</pre>

<p>Moving swiftly along:we&#8217;ve got a PDF object containing one blank PDF file. Now we start to fill it in. First let&#8217;s set the metadata fields for the PDF. This step is optional. Every PDF has header information in which the document title, creator, and so forth can be specified. We set this data with the <a href="http://www.php.net/manual/en/function.pdf-set-info.php" class="external text" title="http://www.php.net/manual/en/function.pdf-set-info.php" rel="nofollow">pdf_set_info()</a> function. This function takes three arguments:the PDF object we&#8217;re working with, the name of the property we want to set, and the value we want to set it to. The predefined property names you can use are Author, Creator, Keywords, Subject, and Title. So, for example:

</p>

<pre class="brush: js">  pdf_set_info($pdf, "Author", "Paul Adams");

</pre>



<p>Next we create the first page of our PDF, specifying width and then height, in points. A point is 1/72 of an inch, so if we want an 8.5-by-11-inch page, we do a little multiplying:

</p>

<pre class="brush: js">  pdf_begin_page($pdf, (72 * 8.5), (72 * 11));

</pre>

<p>(If you are following along with pencil and paper, you will see that we could have substituted &#8220;612&#8243; and &#8220;792&#8243; for the width and height terms.)

</p><p>Now we&#8217;ve got everything set up. Next, let&#8217;s get to the meat of our PDF:the content. First we need to choose a font to display our text. This is a two-step process:locating the font, and then using it. The PDFlib manual is very helpful on this topic.

</p><p><br />

</p>

<a name="Displaying_Text"></a><h3> <span class="mw-headline">Displaying Text</span></h3>

<p><i>This section covers the use of a <b>depricated function</b>. Please help us keep Webmonkey up to date by editing it.</i>

</p><p>The function we use to locate and prepare the font is <tt>pdf_findfont()</tt>. It takes four arguments:the PDF object handle, the font name, the encoding of the font, and whether or not the font should be embedded in the document. We are going to use one of the 14 fonts built in to PDFlib, which means we don&#8217;t have to get our hands too dirty. It means we don&#8217;t have to embed the font (which we indicate by a &#8220;0&#8243; value for the last argument), and the encoding is &#8220;host&#8221;. These 14 fonts are (still have that pencil ready?):Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique, Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Symbol, and ZapfDingbats. For our text we&#8217;ll use Times-Roman, okay? pdf_findfont() returns a font handle, which we&#8217;ll pass to the $font variable:



</p>

<pre class="brush: js">  $font = pdf_findfont($pdf, "Times-Roman", "host", 0)

</pre>

<p>If something goes wrong, the font handle $font will be set to &#8220;FALSE&#8221;. But nothing ever goes wrong. Part Two is to tell the renderer that we want to use that font, and to specify the size in points, with:

</p>

  <pre class="brush: js">pdf_setfont().



  pdf_setfont($pdf, $font, 16);

</pre>

<p>Now we&#8217;re almost ready to print our text. We just need to set the position for it to appear, with <tt>pdf_set_text_pos()</tt>. With this function, we specify an X and Y coordinate in points, counting from the lower left corner of the page, which is (0,0). Let&#8217;s print our text near the upper left of the page:

</p>

<pre class="brush: js">  pdf_set_text_pos($pdf, 72, 720);

</pre>

<p>pdf_show() is used to print text at the current position. Its usage is very simple:



</p>

<pre class="brush: js">  pdf_show($pdf, "My First PDF Document");

</pre>

<p>There&#8217;s the text! At last! At this point, if we wanted, we could continue to move around the page, printing more text in various fonts, drawing images, adding new pages, and so forth. But I think we&#8217;ve covered about enough for an intro course. Let&#8217;s close things down.

</p>

<a name="Wrapping_Up"></a><h3> <span class="mw-headline">Wrapping Up</span></h3>

<p>We end the page with the <tt>pdf_end_page()</tt> function:

</p>

<pre class="brush: js">  pdf_end_page($pdf);

</pre>



<p>and then close the object:

</p>

<pre class="brush: js">  pdf_end_document($pdf);

</pre>

<p>The complete PDF document that we&#8217;ve created is now neatly put away in the PHP buffer. In order to display or save it, we need to fetch it from there and do something with it. The <tt>pdf_get_buffer()</tt> function gets the contents of the buffer and puts it into a new variable.

</p>

<pre class="brush: js">  $document = pdf_get_buffer($pdf);

</pre>

<p>Now let&#8217;s create a new PDF &#8212; just kidding. Actually we can&#8217;t use any more PDF functions until we get rid of this object we&#8217;ve just pulled from the buffer. That&#8217;s a safeguard built into the system. We must either send the PDF to the user&#8217;s browser, save it as a file on the server, or delete it from memory. Let&#8217;s do the latter two.

</p><p>To display it, we need to first send HTTP header information, to let the browser know what&#8217;s coming. This is done with the <tt>header()</tt> function. We need to send three headers:Content-Type, Content-Length, and Content-Disposition.



</p><p>First let&#8217;s find the length of the PDF, and give it a filename:

</p>

 <pre class="brush: js"> $length = strlen($document);



  $filename = "myfirstpdf.pdf";

</pre>

<p>Then we output the headers:

</p>

<pre class="brush: js">  header("Content-Type:application/pdf");



  header("Content-Length:" . $length);



  header("Content-Disposition:inline; filename=" . $filename);

</pre>

<p>Now we&#8217;re ready to send the actual document to the browser, with a simple echo() function:

</p>

<pre class="brush: js">  echo($document);

</pre>

<p>All set. Finally, as a last step, we purge the memory to get ready for our next adventure:



</p>

  <pre class="brush: js">unset($document);



  pdf_delete($pdf);

</pre>

<p>Here is a look at all that code assembled in one place:

</p>

<pre class="brush: js">

  &lt;?php



  $pdf = pdf_new();



  pdf_begin_document($pdf);



  pdf_set_info($pdf, "Author", "Paul Adams");



  pdf_begin_page($pdf, (72 * 8.5), (72 * 11));



  $font = pdf_findfont($pdf, "Times-Roman", "host", 0)



  pdf_setfont($pdf, $font, 16);



  pdf_set_text_pos($pdf, 72, 720);



  pdf_show($pdf, "My First PDF Document");



  pdf_end_page($pdf);



  pdf_end_document($pdf);



  $document = pdf_get_buffer($pdf);



  $length = strlen($document);



  $filename = "myfirstpdf.pdf";



  header("Content-Type:application/pdf");



  header("Content-Length:" . $length);



  header("Content-Disposition:inline; filename=" . $filename);



  echo($document);



  unset($document);



  pdf_delete($pdf);



 &nbsp;?&gt;



  </pre>

<p>That&#8217;s kind of a lot of code to print one line of text, it&#8217;s true. But that was just an example. The PDF format wasn&#8217;t invented for displaying single lines (although it does them so nicely!). You can use the framework above to output all kinds of complex PDFs, with logos and diagrams and color and links and whatever. There are simple PHP functions for all that stuff. Just start small and work step-by-step. The PDFlib package includes a very cool demo, called pdfclock, of an analog clock rendered as a dynamic PDF. See where that leads you. Follow your heart.

</p><div id='linker_widget' class='contextly-widget'></div>]]></content:encoded>
            <wfw:commentRss>http://www.webmonkey.com/2010/02/generate_pdfs_dynamically_with_php/feed/</wfw:commentRss>
        <slash:comments>2</slash:comments>

        
    </item>
    
    <item>
        <title>100 Open Source Presentations From OSCon</title>
        <link>http://www.webmonkey.com/2008/08/100_open_source_presentations_from_oscon/</link>
        <comments>http://www.webmonkey.com/2008/08/100_open_source_presentations_from_oscon/#comments</comments>
        <pubDate>Tue, 05 Aug 2008 06:55:30 +0000</pubDate>

                <dc:creator>Adam Duvander</dc:creator>

        <guid isPermaLink="false">http://www.webmonkey.com/blog/100opensourcepresentationsfromoscon</guid>
        		<category><![CDATA[Events]]></category>
		<category><![CDATA[odp]]></category>
		<category><![CDATA[oscon]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[ppt]]></category>
		<category><![CDATA[presentations]]></category>
        <description><![CDATA[Now you can attend OSCon for free. Well, sort of. Book publisher and conference organizer O&#8217;Reilly has shared over 100 presentations from the July conference. Some speakers did not share their presentations (at an open source conference? really?), but it appears most did. Some development topics to check out: Developers vs. Designers (PDF) has great [...]]]></description>

            <content:encoded><![CDATA[<p><!-- wpautop enabled --><img class="blogimg" src="http://howto.wired.com/mediawiki/images/Oscon-header.png" alt="OSCon Open Source Convention" />Now you can attend OSCon for free. Well, sort of. Book publisher and conference organizer O&#8217;Reilly has shared <a href="http://en.oreilly.com/oscon2008/public/schedule/proceedings">over 100 presentations</a> from the July conference. Some speakers did not share their presentations (at an open source conference? really?), but it appears most did.</p>
<p>Some development topics to check out:</p>
<ul>
<li><a href="http://assets.en.oreilly.com/1/event/12/Developers%20vs%20Designers_%20Expanding%20the%20Java%20Platform%20Beyond%20Programmers%20Presentation.pdf">Developers vs. Designers</a> (PDF) has great history, even if you don&#8217;t think Java is the answer.</li>
<li><a href="http://assets.en.oreilly.com/1/event/12/Even%20Faster%20Web%20Sites%20Presentation%202.ppt">Even Faster Web Sites</a> (PPT) gives some great tips to improve the performance of your site.</li>
<li><a href="http://www.nexen.net/images/stories/conferences/oscon.2008.pdf">Hack this app</a> (PDF) is technical, but a mandatory reading for PHP developers.</li>
<li><a href="http://www.mikehillyer.com/wp-content/uploads/2008/07/oscon_2008.pdf">How to be Normal</a> is a tough thing for a geek to achieve. Luckily, Mike Hillyer was talking about databases.</li>
</ul>
<p><strong>You down with ODP?</strong></p>
<p>Most of the presentations are downloadable in some sort of slide format (as opposed to a web page). As I glanced through, I was surprised to see more Powerpoint (PPT) than the open document presentation (ODP) format. Strange, for an OS conference.</p>
<p>My tally shows that 19 supplied the Microsoft format, while 12 were ODP. I&#8217;m sure it&#8217;s a coincidence that Microsoft sponsored the presentation files page. Of course, PPT and ODP were both blown out of the water by <a href="http://www.webmonkey.com/blog/Adobe_s_PDF_Format_Adopted_as_Standard">another open format, PDF</a>. About 70% of the downloadable presentations used Adobe&#8217;s format.</p>
<p><strong>See also:</strong></p>
<ul>
<li><a href="http://www.webmonkey.com/blog/OSCON:_New_York_Times_and_NPR_Discuss_APIs">New York Times&#8217; Derek Gottfrid and NPR&#8217;s Dan Jacobson Discuss APIs</a></li>
<li><a href="http://www.webmonkey.com/blog/Microsoft_Dons_Sheep_Suit__Joins_Open_Source_Foundation">Microsoft Dons Sheep Suit, Joins Open Source Foundation</a></li>
<li><a href="http://www.webmonkey.com/blog/Alpha_Geeks_of_Open-Source_Converge_at_OSCON">Alpha Geeks of Open-Source Converge at OSCON</a></li>
</ul>
<div id='linker_widget' class='contextly-widget'></div>
]]></content:encoded>
            <wfw:commentRss>http://www.webmonkey.com/2008/08/100_open_source_presentations_from_oscon/feed/</wfw:commentRss>
        <slash:comments>0</slash:comments>

        
    </item>
    
    <item>
        <title>Adobe&#8217;s PDF Format Adopted as Standard</title>
        <link>http://www.webmonkey.com/2008/07/adobe_s_pdf_format_adopted_as_standard/</link>
        <comments>http://www.webmonkey.com/2008/07/adobe_s_pdf_format_adopted_as_standard/#comments</comments>
        <pubDate>Thu, 03 Jul 2008 17:38:26 +0000</pubDate>

                <dc:creator>Adam Duvander</dc:creator>

        <guid isPermaLink="false">http://www.webmonkey.com/blog/adobespdfformatadoptedasstandard</guid>
        		<category><![CDATA[Web Basics]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[xps]]></category>
        <description><![CDATA[Adobe&#8217;s PDF format was officially adopted as an open standard this week. The International Organization for Standardization (ISO) announced that PDF is now accessible as ISO 32000-1. The announcement follows Adobe&#8217;s decision to relinquish control of the proprietary format first introduced in 1993. &#8220;By releasing the full PDF specification for ISO standardization, we are reinforcing [...]]]></description>

            <content:encoded><![CDATA[<p><!-- wpautop enabled --><img class="blogimg" src="http://howto.wired.com/mediawiki/images/PDF.png" alt="Adobe PDF icon" width="200" /></p>
<p>Adobe&#8217;s PDF format was officially adopted as an open standard this week. The International Organization for Standardization (ISO) <a href="http://www.iso.org/iso/pressrelease.htm?refid=Ref1141">announced that PDF is now accessible as ISO 32000-1</a>.</p>
<p>The announcement follows Adobe&#8217;s decision to relinquish control of the proprietary format first introduced in 1993. &#8220;By releasing the full PDF specification for ISO standardization, we are reinforcing our commitment to openness,&#8221; Adobe chief technology officer Kevin Lynch said in a press release.</p>
<p>As we noted in January when <a href="http://www.webmonkey.com/blog/Adobe_s_PDF_File_Format_Receives_ISO_Blessing">ISO approved PDF</a>, some see this move from Adobe as a defensive move to stop Microsoft&#8217;s XML Paper Specification. Adobe denies the decision has anything to do with Redmond and instead is intended to answer a call from their users for open formats.</p>
<p>If you&#8217;re a PHP programmer, check out our tutorial on <a href="/2010/02/Generate_PDFs_Dynamically_With_PHP">generating PDFs dynamically</a>.</p>
<p><strong>See also:</strong></p>
<ul>
<li><a href="http://www.webmonkey.com/blog/Linux_Tip%3A_Easily_Create_PDF_Documents">Linux Tip: Easily Create PDF Documents</a></li>
<li><a href="http://www.webmonkey.com/blog/Papers%3A_A_PDF_Browser">Papers: A PDF Browser</a></li>
<li><a href="http://www.webmonkey.com/blog/Adobe_Confirms_PDF_Exploit">Adobe Confirms PDF Exploit</a></li>
</ul>
<div id='linker_widget' class='contextly-widget'></div>
]]></content:encoded>
            <wfw:commentRss>http://www.webmonkey.com/2008/07/adobe_s_pdf_format_adopted_as_standard/feed/</wfw:commentRss>
        <slash:comments>0</slash:comments>

        
    </item>
    </channel>
</rss>
