{"id":29,"date":"2012-02-03T18:55:17","date_gmt":"2012-02-03T18:55:17","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2012\/02\/creating_a_simple_pdf_with_the_zend_framework-shtml\/"},"modified":"2012-02-03T18:55:17","modified_gmt":"2012-02-03T18:55:17","slug":"creating_a_simple_pdf_with_the_zend_framework-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/","title":{"rendered":"Creating A Simple PDF With The Zend Framework"},"content":{"rendered":"<p>\nI&#8217;ve recently been playing with creating PDF documents for a project I&#8217;ve been working on.\n<\/p>\n<p>\nI thought I&#8217;d share some of the knowledge I&#8217;ve gained in the hope it will help others.\n<\/p>\n<p>\nI&#8217;ve been using the excellent Zend Framework as usual, and the Zend_Pdf component in particular. This is a pure PHP implementation of the PDF standard and it doesn&#8217;t require any other libraries to be installed to work.\n<\/p>\n<p>\nA PDF document is text document that has been been designed by Adobe a way to create and share printable documents across multiple platforms. If you open a PDF document in a text editor, you can see the markup for yourself, however it&#8217;s not very user friendly. This is where Zend_Pdf comes to our aid.\n<\/p>\n<p>\nLet&#8217;s create a simple A4 document, and say Hello World!.\n<\/p>\n<div class=\"code\"><code><br \/>\n&lt;?php<br \/>\n\/\/ load the Zend Framework Autoloader.<br \/>\nini_set(\"include_path\",\".\/library\");<br \/>\nrequire_once 'Zend\/Loader\/Autoloader.php';<br \/>\n$loader = Zend_Loader_Autoloader::getInstance();<br \/>\ntry {<br \/>\n\/\/ create a new PDF document.<br \/>\n$pdf = new Zend_Pdf();<br \/>\n\/\/ create the Helvetica font.<br \/>\n$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);<br \/>\n\/\/ create a new A4 sized page.<br \/>\n$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);<br \/>\n\/\/ draw the text Hello World! to position 0,0 with the Helvetica font.<br \/>\n$page->setFont($font, 16)<br \/>\n->drawText('Hello World!',0,0);<br \/>\n\/\/ add the page to the pdf document.<br \/>\n$pdf->pages[] = $page;<br \/>\n\/\/ save the finished PDF to a file<br \/>\n$pdf->save('helloworld.pdf');<br \/>\n\/\/ catch any errors<br \/>\n} catch (Zend_Pdf_Exception $e) {<br \/>\ndie('PDF error: ' . $e->getMessage());<br \/>\n} catch (Exception $e) {<br \/>\ndie ('Error: ' . $e->getMessage());<br \/>\n}<br \/>\n?&gt;<br \/>\n<\/code><\/div>\n<p>\nWhat&#8217;s going on? Well firstly I take advantage of Zend Frameworks autoloader to load in the required components.\n<\/p>\n<p>\nNext I create an empty PDF document. A copy of the Helvetica font is prepared for use. We&#8217;ll need to add pages to this document, so after this we create a new A4 sized page. I&#8217;ve used A4 as that is the common paper size in Europe, but in the US letter size is the standard. This can be changed by using <code>Zend_Pdf_Page::SIZE_LETTER<\/code> instead of <code>Zend_Pdf_Page::SIZE_A4<\/code>.\n<\/p>\n<p>\nNow I have a page, I want to add some text to it. This is easily achieved using the <code>drawText<\/code> method. This takes the text, and an X and Y position to draw it at. We do need to set the font first, let&#8217;s use the Helvetica font we defined earlier, and use size 16. I chose position 0,0 to write the text, this is the bottom left hand side of the page.\n<\/p>\n<p>\nFinally, I add the page to PDF document, and save it.\n<\/p>\n<p>\nIf an error occurs, I catch this with my exception handlers.\n<\/p>\n<p>\nOf course, in reality you&#8217;ll want o do a lot more than just write one line of text. Zend_Pdf allows you to do a lot more, such as adding images, drawing graphics, adding meta data. I&#8217;ll cover this in another post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve recently been playing with creating PDF documents for a project I&#8217;ve been working on. I thought I&#8217;d share some of the knowledge I&#8217;ve gained in the hope it will help others. I&#8217;ve been using the excellent Zend Framework as usual, and the Zend_Pdf component in particular. This is a pure PHP implementation of the &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating A Simple PDF With The Zend Framework&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[50,81],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-dev","tag-php","tag-zend-framework"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating A Simple PDF With The Zend Framework - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating A Simple PDF With The Zend Framework - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve recently been playing with creating PDF documents for a project I&#8217;ve been working on. I thought I&#8217;d share some of the knowledge I&#8217;ve gained in the hope it will help others. I&#8217;ve been using the excellent Zend Framework as usual, and the Zend_Pdf component in particular. This is a pure PHP implementation of the &hellip; Continue reading &quot;Creating A Simple PDF With The Zend Framework&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2012-02-03T18:55:17+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_simple_pdf_with_the_zend_framework-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_simple_pdf_with_the_zend_framework-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Creating A Simple PDF With The Zend Framework\",\"datePublished\":\"2012-02-03T18:55:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_simple_pdf_with_the_zend_framework-shtml\\\/\"},\"wordCount\":374,\"keywords\":[\"PHP\",\"Zend Framework\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_simple_pdf_with_the_zend_framework-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_simple_pdf_with_the_zend_framework-shtml\\\/\",\"name\":\"Creating A Simple PDF With The Zend Framework - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2012-02-03T18:55:17+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_simple_pdf_with_the_zend_framework-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_simple_pdf_with_the_zend_framework-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_simple_pdf_with_the_zend_framework-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating A Simple PDF With The Zend Framework\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating A Simple PDF With The Zend Framework - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Creating A Simple PDF With The Zend Framework - Robert Price","og_description":"I&#8217;ve recently been playing with creating PDF documents for a project I&#8217;ve been working on. I thought I&#8217;d share some of the knowledge I&#8217;ve gained in the hope it will help others. I&#8217;ve been using the excellent Zend Framework as usual, and the Zend_Pdf component in particular. This is a pure PHP implementation of the &hellip; Continue reading \"Creating A Simple PDF With The Zend Framework\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/","og_site_name":"Robert Price","article_published_time":"2012-02-03T18:55:17+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Creating A Simple PDF With The Zend Framework","datePublished":"2012-02-03T18:55:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/"},"wordCount":374,"keywords":["PHP","Zend Framework"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/","name":"Creating A Simple PDF With The Zend Framework - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2012-02-03T18:55:17+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_simple_pdf_with_the_zend_framework-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Creating A Simple PDF With The Zend Framework"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}