You are here

function get_pdf_pages in Recipe 6

1 call to get_pdf_pages()
recipe_pdf35_export_single in plugins/recipe_pdf35.module

File

plugins/recipe_pdf35.module, line 249
recipe_pdf35.module - Enables exporting of 3x5" cards in pdf format. This is incredibly rudimentary at this point. 1 and only 1 card and if you go over, the text is lost.

Code

function get_pdf_pages($recipe_data) {
  $pages = array();

  /*
   * Handle the ingredients.
   */

  // Put them in the correct column.
  $col1 = array();
  for ($i = 0; $i < count($recipe_data['ingredients']); $i++) {
    $tmp_list = wrap_and_pdf_escape($recipe_data['ingredients'][$i], 35, "\n   ");
    $col1 = array_merge($col1, $tmp_list);
  }

  // setup first page.
  $page = get_new_pdf_page($recipe_data);
  $line = 1;
  for ($i = 0; $i < count($col1); $i++) {

    // You've filled the page, get a new one.
    if ($line > 16) {

      // End the strings in the columns.
      if (count($page['column_1']) > 0) {
        $obj = $page['column_1'][count($page['column_1']) - 1];
        $obj .= 'ET';
        $len = strlen($obj);
        $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
        $page['column_1'][count($page['column_1']) - 1] = $obj;
      }
      if (count($page['column_2']) > 0) {
        $obj = $page['column_2'][count($page['column_2']) - 1];
        $obj .= 'ET';
        $len = strlen($obj);
        $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
        $page['column_2'][count($page['column_2']) - 1] = $obj;
      }

      // Put the page onto pages and get a new one.
      $pages[] = $page;
      $page = get_new_pdf_page($recipe_data);
      $line = 1;
    }

    // If you have more than 16 ingredients, use two columns.
    if (count($col1) > 16) {
      if ($line == 1) {

        // The ingredients header.
        $ingredient_heading = $recipe_data['ingredient_heading'];
        $obj = "BT /F1 10 Tf 12 TL 20 185 Td ({$ingredient_heading})' ET";
        $len = strlen($obj);
        $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
        $page['column_1'][] = $obj;

        // The 2-coloumn ingredients header underline.
        $obj = "0.5 w\n20 170 m\n340 170 l\nS\n";
        $len = strlen($obj);
        $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
        $page['column_1'][] = $obj;

        // Begin ingredients text for column 1.
        $page['column_1'][] = "BT /F1 8 Tf 10 TL 25 170 Td ";

        // Begin ingredients text for column 2.
        $page['column_2'][] = "BT /F1 8 Tf 10 TL 170 170 Td ";
      }
      $page['column_1'][count($page['column_1']) - 1] .= $col1[$i++];
      $page['column_2'][count($page['column_2']) - 1] .= $col1[$i];
    }
    else {
      if ($line == 1) {

        // The ingredients header.
        $ingredient_heading = $recipe_data['ingredient_heading'];
        $obj = "BT /F1 10 Tf 12 TL 20 185 Td ({$ingredient_heading})' ET";
        $len = strlen($obj);
        $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
        $page['column_1'][] = $obj;

        // The 1-column ingredients header underline.
        $obj = "0.5 w\n20 170 m\n155 170 l\nS\n";
        $len = strlen($obj);
        $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
        $page['column_1'][] = $obj;
        $page['column_1'][] = "BT /F1 8 Tf 10 TL 25 170 Td ";
      }
      $page['column_1'][count($page['column_1']) - 1] .= $col1[$i];
    }
    $line++;
  }

  // Finish the last page.
  if (count($page['column_1']) > 0) {
    $obj = $page['column_1'][count($page['column_1']) - 1];
    $obj .= 'ET';
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
    $page['column_1'][count($page['column_1']) - 1] = $obj;
  }
  if (count($page['column_2']) > 0) {
    $obj = $page['column_2'][count($page['column_2']) - 1];
    $obj .= 'ET';
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
    $page['column_2'][count($page['column_2']) - 1] = $obj;
  }

  /*
   * Handle the instructions.
   */

  // $page still contains last page
  $col = 0;
  $col2_i_list = array();

  // If column 2 is empty, use it.
  if (count($page['column_2']) == 0) {
    $col = 2;
    $inst_list = wrap_and_pdf_escape($recipe_data['instructions'], 50);
  }
  else {
    $col = 0;
    $inst_list = wrap_and_pdf_escape($recipe_data['instructions'], 85);

    // Put the page onto pages and get a new one.
    $pages[] = $page;
    $page = get_new_pdf_page($recipe_data);
  }
  $instruction_heading = $recipe_data['instruction_heading'];
  if ($col == 0) {

    // The instructions header.
    $obj = "BT /F1 10 Tf 12 TL 20 185 Td ({$instruction_heading})' ET";
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
    $page['column_0'][] = $obj;

    // The instructions header underline.
    $obj = "0.5 w\n20 170 m\n340 170 l\nS\n";
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
    $page['column_0'][] = $obj;
  }
  elseif ($col == 2) {

    // The instructions header.
    $obj = "BT /F1 10 Tf 12 TL 165 185 Td ({$instruction_heading})' ET";
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
    $page['column_2'][] = $obj;

    // The instructions header underline.
    $obj = "0.5 w\n165 170 m\n340 170 l\nS\n";
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}endstream";
    $page['column_2'][] = $obj;
  }
  $line = 1;
  for ($i = 0; $i < count($inst_list); $i++) {

    // You've filled the page, finish the columns and get a new page.
    if ($line > 16) {
      if (count($page['column_2']) > 0) {
        $obj = $page['column_2'][count($page['column_2']) - 1];
        $obj .= 'ET';
        $len = strlen($obj);
        $obj = "<< /Length {$len} >>\nstream\n{$obj}endstream";
        $page['column_2'][count($page['column_2']) - 1] = $obj;
      }
      if (count($page['column_0']) > 0) {
        $obj = $page['column_0'][count($page['column_0']) - 1];
        $obj .= 'ET';
        $len = strlen($obj);
        $obj = "<< /Length {$len} >>\nstream\n{$obj}endstream";
        $page['column_0'][count($page['column_0']) - 1] = $obj;
      }
      $pages[] = $page;
      $page = get_new_pdf_page($recipe_data);
      $line = 1;
      $col = 0;
    }
    if ($line == 1) {
      if ($col == 2) {
        $page['column_2'][] = "BT /F1 8 Tf 10 TL 170 170 Td ";
      }
      elseif ($col == 0) {
        $page['column_0'][] = "BT /F1 8 Tf 10 TL 20 170 Td ";
      }
    }
    if ($col == 2) {
      $page['column_2'][count($page['column_2']) - 1] .= $inst_list[$i];
    }
    elseif ($col == 0) {
      $page['column_0'][count($page['column_0']) - 1] .= $inst_list[$i];
    }
    $line++;
  }

  // Finish the last page.
  if (count($page['column_0']) > 0) {
    $obj = $page['column_0'][count($page['column_0']) - 1];
    $obj .= 'ET';
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
    $page['column_0'][count($page['column_0']) - 1] = $obj;
  }
  if (count($page['column_1']) > 0) {
    $obj = $page['column_1'][count($page['column_1']) - 1];
    $obj .= 'ET';
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
    $page['column_1'][count($page['column_1']) - 1] = $obj;
  }
  if (count($page['column_2']) > 0) {
    $obj = $page['column_2'][count($page['column_2']) - 1];
    $obj .= 'ET';
    $len = strlen($obj);
    $obj = "<< /Length {$len} >>\nstream\n{$obj}\nendstream";
    $page['column_2'][count($page['column_2']) - 1] = $obj;
  }

  // Add remaining page to the pages array.
  $pages[] = $page;
  return $pages;
}