Fix Error: Could not encode content to ISO-8859-1 After Generate QRCode in Laravel
A few days ago, I received feedback from a customer that there was an error on
one of the websites I created and developed.
The error was something like this:
Could not encode content to ISO-8859-1 and there was an exception
stating the problem in the library
BaqonQRCode\Exception\WriterException.
Problem:
I searched for the problem on the internet, it most likely occurred because
the device used by the user, an iPhone, and its Safari browser, could not read
the default character of this QRCode: ISO-8859-1.
ISO-8859-1 is an encoding character commonly used for languages in Western
Europe.
I am not sure which iPhone series the user was using, it seems to be below
6/7. And I also do not know the exact version of the Safari browser.
Solution:
Okay, so I decided to use UTF-8 characters instead of
ISO-8859-1. UTF-8 characters are more compatible and mostly support
various devices, including iPhones and Androids.
Also, I used Laravel 9 and the QR Code library
Simple QRCode
from Simple Software.
Here is my code for generating a QR Code before converting to UTF-8 in the
blade.php
template:<div class="visible-print text-center p-5">
{!! QrCode::size(300)->errorCorrection('H')->generate('url_or_whatever_you_want_to_show_to_the_qrcode') !!}
</div>
To change the character encoded in the QR Code to UTF-8, you can use this
encoding method. So it becomes like this:
<div class="visible-print text-center p-5"> {!! QrCode::size(300)->errorCorrection('H')->encoding('UTF-8')->generate('url_or_whatever_you_want_to_show_to_the_qrcode') !!} </div>
By changing the character encoding to UTF-8, the error should not appear
again.
However, I haven't actually tested it on an iPhone device because there
isn't an iPhone device available for testing yet. Maybe I will try it later.