How to make PDF on Raspberry PI with .NET Core
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 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 its 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 download page.
Download and unzip the archive:
Then we need to add the path to the .NET Core folder in the PATH environment variable and also create the DOTNET_ROOT variable:
Last lines are best added to the user profile configuration file: ~/.bash_profile, ~/.bashrc, ~/.kshrc, ~/.profile, ~/.zshrc, ~/.zprofile.
Correct installation of the .NET Core SDK can be verified by the following command:
For further work you need to install the additional packages (all the rest are already installed with Linux Raspbian Buster with desktop):
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. Let's run the command:
We see a template of the console application with the files testpdf.csproj and Program.cs in the testpdf folder.
Replace the code of testpdf.csproj file:
File contains links to the Nuget FastReport.Core and FastReport.Compat 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:
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 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.exe template editor bundled with FastReport .NET. 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. You can learn more about capabilities of FastReport .NET on the official website.
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 documentation for the FastReport .NET product.
Let's run the program:
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.
The resulting PDF file is fully compliant with the standard, contains text and an embedded font. Text can be selected and copied to another document.
In the upper left corner of the page there is text indicating that we used the demo version of FastReport .NET Core. The maximum number of pages in the demo version is limited to five. The commercial version of FastReport .NET does not contain these restrictions. It can be purchased at Fast Reports Home Site.
There is a way to get a similar PDF file without the DEMO VERSION label absolutely for free. You can use the FastReport Open Source product.
Let's change the csproj file and ItemGroup section:
The file Program.cs needs to be changed as follows:
Then we run the program with the dotnet run command and get a PDF without the “demo version” label and any restrictions on the number of pages.
Unfortunately, there is a significant drawback of the Open Source version: inside PDF files contain 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.
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 in my profile on GitHub.