DEV Community

Cover image for PostScript to PDF without Ghostscript: lessons from a virtual printer
Wolfgang Mitterbucher
Wolfgang Mitterbucher

Posted on Originally published at mitterbucher.com

PostScript to PDF without Ghostscript: lessons from a virtual printer

Virtual PDF printers are among the most inconspicuous tools on Windows: you print from any application, and out comes a PDF. Most of them have used the same recipe for decades – a PostScript printer driver produces PostScript, and Ghostscript turns it into a PDF. In my open-source printer miPDFconvert, I have now replaced Ghostscript with the PostScript interpreter of my .NET library miPDFlib.

This post covers the result, the surprises the Windows print path had in store – and why virtual printers on Windows will have to be built quite differently in the future.

How a virtual PDF printer works on Windows

The classic design has four parts, only two of which you write yourself:

  • Printer driver: Microsoft's PostScript class driver (pscript5) ships with Windows. What it produces is defined by a printer description in PPD format: paper sizes, resolution, color – and which fonts the "printer" supposedly has built in.
  • Spooler: it accepts the job and passes the PostScript on to a port.
  • Print monitor: a DLL loaded by the spooler. It writes the job to a file and starts the program that turns it into a PDF – in the session of the user who printed.
  • Conversion: PostScript is a programming language; turning it into a PDF takes a PostScript interpreter. Until now, that was Ghostscript.

Ghostscript is mature, but it is a native program with its own installation, licensed under the AGPL or a commercial license. For miPDFconvert this meant: Ghostscript had to be installed separately at first, later it was bundled with the installer – which grew to 19 MB as a result – and the whole project was under the AGPL.

Out with Ghostscript, in with miPDFlib

miPDFlib's add-on package miPDFlib.PostScript contains a PostScript interpreter (LanguageLevel 3) in pure managed code. In miPDFconvert it replaces the call to an external program with a few lines:

dotnet add package miPDFlib.PostScript
Enter fullscreen mode Exit fullscreen mode
using MiPdf;
using MiPdf.PostScript;

var options = new PostScriptOptions
{
    AutoRotatePages = false,                // orientation comes from the driver
    Timeout = TimeSpan.FromSeconds(120)     // limit for hanging jobs
};
using var input = File.OpenRead(spoolFile);
using var document = PostScriptConverter.ToPdf(input, options);
byte[] pdf = document.ToArray();
Enter fullscreen mode Exit fullscreen mode

A PostScript error comes back as a PostScriptException with the error name, the offending command and the program's output – what a real printer would print on its error page. The interpreter runs in a sandbox without access to the file system or the network, with limits on run time and memory.

The result in numbers: the installer shrank from 19 MB to 3.4 MB, a four-page Word document with a table, an image, shapes and a chart is converted in less than 0.3 seconds, and there is no native dependency left. Since the print monitor, the printer setup and the printer description were rewritten at the same time, miPDFconvert is now licensed under the Apache License 2.0.

Lessons from practice

The first tests with hand-written PostScript worked right away. Things only got interesting with real print jobs from Word and Excel.

Printer fonts in the PPD

The first Word printout stopped with a PostScript error. The cause was not in the interpreter but in the printer description: the previous PPD came from Ghostscript and declared 47 built-in printer fonts. The driver then replaces TrueType fonts with these printer fonts – Arial with Helvetica, Courier New with Courier – and patches a euro sign into copies of them. That requires the complete programs of the 14 standard fonts, which miPDFlib deliberately does not ship.

The fix was a new PPD without printer fonts. The driver now embeds every TrueType font used, and the PDF contains the original fonts – Calibri stays Calibri. Under Ghostscript this was never the case, by the way: Arial came out as Helvetica.

Fonts that arrive bit by bit

With the new PPD all jobs went through – but the PDFs from Word had no visible text. Tables, lines and images were there, the letters were missing. A look at the embedded fonts showed an empty glyph table.

The reason: the Windows driver downloads TrueType fonts incrementally. It first sends the font skeleton with an empty glyf table and later adds each glyph actually needed, one by one, to the font's GlyphDirectory. miPDFlib already handled this for CID fonts, but not for simple TrueType (Type 42) fonts.

