<?php
  
//
  //  calendar.php - generates a PDF file of a monthly calendar.
  //  Author: Jeff Lee <jeff at shipbrook dot com>
  //
  
set_time_limit( 120 );                                                                          // Time out after two minutes
  
require('../fpdf/fpdf.php');                                                                    // Uses the FPDF library to generate PDFs

  
$month = isset( $HTTP_POST_VARS["month"] ) ? $HTTP_POST_VARS["month"] : date("n");              // Get month from posted form data
  
$year = isset( $HTTP_POST_VARS["year"] ) ? $HTTP_POST_VARS["year"] : date("Y");                 // Get year from posted form data
  
$imageurl = isset( $HTTP_POST_VARS["imageurl"] ) ? $HTTP_POST_VARS["imageurl"] : "";            // Get URL of image user requested

  
if (isset( $HTTP_POST_FILES['imagefile']['tmp_name'] )) $imageurl = $HTTP_POST_FILES['imagefile']['tmp_name']; // uploaded file overrides URL

  
$thetime = mktime( 1, 0, 0, $month, 15, $year );                                                // Get a timestamp around the middle of the month.
  
$titlestring = date( "F Y", $thetime );                                                         // Build the title string (e.g. "May 2005")
  
$daysinmonth = date( "t", $thetime);                                                            // Get the number of days in the month
  
$numweeks = WeeksInMonth( $month, $year );                                                      // Get the number of weeks in the month
  
$cellheight = $cellwidth = 2;                                                                   // Calendar box size: each day is 2 cm tall/wide.
  
$x = (21.59 - 7*$cellwidth) / 2;                                                                // Calculate upper-left corner of the calendar boxes
  
$y = 26.94 - ($numweeks * $cellheight);

  
$pdf=new FPDF( "P", "cm", "Letter" );                                                           // Portrait mode, units in centimeters, letter size
  
$pdf->SetCreator("Jeff Lee's PDF Calendar Creator");                                            // Set the PDF's Creator tag
  
$pdf->SetTitle($titlestring);                                                                   // Set the PDF's Title tag
  
$pdf->AddPage();                                                                                // Start a new page
  
$pdf->SetLineWidth(0.02);                                                                       // Draw lines of width 0.02 cm
  
$pdf->AddFont("PalatinoLinotype-Roman", "", "pala.php");                                        // I like Palatino Linotype.  You'd better like it too.
  
$pdf->SetFont("PalatinoLinotype-Roman", "", "18" );                                             // Select 18-point type for the heading
  
$pdf->SetXY( ((21.59 - $pdf->GetStringWidth($titlestring)) / 2), $y-1 );                        // Center the month/year above the calendar boxes
  
$pdf->Cell( 0, 0, $titlestring, 0, 1 );                                                         // Output the month/year.
  
$pdf->SetFillColor( 230 );                                                                      // Select the shade for the Saturdays and Sundays
  
$pdf->Rect( $x, $y, $cellwidth, $numweeks*$cellheight, "F" );                                   // Shade in the Sundays column
  
$pdf->Rect( $x+6*$cellwidth, $y, $cellwidth, $numweeks*$cellheight, "F" );                      // Shade in the Saturdays column
  
$pdf->Rect( $x, $y, 7*$cellwidth, $numweeks*$cellheight );                                      // Draw a box around the entire thing
  
for ($i = 1; $i < 7; $i++) {                                                                    // Now draw separator lines within the box.
    
$pdf->Line( $x+$i*$cellwidth, $y, $x+$i*$cellwidth, $y+$numweeks*$cellheight );               //   (vertical lines)
    
if ($i < $numweeks) $pdf->Line( $x, $y+$i*$cellheight, $x+7*$cellwidth, $y+$i*$cellheight );  //   (horizontal lines)
  
}
  
$dow = date( "w", mktime( 1, 0, 0, $month, 1, $year ));                                         // Get the day-of-week for the first day in the month
  
$xpos = $x + 0.05 + $cellwidth * $dow;                                                          // Calculate the starting X position for the numbers
  
$ypos = $y + 0.25;                                                                              // Calculate the starting Y position for the numbers
  
$pdf->SetFontSize( 12 );                                                                        // Select 12-point type for the numbers
  
for ($i = 1; $i <= $daysinmonth; $i++) {                                                        // For each day in the month:
    
$pdf->SetXY( $xpos, $ypos );                                                                  //   Move to the appropriate position
    
$pdf->Cell( 0, 0, $i, 0, 0 );                                                                 //   Output the day number
    
$xpos += $cellwidth;                                                                          //   Calculate the next X position for the numbers
    
if (++$dow == 7) {                                                                            //   If we've fallen off the end of the week,
      
$dow = 0;                                                                                   //     Return to Sunday
      
$xpos = $x + 0.05;                                                                          //     Calculate the X position for Sunday
      
$ypos += $cellheight;                                                                       //     Move the Y position down one row
    
}
  }

  if (
$imageurl) {                                                                                // If we've got an image URL (or filename),
    
list( $imgwidth, $imgheight, $imgtype, $attr ) = @getimagesize( $imageurl );                  //   Get the image dimensions and type
    
if (($imgwidth) && ($imgheight) && ($imgtype > 1) && ($imgtype < 4)) {                        //   If it's at least 1x1 and JPEG or PNG,
      
$typestring = $imgtype==2 ? "JPG" : "PNG";                                                  //     Set a variable describing the type
      
$theheight = $y - 3;                                                                        //     Maximize its vertical dimensions on the page
      
$thewidth = $imgwidth / ($imgheight / $theheight);                                          //     and calculate its horizontal dimensions from that. 
      
if ($thewidth > 19.59) {                                                                    //     If it's too wide for the page,
        
$theheight = $theheight / ($thewidth/19.59);                                              //       reduce it appropriately.
        
$thewidth = 19.59;
      }
      
$pdf->Image( $imageurl, (19.59-$thewidth)/2+1, (($y-3)-$theheight)/2+1, $thewidth, $theheight, $typestring );  // Add the image to the page.
    
}
  }

  
$data = $pdf->Output("","S");                                                                   // Stuff the PDF data into a string
  
header("Content-type: application/pdf");                                                        // Tell the user's browser to expect a PDF file
  
header("Content-disposition: inline; filename=calendar.pdf");                                   // Force a download rather than displaying in the browser
  
header("Content-length: " . strlen($data));                                                     // Tell the browser how big the PDF will be
  
echo $data;                                                                                     // Send the PDF to the user
  
if (isset( $HTTP_POST_FILES['imagefile']['tmp_name'] ))                                         // If a file was uploaded by the user, delete it (PHP
    
unlink( $HTTP_POST_FILES['imagefile']['tmp_name'] );                                          //   supposedly does this automatically, but make sure)

  
function WeeksInMonth( $month, $year ) {                                                        // My Webhost doesn't support the calendar functions, so
    
$daysinmonth = date( "t", mktime( 12, 0, 0, $month, 15, $year ));                             //   this is a quick hack to count the number of weeks
    
$theday = mktime( 1, 0, 0, $month, 1, $year );                                                //   in any given month.
    
$thedow = date("w", $theday);
    
$weeks = $thedow ? 1 : 0;
    for (
$i = 1; $i <= $daysinmonth; $i++) if (!date("w",mktime(1,0,0,$month,$i,$year))) $weeks++;
    return 
$weeks;
  }
?>