{"id":28,"date":"2012-02-10T17:46:31","date_gmt":"2012-02-10T17:46:31","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2012\/02\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/"},"modified":"2012-02-10T17:46:31","modified_gmt":"2012-02-10T17:46:31","slug":"drawing_a_image_in_a_pdf_with_zend_framework-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/","title":{"rendered":"Drawing A Image In A PDF With Zend Framework"},"content":{"rendered":"<p>\nI recently wrote about <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/archive\/2012\/2\/Creating_A_Simple_PDF_With_The_Zend_Framework.shtml\">Creating A Simple PDF With The Zend Framework<\/a>, and so I thought I&#8217;d take a quick look at <a href=\"http:\/\/framework.zend.com\/manual\/en\/zend.pdf.drawing.html\">Zend_PDF&#8217;s Drawing Methods<\/a>.\n<\/p>\n<p>\nAs you may know I work for a company called <a href=\"http:\/\/www.bauermedia.co.uk\">Bauer<\/a> and we have a very simple logo. It&#8217;s basically a blue square, with a gray triangle in the top right hand corner and the name BAUER in white at the bottom. This sounds like an ideal image to try to recreate using Zend_PDF.\n<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/robblog\/images\/bauer_logo_original.png\" width=\"200\" height=\"200\" alt=\"Bauer Media Logo\" class=\"blogimage\" \/>\n<\/p>\n<p>\nTake a look at my previous article on PDF&#8217;s and Zend Framework for an explanation of setting up a document and page to draw on. I will assume this page has been setup and is available in the <var>$page<\/var> variable.\n<\/p>\n<p>\nFirstly we need to prepare a font for use. I don&#8217;t know the font Bauer use, so I&#8217;ll just use Helvetica Bold in this example.\n<\/p>\n<pre class=\"lang:php decode:true \" >\n$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);\n<\/pre>\n<p>\nThe background is a blue square, so I need to set the fill colour to the shade of blue Bauer use, then draw the square using this colour.\n<\/p>\n<pre class=\"lang:php decode:true \" >$page-&gt;setFillColor(new Zend_Pdf_Color_Html('#014495'))\n     -&gt;drawRectangle(0, 0, 200, 200, Zend_Pdf_Page::SHAPE_DRAW_FILL);\n<\/pre>\n<p>\nAs you can see, I&#8217;ve decided to make the logo 200&#215;200 in size.\n<\/p>\n<p>\nNow it&#8217;s time to write BAUER at the bottom in white using the font we prepared earlier.\n<\/p>\n<pre class=\"lang:php decode:true \" >$page-&gt;setFillColor(new Zend_Pdf_Color_Html('#ffffff'))\n     -&gt;setFont($font, 52)\n     -&gt;drawText('BAUER', 7, 10);\n<\/pre>\n<p>\nFirstly I set the fill colour to white. Next I select the font and set it&#8217;s size. Finally I write the word BAUER at co-ordinates 7,10, which happens to be at the bottom of the blue square.\n<\/p>\n<p>\nFinally it&#8217;s time to draw the gray triangle in the top right corner of the blue square.\n<\/p>\n<pre class=\"lang:php decode:true \" >$page-&gt;setFillColor(new Zend_Pdf_Color_Html('#dddddd'))\n     -&gt;drawPolygon(array(70, 190, 190),\n                   array(190, 190, 70),\n                   Zend_Pdf_Page::SHAPE_DRAW_FILL);\n<\/pre>\n<p>\nAs before, I set the fill colour, this time to gray. Now I use the <code>drawPolygon<\/code> method to draw the triangle. As you can see there are two arrays there, the first are the x co-ordinates of the triangle, the second are the y co-ordinates of the triangle. The fill method is the last parameter, it&#8217;s the same as the one we used when drawing the blue square.\n<\/p>\n<p>\nWe should now have our Bauer logo on the page!\n<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/robblog\/images\/bauer_logo_generated.png\" width=\"200\" height=\"200\" alt=\"generated copy of the Bauer Media logo\" class=\"blogimage\" \/>\n<\/p>\n<p>\nI don&#8217;t think that Bauer will be sacking their design team in a hurry, but it&#8217;s quite a close approximation of the logo.\n<\/p>\n<p>\nHere&#8217;s a copy of the finished code.\n<\/p>\n<pre class=\"lang:php decode:true \" >\/\/ load the Zend Framework Autoloader.\nini_set(\"include_path\",\".\/library\");\nrequire_once 'Zend\/Loader\/Autoloader.php';\n\n$loader = Zend_Loader_Autoloader::getInstance();\n\ntry {\n  \/\/ create a new PDF document.\n  $pdf = new Zend_Pdf();\n\n  \/\/ create a new A4 sized page.\n  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);\n\n  \/\/ prepare the Helvetica Bold font.\n  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);\n\n  \/\/ draw the blue square background.\n  $page-&gt;setFillColor(new Zend_Pdf_Color_Html('#014495'))\n       -&gt;drawRectangle(0, 0, 200, 200, Zend_Pdf_Page::SHAPE_DRAW_FILL);\n\n  \/\/ draw the white BAUER text at the bottom of the square.\n  $page-&gt;setFillColor(new Zend_Pdf_Color_Html('#ffffff'))\n       -&gt;setFont($font, 52)\n       -&gt;drawText('BAUER', 7, 10);\n\n  \/\/ draw the grey triangle in the top right corner of the square.\n  $page-&gt;setFillColor(new Zend_Pdf_Color_Html('#dddddd'))\n       -&gt;drawPolygon(array(70, 190, 190),\n                     array(190, 190, 70),\n                     Zend_Pdf_Page::SHAPE_DRAW_FILL);\n\n  \/\/ add the page to the pdf document.\n  $pdf-&gt;pages[] = $page;\n\n  \/\/ save the finished PDF to a file\n  $pdf-&gt;save('bauer.pdf');\n\n\/\/ catch any errors\n} catch (Zend_Pdf_Exception $e) {\n  die('PDF error: ' . $e-&gt;getMessage());\n} catch (Exception $e) {\n  die ('Error: ' . $e-&gt;getMessage());\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I recently wrote about Creating A Simple PDF With The Zend Framework, and so I thought I&#8217;d take a quick look at Zend_PDF&#8217;s Drawing Methods. As you may know I work for a company called Bauer and we have a very simple logo. It&#8217;s basically a blue square, with a gray triangle in the top &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Drawing A Image In A PDF With 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-28","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>Drawing A Image In A PDF With 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\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Drawing A Image In A PDF With Zend Framework - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I recently wrote about Creating A Simple PDF With The Zend Framework, and so I thought I&#8217;d take a quick look at Zend_PDF&#8217;s Drawing Methods. As you may know I work for a company called Bauer and we have a very simple logo. It&#8217;s basically a blue square, with a gray triangle in the top &hellip; Continue reading &quot;Drawing A Image In A PDF With Zend Framework&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2012-02-10T17:46:31+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=\"3 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\\\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Drawing A Image In A PDF With Zend Framework\",\"datePublished\":\"2012-02-10T17:46:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\\\/\"},\"wordCount\":387,\"keywords\":[\"PHP\",\"Zend Framework\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\\\/\",\"name\":\"Drawing A Image In A PDF With Zend Framework - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2012-02-10T17:46:31+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Drawing A Image In A PDF With 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":"Drawing A Image In A PDF With 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\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Drawing A Image In A PDF With Zend Framework - Robert Price","og_description":"I recently wrote about Creating A Simple PDF With The Zend Framework, and so I thought I&#8217;d take a quick look at Zend_PDF&#8217;s Drawing Methods. As you may know I work for a company called Bauer and we have a very simple logo. It&#8217;s basically a blue square, with a gray triangle in the top &hellip; Continue reading \"Drawing A Image In A PDF With Zend Framework\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/","og_site_name":"Robert Price","article_published_time":"2012-02-10T17:46:31+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Drawing A Image In A PDF With Zend Framework","datePublished":"2012-02-10T17:46:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/"},"wordCount":387,"keywords":["PHP","Zend Framework"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/","name":"Drawing A Image In A PDF With Zend Framework - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2012-02-10T17:46:31+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_image_in_a_pdf_with_zend_framework-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Drawing A Image In A PDF With 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\/28","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=28"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}