ASNA Knowledge Base
Technical references, how-tos, and trouble-shooting for ASNA products

Print to PDF with Visual RPG and Windows

View Source

Download

Enable the Microsoft PDF print driver as a Windows feature

Until Windows 10 and Windows Server 2016 came along, you needed to use a third-party PDF driver to print to PDF with Web applications. There are free PDF drivers available, but these low-end drivers aren’t multi-threaded and they don’t work with Web applications.

Even for Windows apps, the low-end drivers are a hassle. If they allow you to specify the PDF file name, that task usually requires a registry hack.

Alas, for several years now Microsoft has packaged a PDF print driver with both Windows and Windows Servers. The driver is a Windows feature and needs to be installed with the “Windows features” dialog (available through Windows “Add or remove programs” dialog). Make sure it is enabled before continuing.

See this article for using the Microsoft PDF driver with a Web application.

Isolating print code

This example includes a that class prints an ASNA Print File to either a physical printer or the “Microsoft Print to PDF” print driver in a Windows application.

There are special considerations for using the MS PDF driver with a Web application. See this ariticle for using the MS PDF driver with an AVR web app.

I like to isolate printer code in a class for each print file. In this example, the CustomerReport does that. You don’t have to use a class to use the techniques shown in this example. You can copy code from the class and copy it inline into your code. That said, one of these days you’ll be very grateful then if you take the time to separate this code initially!

Use the CustomerReport constructor that requires two arguments to print to a physical printer or use the constructor that requires three arguements to print to a PDF file.

To print to a physical printer:

DclFld cr Type(CustomerReport) 	
DclFld PrinterName Type(*String) 
DclFld UsePrintSetup Type(*Boolean) 

PrinterName = "HP LaserJet 1020 (redirected 2)"
UsePrintSetup = *True 

// If UsePrintSetup is *True then printer setup is shown.
cr = *NEW CustomerReport(PrinterName, UsePrintSetUp) 
cr.Print()

Try 
    cr.Print()
    MsgBox String.Format("File printed") 
Catch Type(Exception) Name(ex)
    MsgBox String.Format("ERROR :" + ex.Message) 
EndTry 

To print to a PDF file with the MS native PDF print driver:

DclFld cr Type(CustomerReport) 	
DclFld PrinterName Type(*String) 

cr = *NEW CustomerReport(PrinterName) 

DclFld PrinterName Type(*String) 
DclFld PDFOutputFolder Type(*String) 
DclFld PDFFileName Type(*String) 

PrinterName = "Microsoft Print to PDF"
PDFOutputFolder = "C:\Users\thumb\Documents\projects\avr\pdf-files"

// Existing PDF files are overwritten! 
PDFFileName = "test.pdf"

DclFld cr Type(CustomerReport) 	
		
cr = *NEW CustomerReport(PrinterName, PDFOutputFolder, PDFFileName) 
Try 
    cr.Print()
    MsgBox String.Format("File {0} created in {1}", PDFFileName, +
        PDFOutputFolder) 
Catch Type(Exception) Name(ex)
    MsgBox String.Format("ERROR :" + ex.Message) 
EndTry 

When printing to the MS native PDF driver, the printer name must be:

Microsoft Print to PDF

If you have trouble with this code, use MS Word to save a sample document the MS PDF driver to ensure the driver is available. That will help confirm the driver is working.

You cannot show the printer setup in a Web app. The entire printer configuration must specified programmatically for Web apps.

Published: Feb 4, 2025