How to create PDF on Raspberry PI with .NET Core 3.1
Raspberry PI is a miniature single-board computer with ARM processor. This microcomputer is often used as an educational platform or for development of embedded solutions.
For our experiments we use the Raspberry PI 3B board with 1GB of RAM and the installed Linux Raspbian Buster with desktop operating system. I will omit the installation and configuration of the system - we will assume that everything is already installed and working for you.
Despite the tiny size, we are using a computer with powerful capabilities. Let's try to install the .NET Core framework on it and write a simple C # application that will generate a PDF document.
Firstly, we need to connect to the Raspberry via SSH or open the terminal application on the desktop if you connected the board to the monitors and keyboard. Of course, the board must be connected to the Internet to install the components we need.
The Raspbian operating system supports the execution of .NET Core applications for the ARM32 architecture. A link to the .NET Core SDK can be found on the official page.
Download and unzip the archive:
$ sudo wget https://download.visualstudio.microsoft.com/download/pr/f2e1cb4a-0c70-49b6-871c-ebdea5ebf09d/acb1ea0c0dbaface9e19796083fe1a6b/dotnet-sdk-3.1.300-linux-arm.tar.gz
$ mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-3.1.300-linux-arm.tar.gz -C $HOME/dotnet
Then we need to add the path to the .NET Core folder in the PATH environment variable and also create the DOTNET_ROOT variable:
$ export DOTNET_ROOT=$HOME/dotnet
$ export PATH=$PATH:$HOME/dotnet
Last lines are best added to the user profile configuration file: ~/.bash_profile, ~/.bashrc, ~/.kshrc, ~/.profile, ~/.zshrc, ~/.zprofile.
The correct installation of the .NET Core SDK can be verified by the following command:
$ dotnet --info
.NET Core SDK (reflecting any global.json):
OS Name: raspbian
OS Version: 10
OS Platform: Linux
Base Path: /home/pi/dotnet/sdk/3.1.300/
Host (useful for support):
.NET Core SDKs installed:
.NET Core runtimes installed:
Microsoft.AspNetCore.App 3.1.4 [/home/pi/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.4 [/home/pi/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
For further work you need to install the additional packages (all the rest are already installed with Linux Raspbian Buster with desktop):
$ sudo apt-get install libgdiplus
$ sudo wget http://ftp.de.debian.org/debian/pool/contrib/m/msttcorefonts/ttf-mscorefonts-installer_3.6_all.deb
$ sudo apt-get install -y ttf-mscorefonts-installer_3.6_all.deb
We installed the font set that comes with Windows, so the applications will look similar on both Windows and Linux.
Now you can create our application. Lets run the command:
$ dotnet new console -o testpdf
We see a template of the console application with the files testpdf.csproj and Program.cs in the testpdf folder.
Replace the code of the testpdf.csproj file:
<ItemGroup> <PackageReference Include="FastReport.Compat" Version="2020.3.2" /> <PackageReference Include="FastReport.OpenSource" Version="2020.3.1" /> <PackageReference Include="FastReport.OpenSource.Export.PdfSimple" Version="2020.3.1" /> </ItemGroup>
The file contains links to the Nuget FastReport.OpenSource, FastReport.Compat and FastReport.OpenSource.Export.PdfSimple packages. They will be downloaded during the build process and placed in the ~/.nuget/packages.
The Program.cs file should be replaced with the following code:
static void Main()
Console.WriteLine("Test FastReport Open Source");
// create report object
using Report report = new Report();
// create page
using ReportPage page = new ReportPage();
// add page in report
// create band
page.ReportTitle = new ReportTitleBand()
Height = Units.Centimeters * 10
// create text object placed on band
using TextObject text = new TextObject()
Left = Units.Centimeters * 7,
Top = Units.Centimeters * 5,
Font = new System.Drawing.Font("Arial", 24),
CanGrow = true,
AutoWidth = true,
Text = "Hello Raspberry!",
Parent = page.ReportTitle
// make the document
// save the document as PDF file
using PDFSimpleExport pdf = new PDFSimpleExport();
The code creates a report instance, adds a page to it, and a band on it. Then a text object is created at the coordinates Left and Top. The CanGrow and AutoWidth properties allow the object to automatically calculate the height and width depending on the size of the text.
Creating objects in program code is not the only way to generate documents. You can use the "Designer Community Edition" - template editor bundled with FastReport Open Source. The file with the *.frx extension generated by the editor can then be loaded using the Report.Load method. In the xml template you can specify bindings to user data, variables, use built-in and user-defined functions.
After preparing the document we save it to a PDF file. All objects used in the code contain many properties that affect their behavior in the document. More details can be found in the documentation for the FastReport Open Source product.
Let's run the program:
$ dotnet run
If everything is done and all the necessary packages are installed, we will get the file.pdf. Otherwise you need to read the text of the errors and eliminate them.
Unfortunately, there is a significant drawback of the Open Source version: PDF files contain inside the images instead of text. Copying such text will not work and the file size will be significantly larger. For simple applications this should be enough. If you need get PDF fully compliant with the standard, contains text and an embedded font you need try FastReport .NET. The Comparison Table can be found here.
When creating a PDF using FastReport Open Source, you may encounter the problem of rendering characters as empty squares. This .NET Core bug has long been fixed for x86 and x64 platforms. In our case you need to wait for a fix in the latest .NET Core builds or use the .NET Core SDK 2.2.
I am very pleased with the ability to run .NET Core applications on ARM32 / ARM64 platforms. Server manufacturers have already begun selling hardware platforms on ARM processors. It is possible that in the very near future we will see an increase in the popularity of ARM Bare-Metal offers on hosting providers.
Well, now we have the opportunity to practice on the Raspberry PI with an eye on the server.
The examples described in the article can be found on GitHub.
Author: Aleksandr Fediashov
Post a Comment