{"id":602,"date":"2010-06-22T09:27:40","date_gmt":"2010-06-22T15:27:40","guid":{"rendered":"http:\/\/www.curtisgibby.com\/blog\/?p=602"},"modified":"2011-02-28T07:29:33","modified_gmt":"2011-02-28T14:29:33","slug":"maximizing-font-size-in-fpdf","status":"publish","type":"post","link":"https:\/\/www.curtisgibby.com\/blog\/2010\/06\/maximizing-font-size-in-fpdf\/","title":{"rendered":"Maximizing font size in FPDF"},"content":{"rendered":"<p>At work, I use the excellent <a href=\"http:\/\/www.fpdf.org\/\">FPDF library<\/a> to write PDF files.\u00a0 But I couldn&#8217;t find any way to automatically make the text as large as possible to fit into a given space.\u00a0 So I wrote the following function to stick into the FPDF class.\u00a0 It&#8217;ll take a given text string and a width, incrementally set the font size higher and higher until it over-runs the space, then gives you the right size to just fit into the width.<!--more--><\/p>\n<pre class='brush:php'>\/**\r\n* Function to maximize the text size based on a given text string and width\r\n*\r\n* returns the calculated text size\r\n*\/\r\nfunction SetMaxFontSize($text, $maxWidth, $step = 1, $fontMin = 1) {\r\n\r\n\t\/\/ prevent stupidity\r\n\tif ($maxWidth &lt; 0) {\r\n\t\t$maxWidth = 1;\r\n\t}\r\n\r\n\tif ($step &lt; 0) {\r\n\t\t$step = 1;\r\n\t}\r\n\r\n\tif ($fontMin &lt; 0) {\r\n\t\t$fontMin = 1;\r\n\t}\r\n\r\n\t$fontSize = $fontMin;\r\n\t$text_width = 1;\r\n\twhile($text_width &lt; $maxWidth) {\r\n\r\n\t\t$this-&gt;SetFontSize($fontSize);\r\n\t\t$text_width=$this-&gt;GetStringWidth($text);\r\n\t\t$fontSize += $step;  \/\/ larger step is faster, smaller step is more accurate\r\n\t}\r\n\r\n\t$fontSize -= ($step * 2); \/\/ this is the biggest you can get without going over\r\n\t$this-&gt;SetFontSize($fontSize);\r\n\r\n\treturn $fontSize;\r\n}\r\n<\/pre>\n<p>Here&#8217;s how I&#8217;m calling it in my PDF-generating script.<\/p>\n<pre class='brush:php'>\t\/\/ you have to set the font first (at least once someplace in the script), or else FPDF throws an error\r\n\t$pdf-&gt;SetFont($fontName,'',1);\r\n\t\/\/ make the text as big as possible without overflowing\r\n\t$pdf-&gt;SetMaxFontSize($text, BOX_WIDTH, 2, 1);\r\n<\/pre>\n<p>The third and fourth parameters in the function are the step size (larger step is faster, smaller step is more accurate) and the beginning font size value.  They&#8217;re both optional.<\/p>\n<p>Hope this helps someone!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At work, I use the excellent FPDF library to write PDF files.\u00a0 But I couldn&#8217;t find any way to automatically make the text as large as possible to fit into a given space.\u00a0 So I wrote the following function to stick into the FPDF class.\u00a0 It&#8217;ll take a given text string and a width, incrementally&hellip; <a class=\"more-link\" href=\"https:\/\/www.curtisgibby.com\/blog\/2010\/06\/maximizing-font-size-in-fpdf\/\">Continue reading <span class=\"screen-reader-text\">Maximizing font size in FPDF<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,17],"tags":[115,16],"class_list":["post-602","post","type-post","status-publish","format-standard","hentry","category-programming","category-work","tag-fdpf","tag-php","entry"],"_links":{"self":[{"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/posts\/602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/comments?post=602"}],"version-history":[{"count":4,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":699,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/posts\/602\/revisions\/699"}],"wp:attachment":[{"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/media?parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/categories?post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/tags?post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}