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:
- Helvetica (Arial)
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:
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:
Problem solved. We now have Checkmarks in a PDF, using FPDF without any headaches.