A second, smaller detail followed: the driver names the fonts it downloads TT476t00 and the like, and keeps the real name and style in OrigFontName and OrigFontStyle of the font's FontInfo. Use those, and a PDF viewer shows Arial,Bold instead of TT476t00. Both fixes are in miPDFlib 1.5.1 and 1.5.2.

The lesson: a PostScript interpreter for print jobs has to be tested with real driver output. The easiest way to get it is to print through the installed driver into a file – for example with PrintDocument and PrintToFile in .NET:

using System.Drawing;
using System.Drawing.Printing;

var doc = new PrintDocument();
doc.PrinterSettings.PrinterName = "miPDFconvert";
doc.PrinterSettings.PrintToFile = true;
doc.PrinterSettings.PrintFileName = @"C:\temp\job.ps";
doc.PrintPage += (_, e) => e.Graphics!.DrawString("Hello", new Font("Arial", 14), Brushes.Black, 50, 50);
doc.Print();
Enter fullscreen mode Exit fullscreen mode

Word's PrintOut has an OutputFileName parameter for the same purpose.

The comparison with Ghostscript

I then converted the same driver output with both interpreters and rendered the pages: the images are practically identical. Even quirks such as rasterized transparency come out the same with both – those are produced by the driver, not the interpreter.

The spooler has its own rules

Rewriting the print monitor and the setup revealed a few pitfalls that the documentation does not make obvious:

  • Even a monitor without configuration has to offer the functions for reading and configuring the port (ReadPort, XcvOpenPort, XcvDataPort, XcvClosePort); otherwise AddMonitor rejects it as lacking the required functions. Empty implementations are enough.
  • Installing the driver: letting AddPrinterDriverEx copy the driver files from a folder (APD_COPY_FROM_DIRECTORY) was rejected on my test system with "driver must be downloaded". What works reliably is the classic way: copy the files into the printer driver directory, then call AddPrinterDriver. The setup takes the PostScript class driver from the driver store of the target computer – Microsoft's files no longer have to be in the installer.
  • Which session? The monitor runs in the spooler service. It starts the PDF program with the token of the session the job was printed from (WTSQueryUserToken), and only the printing user can read the job file.
  • The Save As dialog in the background: because the print service starts the program, Windows' foreground lock keeps its windows behind the active one. Only attaching to the input queue of the foreground window (AttachThreadInput) before SetForegroundWindow brings the dialog to the front reliably.

Outlook: virtual printers without drivers

All of this works today – but the approach has an expiry date. In 2023, Microsoft announced the end of servicing for third-party printer drivers. For Windows 11 and Windows Server 2025:

  • 15 January 2026: new printer drivers are no longer published to Windows Update.
  • 1 July 2026: driver ranking always prefers the built-in IPP class driver.
  • 1 July 2027: third-party driver updates are only allowed for security fixes.

This is aimed mainly at drivers for physical printers. Windows continues to allow separate installation packages, and according to Microsoft there are no plans to disable features of the legacy driver model. So a classic virtual printer can still be installed.

Windows Protected Print Mode is a different matter. It exists since Windows 11 24H2, and administrators can enable it via Group Policy or Intune. In this mode, the spooler no longer loads third-party drivers, and only Microsoft-signed modules. A print monitor DLL, which practically every classic virtual printer needs, is ruled out there. The reasoning makes sense: the spooler runs with high privileges, and loadable DLLs like these were exactly the entry point for attacks such as PrintNightmare.

The future: the virtual printer as an app

Microsoft's replacement is Print Support App v4 with an architecture for virtual printers, available from Windows 11 24H2. Instead of a driver and a print monitor, an app packaged as MSIX registers one or more printers in its manifest; a Microsoft monitor takes care of the port. When printing, a background task of the app receives the print data. A sketch based on the manifest specification:

<printsupport2:Extension Category="windows.printSupportVirtualPrinterWorkflow"
                         EntryPoint="Tasks.VirtualPrinterTask">
  <printsupport2:PrintSupportVirtualPrinter DisplayName="miPDFconvert"
      PreferredInputFormat="application/postscript"
      PdcFile="Config\printer-capabilities.xml">
    <printsupport2:SupportedFormats>
      <printsupport2:SupportedFormat Type="application/pdf" MaxVersion="1.7" />
    </printsupport2:SupportedFormats>
  </printsupport2:PrintSupportVirtualPrinter>
</printsupport2:Extension>
Enter fullscreen mode Exit fullscreen mode

