Add barcode to pdf using chart module

1 minutters læsning

On Vejle Idrætshøjskoles site I needed to make pdf’s of all the lectures, so I could create a small booklet. I created a lecture content type with a title, body and an image. I made a simple pdf of each lecture, but wanted to include a barcode, so people seeing the pdf could just scan it on their smartphones and go directly to the page and registration to participate in the lecture.

First I installed and enabled chart and libraries and put the external PHP library TCPDF in the libraries folder. Using TCPDF it is fairly easy to create a PDF, so I just needed to figure out how to use chart to create the barcode. So basically this is what I did.

$pdf = new TCPDF();

$chart = array(
  '#chart_id' => 'lectures_chart',
  '#type' => CHART_TYPE_QR,
  '#size' => chart_size(200, 200) 
);
        
$chart['#data'][] = '';
$chart['#labels'][] = $link;
        
$qr_file = chart_copy($chart, 'my_chart_' . uniqid(), '/home/username/www/sites/larsolesen.dk/files/charts/');
        
if ($qr_file !== false) {
  $pdf->Image($qr_file, 4, 250, 35, 0, '');
}

If you want to see the full approach on how I created this pdf, you can see my small custom module on github.

PDF - Vejle Idrætshøjskoles historie

There is many ways to improve the module, and it needs cleanup. However, it works, and you can easily include the barcode. Tweak it and try it (it is a custom module so it will not work directly on your website before changing it). Maybe you can even improve it?

P.S. To make the function chart_copy() work in the above example, you need to apply the patch from issue #1303706.

Kommentarer

You can be the first to add a comment below.

Leave a Comment

Din e-mail bliver ikke offentliggjort. Obligatoriske felter er markeret Required fields are marked *.

Indlæser...
Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.