{
  "slug": "Make-Pdf-in-Cakephp-with-UTF-8-for-print-in-label-printer-f6d172423eff",
  "title": "Make Pdf in Cakephp with UTF-8 for print in label printer",
  "subtitle": "Cakephp is one of the PHP frameworks that famous for agile. in this article demonstrate step by step make Pdf file with Tcpdf library in…",
  "excerpt": "Cakephp is one of the PHP frameworks that famous for agile. in this article demonstrate step by step make Pdf file with Tcpdf library in…",
  "date": "2020-09-17",
  "tags": [
    "PHP"
  ],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/make-pdf-in-cakephp-with-utf-8-for-print-in-label-printer-f6d172423eff",
  "hero": "https://cdn-images-1.medium.com/max/800/1*lo2M51Dgo74vWEyVTWWa0Q.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Make Pdf in Cakephp with UTF-8 for print in label printer"
    },
    {
      "type": "paragraph",
      "html": "Cakephp is one of the PHP frameworks that famous for agile. in this article demonstrate step by step make Pdf file with Tcpdf library in Cakephp for label print invoice of the customer with UTF-8 characters and using label print size for pdf."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*lo2M51Dgo74vWEyVTWWa0Q.png",
      "alt": "Make Pdf in Cakephp with UTF-8 for print in label printer",
      "caption": "",
      "width": 1533,
      "height": 609
    },
    {
      "type": "paragraph",
      "html": "at this moment I realize that CakePHP 4 has come and I still using cakephp3&nbsp;. one the big nightmare of the developer is changing everything in any minute and I should update my system immediately."
    },
    {
      "type": "paragraph",
      "html": "Tcpdf is powerfull library of PHP can make easily pdf and it’s open source."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*MkWFgrEtPN3hkB2SdvZccw.png",
      "alt": "Make Pdf in Cakephp with UTF-8 for print in label printer",
      "caption": "",
      "width": 1532,
      "height": 711
    },
    {
      "type": "paragraph",
      "html": "tcpdf has composer for cakephp4, with command below can install it easly."
    },
    {
      "type": "paragraph",
      "html": "composer require friendsofcake/cakepdf"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*VvaZ2mu2ADFgE3ow4WIV7Q.png",
      "alt": "Make Pdf in Cakephp with UTF-8 for print in label printer",
      "caption": "",
      "width": 727,
      "height": 246
    },
    {
      "type": "paragraph",
      "html": "unfortunately, composer is not compatible with Cakephp version 3 so let’s do it in another way."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "download Tcpdf"
    },
    {
      "type": "paragraph",
      "html": "it’s very easy, just going to github of Tcpdf and git clone codes."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*Q_Rj5eK49oI4So8316h8Bw.png",
      "alt": "Make Pdf in Cakephp with UTF-8 for print in label printer",
      "caption": "",
      "width": 1525,
      "height": 753
    },
    {
      "type": "paragraph",
      "html": "put download folder in Vendor folder of cakephp.I make folder /vendor/TCPDF and put file in there."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "layout"
    },
    {
      "type": "paragraph",
      "html": "as you know very page has layout format. for print PDF file make file in /src/Template/Layout/pdf.ctp"
    },
    {
      "type": "paragraph",
      "html": "and put code below&nbsp;:"
    },
    {
      "type": "paragraph",
      "html": "&lt;?php<br>header(“Content-type: application/pdf”);<br>echo $this-&gt;fetch(‘content’);"
    },
    {
      "type": "paragraph",
      "html": "?&gt;"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Controller"
    },
    {
      "type": "paragraph",
      "html": "in controller using TCPDF library by calling vendor class."
    },
    {
      "type": "paragraph",
      "html": "require_once(ROOT&nbsp;. DS&nbsp;. ‘vendor’&nbsp;. DS&nbsp;. ‘TCPDF’&nbsp;. DS&nbsp;. ‘tcpdf.php’);"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Declare page Size"
    },
    {
      "type": "paragraph",
      "html": "define instant of Tcpdf class and setup page size 350 X 250&nbsp;. using l for landscape pdf page. using UTF-8 for using Persian words."
    },
    {
      "type": "paragraph",
      "html": "$width = 350;<br>&nbsp;$height = 200;<br>&nbsp;$pageLayout = array( $height&nbsp;, $width); // or array($height, $width)&nbsp;<br>&nbsp;$pdf = new \\TCPDF(‘l’, ‘pt’, $pageLayout, true, ‘UTF-8’, false);"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "remove header and footer"
    },
    {
      "type": "paragraph",
      "html": "I want to make some kind of invoice that print on package so remove the footer and header of pdf."
    },
    {
      "type": "paragraph",
      "html": "$pdf-&gt;setPrintHeader(false);<br>&nbsp;$pdf-&gt;setPrintFooter(false);"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Set some language dependent data:"
    },
    {
      "type": "paragraph",
      "html": "$lg = Array();<br>&nbsp;$lg[‘a_meta_charset’] = ‘UTF-8’;<br>&nbsp;$lg[‘a_meta_dir’] = ‘rtl’;<br>&nbsp;$lg[‘a_meta_language’] = ‘fa’;<br>&nbsp;$lg[‘w_page’] = ‘page’;"
    },
    {
      "type": "paragraph",
      "html": "$pdf-&gt;setLanguageArray($lg);"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Set a font:"
    },
    {
      "type": "paragraph",
      "html": "// set font<br>&nbsp;$pdf-&gt;SetFont(‘dejavusans’, ‘’, 12);<br>&nbsp;$pdf-&gt;SetAutoPageBreak(true, 0);"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "add a page:"
    },
    {
      "type": "paragraph",
      "html": "$pdf-&gt;AddPage();"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "write Text:"
    },
    {
      "type": "paragraph",
      "html": "$text = “گیرنده&nbsp;:”&nbsp;. $variable[‘user’][‘name’];"
    },
    {
      "type": "paragraph",
      "html": "// Persian and English content<br>&nbsp;$pdf-&gt;WriteHTML($text, true, 0, true, 0);"
    },
    {
      "type": "paragraph",
      "html": "// set LTR direction for english translation<br>&nbsp;$pdf-&gt;setRTL(true);"
    },
    {
      "type": "paragraph",
      "html": "$pdf-&gt;SetFontSize(10);"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "next Line:"
    },
    {
      "type": "paragraph",
      "html": "$pdf-&gt;Ln();"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Make pdf file:"
    },
    {
      "type": "paragraph",
      "html": "$pdf-&gt;Output(‘test.pdf’, ‘I’);"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*T7BGTg96iZBrRFBYd1sqaA.png",
      "alt": "Make Pdf in Cakephp with UTF-8 for print in label printer",
      "caption": "",
      "width": 1536,
      "height": 812
    }
  ]
}
