0

I have need to send pdf via drupal mail. I want to use tcpdf library but when im trying to use example i have error: http: //..../tcpdf_example/download/simple

Fatal error: Call to a member function DrupalInitialize() on a non-object in C:\xampp\htdocs\drupalac_gw\sites\all\modules\tcpdf\tcpdf_example\tcpdf_example.pages.inc on line 55

  $html = theme('tcpdf_example_basic_html');

  // Never make an instance of TCPDF or TCPDFDrupal classes manually.
  // Use tcpdf_get_instance() instead.
  $tcpdf = tcpdf_get_instance();
  /* DrupalInitialize() is an extra method added to TCPDFDrupal that initializes
  *  some TCPDF variables (like font types), and makes possible to change the
  *  default header or footer without creating a new class.
  */
  $tcpdf->DrupalInitialize(array(
    'footer' => array(
      'html' => 'This is a test!! <em>Bottom of the page</em>',
    ),
    'header' => array(
      'callback' => array(
        'function' => 'tcpdf_example_default_header',
        // You can pass extra data to your callback.
        'context' => array(
          'welcome_message' => 'Hello, context array!',
        ),
      ),
    ),
  ));

then somehow...

  ...
  $attachment = array(
  'filecontent' => $tcpdf->Output('', 'E');
  'filename' => 'test.txt',
  'filemime' => 'text/plain',
  );

  switch($key) {
  case 'notice':
  $langcode = $message['language']->language;
  $message['subject'] = 'example submission from' . $account;
  $message['body'][] =''. $account .' has submitted an example.';
  $message['params']['attachments'][] = $attachment;
  break;
  ...

What i need to correct to get rid of error?

3

Ok First off all Installing drupal TCPDF module is not enough to have working pdf-s. We are also obligated to download TCPDF libray from http //www.tcpdf.org/ and place it to drupal files so that library will be avaible like this

C:\xampp\htdocs\drupalac_gw\sites\all\libraries\tcpdf\tcpdf.php

above will solve:

Fatal error: Call to a member function DrupalInitialize()

With working pdf library sending emails with attachments is easy i have like this:

drupal_mail('module', 'notice', "to@mail","", array(), "from@mail");

where:

function module_mail($key, &$message, $params) {
  $attachment = array(
    'filecontent' => generatePdf("S"),
    'filename' => 'example-filename',
    'filemime' => 'application/pdf',
  );

  switch($key) {
    case 'notice':
      $langcode = $message['language']->language;
      $message['subject'] = 'example submission from ';
      $message['body'][] ='my example.';
      $message['params']['attachments'][] = $attachment;
      break;
  }  
}

And where:

function generatePdf($where="S") {
  $pdf = tcpdf_get_instance();

  $pdf->setFontSubsetting(true);

  // set font
  $pdf->SetFont('freeserif', '', 12);

  $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 008', PDF_HEADER_STRING);

  // set header and footer fonts
  $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

  // set default monospaced font
  $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

  // set margins
  $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP+10, PDF_MARGIN_RIGHT);
  $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

  // set auto page breaks
  $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

  // set image scale factor
  $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  return $pdf->Output('example_008.pdf', $where);
}

Above is now my working example :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.