Three details matter for a PDF printer:

  • PreferredInputFormat may only be application/oxps or application/postscript. Choosing PostScript gives you the same format as today – so a PostScript interpreter remains the right building block.
  • SupportedFormats lets applications that produce PDF themselves – Microsoft mentions Edge – pass their PDF through unchanged. Then no conversion is needed at all.
  • OutputFileTypes turns the printer into a file printer; Windows then shows the Save As dialog itself. For handing the PDF to a target application, as miPDFconvert does, you leave the attribute out and process the data in the app.

One question is still open for me: how to hand the PDF to an arbitrary desktop application most cleanly from the isolated background task of an app. I will settle that before committing to an architecture.

What this means for miPDFconvert

The classic printer stays for Windows 10 and for systems without Protected Print Mode. Alongside it comes an edition of miPDFconvert as a virtual printer based on the new model for Windows 11 24H2 and later – with PostScript as the input format, conversion by miPDFlib and PDF passthrough for applications that deliver PDF themselves. Both are on the project's roadmap.

Try it yourself

miPDFconvert is open source (Apache-2.0) and free:

winget install mipdfconvert
# or
choco install mipdfconvert
Enter fullscreen mode Exit fullscreen mode

GitHub logo Mibuw / miPDFconvert

Virtual PDF printer for Windows (Apache-2.0) - print to PDF and hand it to a configurable target application. Converts with miPDFlib, no Ghostscript needed.

miPDFconvert logo

miPDFconvert

GitHub release License: Apache 2.0 winget Chocolatey

A virtual PDF printer for Windows. Install it and a new printer named miPDFconvert appears in Windows. Anything you print to it is converted to a PDF, which is then either handed to a configurable target application or offered to you through a Save As dialog.

The PDF conversion uses miPDFlib, a PDF library with its own PostScript interpreter in managed code — no Ghostscript or other external engine is needed miPDFconvert is released under the Apache License 2.0 — see Licensing.

How it works

Application  ──►  "miPDFconvert" printer  ──►  print monitor (miPDFport.dll)
                  (Microsoft PostScript class        │  writes the PostScript job to a file
                   driver + miPDFconvert PPD)        │  only the printing user can read
                                                     ▼
                              miPDFconvert (started in the user's session)
                                                     │  PostScript ──► PDF with miPDFlib
                                                     │
                        ┌────────────────────────────┴────────────────────────────┐
                        ▼                                                         ▼
        TARGET_APPLICATION is set:                               TARGET_APPLICATION is empty
        the PDF is passed to that app                            a "Save
…

If you want to convert PostScript or EPS files in your own .NET application, the PostScript guide of miPDFlib covers all the options:

GitHub logo Mibuw / miPDFlib

Open-source PDF library for .NET 8/10 (Apache-2.0): create, edit, merge, forms, text extraction, redaction, encryption, PAdES signatures (B-B to B-LTA), PDF/A, PDF/UA, ZUGFeRD, barcodes – no native dependencies

miPDFlib logo

miPDFlib

Open-source PDF library for .NET – create, edit, sign and validate PDFs without native dependencies.

NuGet NuGet downloads License CI .NET 8 | 10

On NuGet: dotnet add package miPDFlib – optional packages: miPDFlib.Rendering (pages to images) and miPDFlib.PostScript (PostScript and EPS to PDF)

An open-source (Apache-2.0) PDF library for .NET 8 / .NET 10 – pure managed code, no native dependencies Create, read, modify, merge, fill forms, extract text, encrypt and digitally sign (PAdES) PDF documents.

Why miPDFlib?

  • Apache-2.0: free in closed commercial products – no AGPL obligations, no revenue limits, no licence key.
  • eIDAS-ready signatures: PAdES B-B to B-LTA, HSM/cloud signers and two-step signing, built on .NET cryptography without BouncyCastle.
  • Archiving and e-invoices: PDF/A, PDF/UA and ZUGFeRD / Factur-X / XRechnung hybrid invoices.
  • Coming from iText or Syncfusion? See the comparison (also with PDFBox, PdfPig, PDFsharp/MigraDoc and QuestPDF) and the migration guide.

Status: 1.5.2 (stable) – all planned feature areas are implemented and tested (743…

If a print job comes out wrong, an issue with the PostScript file is the fastest way to a fix – ideally printed through the driver into a file, as shown above.

Top comments (0)