Tuesday, 10 July 2012

Auto Shading OMR Sheet using PHP in PDF

Lets assume you have a sample style of these sheet below and you want to automatically shade it printing as many as possible in PDF format below.

This is not so much work as you have expected, it only takes you back to your high school days in mathematics. Do you remember drawing a graph...X and Y axis...Yes. thats what we will be doing in these tutorial. 

In Pdf, your x,y axis is like your graph starting from the bottom-left position of your page.

All you need to do is calculate the length of your sheet in pixels and thats your maximum y value. which in these case might be our starting point. since the machine works with proper alignment and efficiency, the space between the boxes to be shaded is the same. both in length and in width and that is what you need to get. which is the difference between two shaded boxes on x and y. see the image below.
lets say distance bty two shaded box is $dif_x & $dif_y respectively

The characters are split and as you encounter a new character, you move the x-axis to be shaded by $dif_x.

For the Y axis they are initialized by default. Since you know the starting position of the 1st dot(y_max) all because our starting point is 0,0 bottom left. If it were to be top left..then we add. Just remove $dif_y from Y_max(which is position A) to get position B and so on to initialize the array A-Z.

For more information, download the project and peruse UseImage.php Class.


You are going to need the ezpdf class to build your pdf. to download go to
http://sourceforge.net/projects/pdf-php/files/latest/download

The background image in JPG of your OMR Sheet you want to print on. called sheet.jpg.

The Whole Completed Project can be downloaded below.

Download Project Here

Demo looks like this.


Explanation....
Main file is Test.php

Complusory Files....
require("UseImage.php");
require('pdf-php/class.ezpdf.php');
require('Component.php');

// CREATE NEW PDF OBJECT
$pdf = new Cezpdf('A4');     // A4 paper
$pdf -> selectFont('pdf-php/fonts/Helvetica.afm');  // Selected Font to type
$pdf -> ezStartPageNumbers(300,20,8,'','',1); // Use page numbering start from 1
$pdf -> addJpegFromFile("sheet.jpg",0,0); // The background of the omr sheet

// ADD COMPONENTS (Write on the PDF Object and return it. For Page 1.)
$comp = new Component();
$pdf = $comp ->getLastname("CHUKWUNONSO",$pdf);
$pdf = $comp ->getFirstName("UCHENNA",$pdf);
$pdf = $comp ->getMiddleName("Z",$pdf);

// Note That for every new page you create to the pdf, you have to add the background.
$pdf -> ezNewPage(); // Create new page to the PDF
$pdf -> addJpegFromFile("sheet.jpg",0,0); // Add the OMR background sheet.

// ADD COMPONENTS (Write on the PDF Object and return it. For Page 2.)
$pdf = $comp ->getLastname("IGINLA",$pdf);
$pdf = $comp ->getFirstName("OMOTAYO",$pdf);
$pdf = $comp ->getMiddleName("O",$pdf);

// The code that does the final Piece.

if (isset($_GET['d']) && $_GET['d'])
{
  // Code Below Debugs if Get Variable d is set. i.e ?d=5
  $pdfcode = $pdf->ezOutput(1);
  $pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
  echo '<html><body>';
  echo trim($pdfcode);
  echo '</body></html>';
} else 
{
  $pdf->ezStream(); // GENERATE THE FINAL PDF
}

// SETTING THE REQUIRED (x, y) AXIS ON YOUR OMR SHEET. (component.php)

public function getLastname($lastname,$pdf)
{
    /* Initialze Parameters */
    $this->pdf = $pdf;
    $LN = new UseImage($this->pdf);

   // NAME TO USE
    $LN -> setNameArr($lastname); 

   // [optional] ALLOWS FOR DISPLAY OF CHARACTERS     
    $LN -> setAllowChars(true);
   
 /*
Top of your page.Note.like your graph (x,y axis starts from bottom left of the page in pdf) */
    $LN -> setMax_Y(726);                           

/* The distance between each shade box on x-axis */
    $LN -> setCoordDistanceX(11.5);

/* The distance between each shade box on x-axis */
    $LN -> setCoordDistanceY(9.2);

/* Initializes the (x, y) co-ordinates */             
    $LN -> fillCoordinates();

/*
If you are Displaying the Character, Set the Co-ordinate of the First Character to align inside the name box (x,y axis)
*/    
    $LN -> setCharsCood(27,728);






// same as above, the first cordinate of the 1st dot.
    $LN -> setDotsCood(29,98.1); 

    $LN -> display();                                 /* writes the name and the dots on the pdf */
    $this->pdf = $LN -> drawComplete();   /* returns pdf object */
    $LN = NULL;                                      /* return resources back to system */
    return $this->pdf;                               /* returns the pdfobject to write anoda name */
}


NOTE:
Just keep manipulating those coordinate values till you get your exact spot on your OMR sheet.

Debugging
If you have issues in running your application from deprecated functions, kindly comment the line set_magic_quotes_runtime ..everywhere it appears in the application. And ensure you set magic_quotes ON in your php.ini file to be able to use these pdf classes.

If you also see error or failed to load document. kindly put infront of the link in your url.. d=1 to enable debugging mode till your code is error free. eg

localhost/projects/pdf_omr/test.php?d=1

No comments:

Post a Comment