Printing to PDF with AVR Web apps has always been a royal hassle. Doing so required expensive, and challenging to configure, third-party print drivers; low-end print drivers don’t work either because they are single-threaded or require registry access to assign a PDF file name at runtime. In the early days of Windows, registry access for such ad hoc tasks was easy but in these security-conscious times it’s not an acceptable best practice (if, given your security policies, it can even be done at all).

Microsoft has finally come to the rescue by adding native PDF print drives to both Windows 10 and Windows Server 2016 (perhaps signaling that MS realized its XPS document writer isn’t a PDF killer!). That’s the good news, the bad news is that this PDF print driver is not present, nor is it available, for previous Windows or Windows Server versions.

You need Windows 10/11 or Windows Server 2016 or higher to get Microsoft’s native PDF driver.

We have tested this Microsoft driver on Windows 10 and Windows Server 2016 and with ASNA Visual RPG 15.0. It isn’t formally supported on AVR 14 (the version that works with Visual Studio 15), but this article’s code ran just fine with that version.

On with the code

The class below in Figure 1a provides an example class to print a report to either a PDF file or a printer. If you compare this code to how other PDF drivers work you’ll see how little friction is encountered printing to PDF with the Microsoft PDF driver.

Depending on how the CustomerReport class is instanced, printer output is directed to either a printer or a PDF file. Printing to a printer is usually not a good idea in a Web app. The printing would occur on a printer on the network, which is probably not where the user is–that’s the appeal of printing to PDF. It allows the user to print the PDF local or save it for later use. The class allows printing to a printer primarily to show how a single class can easily do double, but related, duty.

See the embedded comments for details on this class. The code is pretty simple but if the regular expressions in the CheckFileAndPathSeparators subroutine causes your eyebrows to furrow, take at look this article on regular expressions.

Figure 1a. An example class to print simple report with AVR to PDF for a Web app.

Figure 1a. An example class to print a report with AVR

Its code-behind is shown below in Figure 1b.

Figure 1b. AVR code-behind using Figure 1a’s class.