You are here

function commerce_invoice_pdf_deliver in Commerce Invoice 7.2

Page callback to generate and deliver a PDF file for an invoice.

1 string reference to 'commerce_invoice_pdf_deliver'
commerce_invoice_pdf_menu in modules/pdf/commerce_invoice_pdf.module
Implements hook_menu().

File

modules/pdf/commerce_invoice_pdf.module, line 83
The Commerce Invoice PDF module.

Code

function commerce_invoice_pdf_deliver(Invoice $invoice) {
  $file = commerce_invoice_pdf_create($invoice);
  if (!$file || !is_file($file->uri)) {

    // If this error happens, it's probably to do with file permissions.
    drupal_set_message(t('The invoice PDF file could not be found.'), 'error');
    return MENU_NOT_FOUND;
  }

  // Transfer the file.
  $headers = array(
    'Content-Type' => $file->filemime,
    'Content-Length' => $file->filesize,
  );

  // The 'download' GET parameter will attempt to force a download, rather than
  // viewing the file directly.
  if (!empty($_GET['download'])) {
    $headers = array(
      'Content-Type' => 'force-download',
      'Content-Disposition' => 'attachment; filename="' . $file->filename . '"',
      'Content-Transfer-Encoding' => 'binary',
      'Accept-Ranges' => 'bytes',
    );
  }

  // The following invocation of hook_file_download() uses the same logic as the
  // core function file_download().
  // @see file_download()
  foreach (module_implements('file_download') as $module) {
    $function = $module . '_file_download';
    $result = $function($file->uri);
    if ($result == -1) {
      $headers = array();
      break;
    }
    if (isset($result) && is_array($result)) {
      $headers = array_merge($headers, $result);
    }
  }
  if (count($headers)) {
    file_transfer($file->uri, $headers);
  }
  return MENU_ACCESS_DENIED;
}