FPDF and the Check Mark

For years I’ve been creating PDFs with the lightweight and easy to control FPDF library.  Occassionally I’ve been called on to create checkboxes, and have always fallen back to just using an “X” to indicate the checked-ness of the box.

The reason for this was that I couldn’t find any workable solution for creating the checks.  It’s apparently searched for often enough, but there are no good solutions, other than falling back to an image file; which I think is overkill for this need.

I had a client recently ask me if he can have actual checkmarks in the PDF instead of the X’s, and I told him no, there was no good way to do this.  I told him I’d already researched it fruitlessly several times with no good answer.

This rubbed me the wrong way, having to tell a client ‘No,’ so I did some further digging and found the solution is not only simple, it’s been sitting in front of us since the beginning.

Installed by default in the FPDF libraray are five fonts:

  • Courier
  • Helvetica (Arial)
  • Times
  • Symbol
  • ZapfDingbats

My personal disdain for symbol-type fonts kept me from looking at the last two until I determined to set out to solve this checkmark problem.  All it took was looking at a map of the character set to see how easy this should be:

dingbats

And it was just that easy.  Here’s the final code to produce the checkmark in a box:

$pdf->SetX($left_margin);
if($boolean_variable == true)
$check = "4"; else $check = "";
$pdf->SetFont('ZapfDingbats','', 10);
$pdf->Cell($checkbox_size, $checkbox_size, $check, 1, 0);

Setting $check to the string “4” in ZapfDingbats (may the lords of decency have mercy on my soul) gives the checkbox right in the PDF without resorting to an image file:

checkbox-in-pdf

Problem solved.  We now have Checkmarks in a PDF, using FPDF without any headaches.

6 thoughts on “FPDF and the Check Mark”

  1. Alan Shum

    This is just excellent and worked nicely. Thank you.
    I converted it to a function for making it even simpler to add a checkbox:

    
    function checkbox( $pdf, $checked = TRUE, $checkbox_size = 5 , $ori_font_family = 'Arial', $ori_font_size = '10', $ori_font_style = '' )
    {
      if($checked == TRUE)
        $check = "4";
      else
        $check = "";
    
      $pdf->SetFont('ZapfDingbats','', $ori_font_size);
      $pdf->Cell($checkbox_size, $checkbox_size, $check, 1, 0);
      $pdf->SetFont( $ori_font_family, $ori_font_style, $ori_font_size);
    }
    

    To call it, simply run: ($pdf is the FPDF object reference)
    
    checkbox( $pdf, TRUE);
    checkbox( $pdf, FALSE);
    checkbox( $pdf, $whatever_boolean_variable);
    

    1. Kemal

      Hey alan, i always failed when i tried that function. Can i look the entire coding? espescially when call that function

  2. Kemal

    Hey alan, i always failed when i tried that function. Can i look the entire coding? espescially when call that function

  3. Charles Wilkins IV

    I’m trying to populate a PDF using fdpm.php and I’d like to incorporate this as I can’t find a way to check the checkbox.

    I generate an array of $fields:
    $fields = array(
    ‘Center Address’ => ‘Meadow Lanes’,
    ‘Center’ => ‘5371’,
    ‘Address’ => ’20 McCrilis Rd East Wilton, ME 04294′,
    ‘Competition’ => ”,
    ……

    Then use the following to output the pdf:
    $pdf = new FPDM(‘blank_form2d.pdf’);
    $pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
    $pdf->Merge();
    $pdf->Output();
    ?>

    How can I incorporate your code to check the checkbox on this form. For example, if I have a checkbox named ‘Check 150’ – how do I get it to show as checked?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>