How to make PDF on Raspberry PI with .NET Core

2020-05-26

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.

Raspberry PI 3B

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:

1
2
$ 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:

1
2
$ 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.

Correct installation of the .NET Core SDK can be verified by the following command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version: 3.1.300
 Commit: b2475c1295
 
Runtime Environment:
 OS Name: raspbian
 OS Version: 10
 OS Platform: Linux
 RID: linux-arm
 Base Path: /home/pi/dotnet/sdk/3.1.300/
 
Host (useful for support):
 Version: 3.1.4
 Commit: 0c2e69caa6
 
.NET Core SDKs installed:
 3.1.300 [/home/pi/dotnet/sdk]
 
.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:
 https://aka.ms/dotnet-download

For further work you need to install the additional packages (all the rest are already installed with Linux Raspbian Buster with desktop):

1
2
3
$ 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. Let's run the command:

1
$ 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 testpdf.csproj file:

1
2
3
4
5
6
7
8
9
10
11
12
<Project Sdk="Microsoft.NET.Sdk">
 <PropertyGroup>
 <OutputType>Exe</OutputType>
 <TargetFramework>netcoreapp3.1</TargetFramework>
 </PropertyGroup>
 
 <ItemGroup>
 <PackageReference Include="FastReport.Compat" Version="2020.3.2" />
 <PackageReference Include="FastReport.Core" Version="2020.3.1-demo" />
 </ItemGroup>
 
</Project>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using FastReport;
using FastReport.Export.Pdf;
using FastReport.Utils;
 
namespace testpdf
{
class Program
 {
 static void Main()
 {
 Console.WriteLine("Test FastReport Core"); 
 // create report object
 using Report report = new Report();
 // create page
 using ReportPage page = new ReportPage();
 // add page in report
 report.Pages.Add(page);
 // 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
 report.Prepare();
 // save the document as PDF file
 using PDFExport pdf = new PDFExport();
 report.Export(pdf, "file.pdf");
 }
 }
}

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:

1
$ 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.

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.


PDF Raspberry

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:

1
2
3
4
5
 <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 Program.cs needs to be changed as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using FastReport;
using FastReport.Export.PdfSimple;
using FastReport.Utils;
 
namespace testpdf
{
 class Program
 {
 static void Main()
 {
 Console.WriteLine("Test FastReport Open Source");
 
 // ...
 // same code here ...
 // ...
 
 // save the document as PDF file
 using PDFSimpleExport pdf = new PDFSimpleExport();
 report.Export(pdf, "file.pdf");
 }
 }
}

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.

GIGABYTE ARM Server

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.

April 09, 2024

How to work with FastReport Avalonia on .NET 8 in Visual Studio Code for Linux

In this article, we will look at how to run FastReport Avalonia on .NET 8 on the Linux operating system using Visual Studio.
November 01, 2023

PDF/X, PDF/A, PDF/E: What is the difference, and why are there so many formats?

Let's try to understand the variety of PDF formats, compare the pros and cons of standards for various industries and their needs.
October 04, 2023

How to generate a report from an ASP.NET Core application using FastReport.Core.Skia

We tell you how to generate a report on Windows and Linux using FastReport.Core.Skia and a private NuGet server.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.