All Blog Posts  |  Next Post  |  Previous Post

Generate QR codes with TMS WEB Core


Tuesday, August 4, 2020

TMS WEB Core already has a TWebQRDecoder component to decode QR codes. It's evident that we need something that works the other way around and creates QR codes instead. Meet our new addition to tackle this problem: TWebQRCode.

TWebQRCode is a wrapper around the qrcode.js JavaScript library. qrcode.js is a small library with the purpose of creating a QR code based on a string input.


Not surprisingly, you can generate a QR code based on a text. Just drop the component onto the form, and set the TWebQRCode.Text property:
procedure TForm1.WebButton1Click(Sender: TObject);
  WebQRCode1.Text := WebEdit1.Text;

You can also retrieve the QR code as a base64 encoded string or as a TBitmap. To retrieve this data, use the GetBase64/GetBitmap methods or their GetBase64Async/GetBitmapAsync versions. The reason behind an async version of these methods is simple: GetBase64 and GetBitmap will work right away if the component is visible, because the underlying image source is a base64 encoded string. If you want to get a QR code data without showing any images, then the process will be asynchronous because the image needs to be created and loaded on the fly by the library. These asynchronous versions work with anonymous methods. This way you have full control over the retrieved data.
procedure TForm1.WebButton1Click(Sender: TObject);
  qr: TWebQRCode;
  qr := TWebQRCode.Create(Self);
  qr.Width := 200;
  qr.Height := 200;
  qr.Text := 'my text';
  qr.GetBase64Async(procedure (AData: string)
    //do something with AData
And last but not least, the QR code can be downloaded as an image. Only PNG format is supported by the library.
procedure TForm1.WebButton1Click(Sender: TObject);


As a small example, we are going to create a small business card generator application with TTMSFNCPDFLib. TTMSFNCPDFLib makes it possible to create PDF files on the fly and if we combine it with TWebQRCode then we have an application which can generate PDF files with QR codes directly in the client!

To keep it short, we are going to focus on the QR code drawing. As a first step, we need to load the default fonts and disable the button until the fonts are loaded.
procedure TForm1.WebFormCreate(Sender: TObject);
  WebButton2.Enabled := False;
  Application.OnFontCacheReady := DoFontCacheReady;

procedure TForm1.DoFontCacheReady(Sender: TObject);
  WebButton2.Enabled := True;
For the business cards we want to turn the website URL into a QR code. So the next step is to create a TTMSFNCPDFLib instance, get the TBitmap from TWebQRCode and draw it. We get the URL from a TWebEdit so if we change the website URL the underlying library needs to update the QR code first. In this case it's better to use the GetBitmapAsync method.
procedure TForm1.WebButton2Click(Sender: TObject);
  I: Integer;
  WebQRCode1.Text := edtWebsite.Text; //Set URL
  WebQRCode1.GetBitmapAsync(procedure (ABitmap: TBitmap) //Wait for bitmap to load
    PDF := TTMSFNCPDFLib.Create;
      PDF.PageSize := psA4;
      PDF.Header := '';
      PDF.Footer := '';
      PDF.Graphics.Font.Name := 'Roboto';
      PDF.Graphics.Font.Color := gcBlack;
      PDF.Graphics.Font.Style := [];
      //Draw with PDF here
We want to draw the business cards multiple times so it's better to create a method for it. We can then introduce a loop and call our drawing method with PDF and ABitmap as a parameter.
procedure TForm1.DrawBusinessCard(APDF: TTMSFNCPDFLib; ALeft, ATop, ARight, ABottom: Integer; AQR: TBitmap);
  APDF.Graphics.DrawRectangle(RectF(ALeft, ATop, ARight, ABottom));
  //text drawing would come here
  APDF.Graphics.DrawImage(AQR, RectF(ARight - 95, ABottom - 90, ARight - 25, ABottom - 20));
This sample not only shows how easy it is to create PDFs with QR codes but also demonstrates the power of TMS FNC and TMS WEB Core combined! You can play around with the full demo here.
But wait, we have some more good news: we made this component available through our TMS WEB Core Partner program which means that you can download it from here for free!

To install, open, compile & install the package from the "Component Library Source" folder. This will install the design-time TWebQRCode component.
For use at runtime, make sure that the "Core Source" folder is in your TMS WEB Core specific library path that you can set via IDE Tools, Options, TMS Web, Library path.

Tunde Keller


This blog post has received 5 comments.

1. Sunday, January 17, 2021 at 5:51:21 PM

A great possibility. Is this example a part of the tms web core examples. I can not find it...


2. Sunday, January 17, 2021 at 8:36:05 PM

You can find the download from this page:

Bruno Fierens

3. Thursday, November 18, 2021 at 11:11:02 AM

Can you tell me where to find the TWebQRDecode component as mentioned in your first line of this blog? I''m really interested in that.

van Heuven Rob

4. Thursday, November 18, 2021 at 11:14:39 AM

It can be downloaded from

Bruno Fierens

5. Monday, November 20, 2023 at 10:33:30 AM

I dont think this works for Alexandria?


Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.

All Blog Posts  |  Next Post  |  Previous Post