title: Prestashop 1.6 - create pdf (tcpdf) and send it as email attachment
body: <p>Using Prestashop 1.6 </p>
  
  <p>I have the following code to generate PDF</p>
  
  <pre><code>&lt;?php
  //============================================================+
  // File name   : example_001.php
  // Begin       : 2008-03-04
  // Last Update : 2013-05-14
  //
  // Description : Example 001 for TCPDF class
  //               Default Header and Footer
  //
  // Author: Nicola Asuni
  //
  // (c) Copyright:
  //               Nicola Asuni
  //               Tecnick.com LTD
  //               www.tecnick.com
  //               info@tecnick.com
  //============================================================+
  
  /**
   * Creates an example PDF TEST document using TCPDF
   * @package com.tecnick.tcpdf
   * @abstract TCPDF - Example: Default Header and Footer
   * @author Nicola Asuni
   * @since 2008-03-04
   */
  
   require_once(dirname(__FILE__).'/../config/config.inc.php');
   require_once(dirname(__FILE__).'/../init.php');
  
  
  // Include the main TCPDF library (search for installation path).
  // require_once('tcpdf_include.php');
  require_once(dirname(__FILE__).'/../tools/tcpdf/config/tcpdf_config.php');
  require_once(dirname(__FILE__).'/../tools/tcpdf/tcpdf.php');
  
  
  // create new PDF document
  $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  
  // set document information
  $pdf-&gt;SetCreator(PDF_CREATOR);
  $pdf-&gt;SetAuthor('Nicola Asuni');
  $pdf-&gt;SetTitle('TCPDF Example 027');
  $pdf-&gt;SetSubject('TCPDF Tutorial');
  $pdf-&gt;SetKeywords('TCPDF, PDF, example, test, guide');
  
  // set default header data
  // $pdf-&gt;SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 027', PDF_HEADER_STRING);
  
  // set header and footer fonts
  $pdf-&gt;setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  $pdf-&gt;setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  
  // set default monospaced font
  $pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  
  // set margins
  $pdf-&gt;SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  $pdf-&gt;SetHeaderMargin(PDF_MARGIN_HEADER);
  $pdf-&gt;SetFooterMargin(PDF_MARGIN_FOOTER);
  
  // set auto page breaks
  $pdf-&gt;SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  
  // set image scale factor
  $pdf-&gt;setImageScale(PDF_IMAGE_SCALE_RATIO);
  
  // set some language-dependent strings (optional)
  if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
      require_once(dirname(__FILE__).'/lang/eng.php');
      $pdf-&gt;setLanguageArray($l);
  }
  
  // ---------------------------------------------------------
  
  // set a barcode on the page footer
  $pdf-&gt;setBarcode(date('Y-m-d H:i:s'));
  
  // set font
  $pdf-&gt;SetFont('helvetica', '', 11);
  
  // add a page
  $pdf-&gt;AddPage();
  
  // print a message
  $txt = "You can also export 1D barcodes in other formats (PNG, SVG, HTML). Check the examples inside the barcodes directory.\n";
  $pdf-&gt;MultiCell(70, 50, $txt, 0, 'J', false, 1, 125, 30, true, 0, false, true, 0, 'T', false);
  $pdf-&gt;SetY(30);
  
  // -----------------------------------------------------------------------------
  
  $pdf-&gt;SetFont('helvetica', '', 10);
  
  // define barcode style
  $style = array(
      'position' =&gt; '',
      'align' =&gt; 'C',
      'stretch' =&gt; false,
      'fitwidth' =&gt; true,
      'cellfitalign' =&gt; '',
      'border' =&gt; true,
      'hpadding' =&gt; 'auto',
      'vpadding' =&gt; 'auto',
      'fgcolor' =&gt; array(0,0,0),
      'bgcolor' =&gt; false, //array(255,255,255),
      'text' =&gt; true,
      'font' =&gt; 'helvetica',
      'fontsize' =&gt; 8,
      'stretchtext' =&gt; 4
  );
  
  // PRINT VARIOUS 1D BARCODES
  
  // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  $pdf-&gt;Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
  $pdf-&gt;write1DBarcode('CODE 39', 'C39', '', '', '', 18, 0.4, $style, 'N');
  
  // ---------------------------------------------------------
  
  //Close and output PDF document
  $pdf-&gt;Output('example_027.pdf', 'I');
  </code></pre>
  
  <p>I have the following code to send email:</p>
  
  <p>
  
  <pre><code>require_once(dirname(__FILE__).'/../config/config.inc.php');
  require_once(dirname(__FILE__).'/../init.php');
  
  include_once(_PS_SWIFT_DIR_.'Swift.php');
  include_once(_PS_SWIFT_DIR_.'Swift/Connection/SMTP.php');
  include_once(_PS_SWIFT_DIR_.'Swift/Connection/NativeMail.php');
  include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php');
  
  $id_shop = 1;
  
  if (!$id_shop) {
      $id_shop = Context::getContext()-&gt;shop-&gt;id;
  }
  
  $configuration = Configuration::getMultiple(array(
      'PS_SHOP_EMAIL',
      'PS_MAIL_METHOD',
      'PS_MAIL_SERVER',
      'PS_MAIL_USER',
      'PS_MAIL_PASSWD',
      'PS_SHOP_NAME',
      'PS_MAIL_SMTP_ENCRYPTION',
      'PS_MAIL_SMTP_PORT',
      'PS_MAIL_TYPE'
  ), null, null, $id_shop);
  
  $from = $configuration['PS_SHOP_EMAIL'];
  $from_name = '';
  $connection = new Swift_Connection_SMTP(
      $configuration['PS_MAIL_SERVER'],
      $configuration['PS_MAIL_SMTP_PORT'],
      $configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl' ? Swift_Connection_SMTP::ENC_SSL : (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls' ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF))
  );
  $connection-&gt;setTimeout(4);
  
  if (!$connection) {
      echo false;die;
  }
  if (!empty($configuration['PS_MAIL_USER'])) {
      $connection-&gt;setUsername($configuration['PS_MAIL_USER']);
  }
  if (!empty($configuration['PS_MAIL_PASSWD'])) {
      $connection-&gt;setPassword($configuration['PS_MAIL_PASSWD']);
  }
  
  $swift = new Swift($connection, Configuration::get('PS_MAIL_DOMAIN', null, null, $id_shop));
  
  $subject = '['.Configuration::get('PS_SHOP_NAME', null, null, $id_shop).'] Tracking barcode';
  $message = new Swift_Message($subject);
  
  $message-&gt;setCharset('utf-8');
  
  $message-&gt;headers-&gt;setEncoding('Q');
  
  $to_list = new Swift_RecipientList();
  
  $to_list-&gt;addTo('tst@gmail.com', 'tst');
  $send = $swift-&gt;send($message, $to_list, new Swift_Address($from, $from_name));
  $swift-&gt;disconnect();
  </code></pre>
  
  <p>Now I try to create a script that generate PDF and send it as email attachment:</p>
  
  <p>
  
  <pre><code>/**
   * Creates an example PDF TEST document using TCPDF
   * @package com.tecnick.tcpdf
   * @abstract TCPDF - Example: Default Header and Footer
   * @author Nicola Asuni
   * @since 2008-03-04
   */
  
   require_once(dirname(__FILE__).'/../config/config.inc.php');
   require_once(dirname(__FILE__).'/../init.php');
  
  
  // Include the main TCPDF library (search for installation path).
  // require_once('tcpdf_include.php');
  require_once(dirname(__FILE__).'/../tools/tcpdf/config/tcpdf_config.php');
  require_once(dirname(__FILE__).'/../tools/tcpdf/tcpdf.php');
  
  
  // create new PDF document
  $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  
  // set document information
  $pdf-&gt;SetCreator(PDF_CREATOR);
  $pdf-&gt;SetAuthor('Nicola Asuni');
  $pdf-&gt;SetTitle('TCPDF Example 027');
  $pdf-&gt;SetSubject('TCPDF Tutorial');
  $pdf-&gt;SetKeywords('TCPDF, PDF, example, test, guide');
  
  // set default header data
  // $pdf-&gt;SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 027', PDF_HEADER_STRING);
  
  // set header and footer fonts
  $pdf-&gt;setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  $pdf-&gt;setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  
  // set default monospaced font
  $pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  
  // set margins
  $pdf-&gt;SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  $pdf-&gt;SetHeaderMargin(PDF_MARGIN_HEADER);
  $pdf-&gt;SetFooterMargin(PDF_MARGIN_FOOTER);
  
  // set auto page breaks
  $pdf-&gt;SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  
  // set image scale factor
  $pdf-&gt;setImageScale(PDF_IMAGE_SCALE_RATIO);
  
  // set some language-dependent strings (optional)
  if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
      require_once(dirname(__FILE__).'/lang/eng.php');
      $pdf-&gt;setLanguageArray($l);
  }
  
  // ---------------------------------------------------------
  
  // set a barcode on the page footer
  $pdf-&gt;setBarcode(date('Y-m-d H:i:s'));
  
  // set font
  $pdf-&gt;SetFont('helvetica', '', 11);
  
  // add a page
  $pdf-&gt;AddPage();
  
  // print a message
  $txt = "You can also export 1D barcodes in other formats (PNG, SVG, HTML). Check the examples inside the barcodes directory.\n";
  $pdf-&gt;MultiCell(70, 50, $txt, 0, 'J', false, 1, 125, 30, true, 0, false, true, 0, 'T', false);
  $pdf-&gt;SetY(30);
  
  // -----------------------------------------------------------------------------
  
  $pdf-&gt;SetFont('helvetica', '', 10);
  
  // define barcode style
  $style = array(
      'position' =&gt; '',
      'align' =&gt; 'C',
      'stretch' =&gt; false,
      'fitwidth' =&gt; true,
      'cellfitalign' =&gt; '',
      'border' =&gt; true,
      'hpadding' =&gt; 'auto',
      'vpadding' =&gt; 'auto',
      'fgcolor' =&gt; array(0,0,0),
      'bgcolor' =&gt; false, //array(255,255,255),
      'text' =&gt; true,
      'font' =&gt; 'helvetica',
      'fontsize' =&gt; 8,
      'stretchtext' =&gt; 4
  );
  
  // PRINT VARIOUS 1D BARCODES
  
  // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  $pdf-&gt;Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
  $pdf-&gt;write1DBarcode('CODE 39', 'C39', '', '', '', 18, 0.4, $style, 'N');
  
  // ---------------------------------------------------------
  
  //Close and output PDF document
  // $pdf-&gt;Output('example_027.pdf', 'I');
  
  $fileatt  = $pdf-&gt;Output('tracking_barcode_.pdf', 'E');
  
  // var_dump($doc);die;
  
  $data = chunk_split($fileatt);
  
  
  
  include_once(_PS_SWIFT_DIR_.'Swift.php');
  include_once(_PS_SWIFT_DIR_.'Swift/Connection/SMTP.php');
  include_once(_PS_SWIFT_DIR_.'Swift/Connection/NativeMail.php');
  include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php');
  
  $id_shop = 1;
  
  if (!$id_shop) {
      $id_shop = Context::getContext()-&gt;shop-&gt;id;
  }
  
  $configuration = Configuration::getMultiple(array(
      'PS_SHOP_EMAIL',
      'PS_MAIL_METHOD',
      'PS_MAIL_SERVER',
      'PS_MAIL_USER',
      'PS_MAIL_PASSWD',
      'PS_SHOP_NAME',
      'PS_MAIL_SMTP_ENCRYPTION',
      'PS_MAIL_SMTP_PORT',
      'PS_MAIL_TYPE'
  ), null, null, $id_shop);
  
  $from = $configuration['PS_SHOP_EMAIL'];
  $from_name = '';
  $connection = new Swift_Connection_SMTP(
      $configuration['PS_MAIL_SERVER'],
      $configuration['PS_MAIL_SMTP_PORT'],
      $configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl' ? Swift_Connection_SMTP::ENC_SSL : (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls' ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF))
  );
  $connection-&gt;setTimeout(4);
  
  if (!$connection) {
      echo false;die;
  }
  if (!empty($configuration['PS_MAIL_USER'])) {
      $connection-&gt;setUsername($configuration['PS_MAIL_USER']);
  }
  if (!empty($configuration['PS_MAIL_PASSWD'])) {
      $connection-&gt;setPassword($configuration['PS_MAIL_PASSWD']);
  }
  
  $swift = new Swift($connection, Configuration::get('PS_MAIL_DOMAIN', null, null, $id_shop));
  
  $subject = '['.Configuration::get('PS_SHOP_NAME', null, null, $id_shop).'] Tracking barcode';
  $message = new Swift_Message($subject);
  
  $message-&gt;setCharset('utf-8');
  
  
  $message-&gt;attach(new Swift_Message_Attachment($data, 'tst.pdf'));
  
          $to_list = new Swift_RecipientList();
  
          $to_list-&gt;addTo('tst@gmail.com', 'tst');
  $send = $swift-&gt;send($message, $to_list, new Swift_Address($from, $from_name));
  $swift-&gt;disconnect();
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/6689567/sending-an-email-attachment-using-tcpdf">sending an email attachment using TCPDF</a></p>
  
  <p>The email is sent with the attachment but I'm unable to open the file with pdf viewer .</p>