Add QR code to Reporting Services(SSRS) report for free without using the Global Assembly Cache (GAC)
Add QR code to Reporting Services(SSRS) report for free without using the Global Assembly Cache (GAC)
![Add QR code to Reporting Services(SSRS) report for free without using the Global Assembly Cache (GAC)](/content/images/size/w2000/2024/07/QRFeatureImage-1.jpg)
A previous post on this blog looked at how to get Zen barcodes to work in SQL reporting services reports for free.
A new need arose, needing to get customer PO numbers onto a despatch report as a QR code.
There is an excellent article on getting QR codes into SSRS reports - Generate QR CodeĀ® barcodes in an SSRS report with the QRCoder library
This article utilizes the QRCoder project from GitHub (https://github.com/codebude/QRCoder/wiki) to provide the DLL needed to generate QR codes. Additionally, it employs another custom DLL, created by the author, which includes the QRCoderImageToByteArray function. This function converts the generated QR code into a ByteArray that can be used by the SSRS picture control.
Installing QR Code DLL Files on the SSRS Server
-
Download Resources: Download the resource zip file, QRCoderResources.zip, from the bottom of the referenced article. Inside the zip file, navigate to the folder
\QRCoderResources\StronglyNamedPartiallyTrustedCallersDLLs
to find the filesQRCoder.dll
andQRCoderImageToByteArray.dll
. -
Place DLLs in the Report Server Bin Directory: Copy these DLLs to the Report Server bin directory. The exact path will depend on your SQL Server installation and version. For example:
\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\bin
Ensure you right-click each file and unblock the DLLs if required, as this is a necessary security step for files copied from outside the server.
- Edit the Report Server Policy File: To grant the necessary permissions to run these assemblies, edit the report server policy file, which avoids the need to add the DLLs to the Global Assembly Cache (GAC). Add the following block to the policy file located at (location may vary):
C:\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\rssrvpolicy.config
<CodeGroup class="UnionCodeGroup" Name="BarcodeControl1" version="1" PermissionSetName="FullTrust" Description="This code group grants QRCoderImageToByteArray.dll FullTrust permission.">
<IMembershipCondition class="UrlMembershipCondition" version="1" Url="C:\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\bin\QRCoderImageToByteArray.dll" />
</CodeGroup>
<CodeGroup class="UnionCodeGroup" Name="BarcodeControl2" version="1" PermissionSetName="FullTrust" Description="This code group grants QRCoder.dll FullTrust permission.">
<IMembershipCondition class="UrlMembershipCondition" version="1" Url="C:\Program Files\Microsoft SQL Server Reporting Services\SSRS\ReportServer\bin\QRCoder.dll" />
</CodeGroup>
This should be pasted into the node:
/configuration/mscorlib/security/policy/PolicyLevel/
In my case went after the close tag of the "NamedPermissionSets" node.
Getting the QR code into the report
-
Open report properties window: Whilst in Report Bulder design mode, right click empty area of the SSRS report , select "Report Properties",
-
Add reference to dll: Select "References" from the left hand panel of the Report Properties window.
Put the reference to the dll file into the report references by using the "Add" button for "Add or remove assembiles".
QRCoderimageToByteArray, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3016ec4525f0585e
The PublicKeyToken and Version can be found by using ILSpy, open the QRCoderimageToByteArray dll with the ILSpy application (find it on the internet) and the right hand panel will tell you this information.
- Add code funtion: Select the Code window from the left hand panel of the Report Properties window.
Now paste the function that will be called by the reprot to get the byte array image. Note that you should call the PngToByteArray method, which is different to that shown in the original article. This performed much better running reports with large numbers of QR codes on them.
Public Function QRCoderByteArray(ByVal inputString As String) as Byte()
Return QRCoderImageToByteArray.QRCoderImageToByteArrayClass.renderPngToByteArray(inputString)
End Function
Add field to the report
Drop an image onto the report, set Image properties by right clicking to get Image Properties.
- Set image source to "Database" in drop down combobox
- Set MIME type to be "image/png"
- Set the "use this field" expression to be: =Code.QRCoderByteArray(Trim(First(Fields!CSTPONBR.Value, "DespatchHeader"))) replacing the field for one from your report.
- In the Size sub tab of the Image Properties window, select "Fit Proportional"
Run the report and the QR code will show!
Another example where a list of QR codes is shown: