{"id":1277,"date":"2020-03-20T11:27:10","date_gmt":"2020-03-20T04:27:10","guid":{"rendered":"https:\/\/www.bagi2info.com\/?p=1277"},"modified":"2022-09-11T14:02:41","modified_gmt":"2022-09-11T07:02:41","slug":"php-mengenerate-certificate-dengan-library-mpdf","status":"publish","type":"post","link":"https:\/\/www.bagi2info.com\/en\/php-joomla-generate-certificates-with-the-mpdf-library\/","title":{"rendered":"PHP Joomla: Generate certificates with the mpdf library"},"content":{"rendered":"\r\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2020\/03\/template-image.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2020\/03\/template-image-1024x745.jpg\" alt=\"certificate background Image\" class=\"wp-image-1279\" width=\"512\" height=\"373\" srcset=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2020\/03\/template-image-1024x745.jpg 1024w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2020\/03\/template-image-300x218.jpg 300w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2020\/03\/template-image-768x559.jpg 768w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2020\/03\/template-image.jpg 1100w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\r\n\r\n\r\n<p>The company I work for happens to have to issue various certificates to many users, it would be very tiring if I had to input user names one by one. <\/p>\r\n\r\n\r\n\r\n<p>By generating the certificate directly through programming, we can send the certificate directly to the user&#8217;s email, without having to print and share <\/p>\r\n\r\n\r\n\r\n<p>Here is an example of a script that generates a certificate:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php line-numbers\">&lt;?php\r\n\r\nerror_reporting(E_ALL);\r\n\r\ndefine('_JEXEC', 1);\r\ndefine('JPATH_BASE', dirname(__FILE__));   \/\/ should point to joomla root\r\ndefine('DS', DIRECTORY_SEPARATOR);\r\ndefined('_JEXEC') or die();\r\n\r\nrequire_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');\r\nrequire_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');\r\n\r\n\/*To use Joomla's database classs*\/\r\n\/\/ gunakan perintah dibawah untuk joomla ver 3.6 kebawah\r\n\/\/ disable jika versi di atas 3.6\r\nrequire_once(JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');\r\n\r\n\/\/ gunakan perintah dibawah untuk joomla ver 3.6 ke atas\r\n\/\/ enable jika versi di atas 3.6\r\n\/\/require_once (JPATH_BASE.DS.'libraries'.DS.'joomla'.DS.'platform.php');\r\n\r\n$mainframe = JFactory::getApplication('site');\r\n$mainframe-&gt;initialise();\r\n\r\n$user = JFactory::getUser(); \/\/ get user info\r\n\r\njimport('joomla.database.table');\r\njimport('joomla.database.table.table');\r\n\r\ninclude(\"libraries\/mpdf\/mpdf.php\");\r\n\r\n$db = JFactory::getDBO();\r\n\r\n$sql = \"SELECT nama, jurusan, ipk\r\n\t\t\tFROM mahasiswa \";\r\n\r\n$db-&gt;setQuery($sql);\r\n\r\necho $sql;\r\n\r\n$result = $db-&gt;query();\r\n$num_rows = $db-&gt;getNumRows();\r\n\r\necho \"Numrows : \" . $num_rows;\r\n\r\n\/\/ check result\r\nif ($num_rows &gt; 0) {\r\n\r\n    $result = $db-&gt;loadAssocList();\r\n\r\n    foreach ($result as $row) {\r\n\r\n        $mpdf = new mPDF('', 'A4', 0, 0, 0, 0, 0, 0);\r\n        $mpdf-&gt;setHeader();    \/\/ Clear headers before adding page\r\n        $mpdf-&gt;AddPage('L', 0, 0, 0, 0, 0, 0);\r\n        $mpdf-&gt;ignore_invalid_utf8 = true;\r\n\r\n        $name = $row[\"nama\"];\r\n        $ipk = $row[\"ipk\"];\r\n        $images = \"template-image.jpg\";\r\n\r\n        $newtext = wordwrap($name, 30, \"&lt;br&gt;\");\r\n        $html = '\t\r\n\t\t\t&lt;style&gt;\r\n\t\t\t\tbody { \r\n\t\t\t\t\tfont-family: Arial;\r\n\t\t\t\t\tbackground-image: url(' . JURI::base() . 'images\/' . $images . ');\r\n\t\t\t\t\tcolor:#1F497D;}\t\r\n\t\t\t\th1 {font-family: Arial;font-size:40px;}\r\n\t\t\t\th2 {font-family: Arial;font-size:24px;}\r\n\t\t\t\t.myfixed {\t\t\t\t\t\r\n\t\t\t\t\tposition: absolute;\r\n\t\t\t\t\tfont-family: Arial;font-size:40px;\r\n\t\t\t\t\toverflow: visible;\r\n\t\t\t\t\tleft: 0;\r\n\t\t\t\t\tright: 0;\t\t\t\t\t\r\n\t\t\t\t\ttop: 53mm;\r\n\t\t\t\t\twidth: 250mm;   \/* you must specify a width *\/\r\n\t\t\t\t\tmargin-top: auto;\r\n\t\t\t\t\tmargin-bottom: auto;\r\n\t\t\t\t\tmargin-left: auto;\r\n\t\t\t\t\tmargin-right: auto;\r\n\t\t\t\t\tfont-weight:bold;\r\n\t\t\t\t}\r\n\t\t\t\t.bpmTopicC td, .bpmTopicC td p { text-align: center; }\r\n\t\t\t\t.bpmTopicC {\r\n\t\t\t\t\twidth:96%; \r\n\t\t\t\t\t\/*background-color: #e3ece4;*\/\r\n\t\t\t\t\tposition: absolute;\r\n\t\t\t\t\tfont-family: Arial;\r\n\t\t\t\t\tfont-size:40px;\r\n\t\t\t\t\toverflow: visible;\r\n\t\t\t\t\tleft: 0;\r\n\t\t\t\t\tright: 0;\r\n\t\t\t\t\ttop: 52;\r\n\t\t\t\t\twidth: 250mm;   \/* you must specify a width *\/\r\n\t\t\t\t\tmargin-top: auto;\r\n\t\t\t\t\tmargin-bottom: auto;\r\n\t\t\t\t\tmargin-left: auto;\r\n\t\t\t\t\tmargin-right: auto;\r\n\t\t\t\t\tfont-weight:bold;\r\n\t\t\t\t}\r\n\t\t\t&lt;\/style&gt;&lt;\/pre&gt;\r\n\r\n    &lt;div class=\"myfixed\"&gt;\r\n    &lt;table class=\"bpmTopicC\"&gt;\r\n    &lt;tbody&gt;\r\n    &lt;tr&gt;\r\n    &lt;td&gt;' . $newtext . '&lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n    &lt;\/tbody&gt;\r\n    &lt;\/table&gt;\r\n    &lt;\/div&gt;';\r\n\r\n        $mpdf-&gt;WriteHTML($html);\r\n        $mpdf-&gt;WriteFixedPosHTML('&lt;div style=\"font-weight: bold;\"&gt;' . $ipk . \"&lt;\/div&gt;\", 64, 74, 50, 100, 'auto');\r\n\r\n        $filename_replaced = str_replace('\/', '', $name);\r\n\r\n        $mpdf-&gt;Output(\"files\/$filename_replaced.pdf\", \"F\");\r\n\r\n        \/\/$mpdf-&gt;Output(\"files\/$name\".\".pdf\",\"I\");\r\n\r\n    }\r\n} else {\r\n    JFactory::getApplication()-&gt;enqueueMessage('No record found for this course', 'error');\r\n    \/\/header('Location: '.$_SERVER['REQUEST_URI']);\r\n    JError::raiseWarning(100, 'Warning');\r\n    \/\/header('Location: ' . $_SERVER[\"HTTP_REFERER\"] );\r\n    JError::raiseWarning(100, 'Warning');\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Requirements:<br>1. Latest Joomla <br>2. The MPDF library is installed in the library folder <br>3. Student table<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>The company I work for happens to have to issue various certificates to many users, it would be very tiring if I had to input user names one by one. By generating the certificate&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1279,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[11,71,82,62,7],"class_list":["post-1277","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-komputer","tag-database","tag-joomla","tag-mysql","tag-php","tag-sql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/1277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/comments?post=1277"}],"version-history":[{"count":0,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/1277\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/media\/1279"}],"wp:attachment":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/media?parent=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/categories?post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/tags?post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}