gdi2ps Linux Toolkit
A-Shell GDI printing on Linux · prototype

gdi2ps Linux Toolkit

Converts A-Shell GDI print files (//TEXTOUT, //SETFONT, //RECTANGLE, //PDFX,… and plain text) into PostScript and finished PDFs on Linux, without Win32 GDI. Output is matched against PDF-XChange and Microsoft Print to PDF on Windows.

4 files, Python 3 standard library onlyGhostscript + qpdfbranch gdi-linux-prototype

How it fits together

print file//GDI + //PDFX directives and plain text
gdi2ps.py+ gdi_prolog.ps, pdfx.py → PostScript
gs pdfwriterenders the page; pdfmarks become bookmarks, links, info
qpdfoverlay, append, encryption (gdi2pdf.py)

Use gdi2ps.py alone when you just need PostScript (for lp/CUPS or your own Ghostscript call). Use gdi2pdf.py for a finished PDF that honours all supported //PDFX settings.

The modules

FileRole
gdi2ps.pyTranslator: print file → PostScript on stdout. Parses each // directive and plain-text line and emits calls to the prolog. Handles symbolic parameters (FIXED_PITCH|FF_MODERN, bold, PS_DOT), fonts (GDI cell heights, SETFONT widths, text metrics), the driver page model (--printer, //PDFX,Paper.*), the printer ini/pqi file (--pqi), //IMAGE (JPEG embedded directly, PNG via a built-in decoder) and Windows-1252 text.
gdi_prolog.psPostScript procedures copied into the front of every output file. They do the work at render time in Ghostscript: placing the device page on the sheet, map-mode and origin/offset conversion, text measurement and alignment, plain-text flow and line spacing, TEXTINDENT, both TEXTRECTANGLE forms, pens, rectangles, ellipses, images, and the per-page hooks used by watermarks.
pdfx.pyLibrary used by gdi2ps.py (not run directly). Collects //PDFX settings, accepting PDFXv3 names, and produces the in-document features as pdfmarks: DocInfo, bookmarks, link annotations and text watermarks. Save, Overlay and Security settings are collected here for gdi2pdf.py.
gdi2pdf.pyFull pipeline to a finished PDF. Runs the translator in-process, renders with Ghostscript, then applies Overlay, WhenExists Append/InsertBefore and Security with qpdf, and finally the Save naming rules (FullFileName, macros, AutoNumber). Prints the path of the PDF it wrote.

gdi2ps.py options

python3 gdi2ps.py [options] infile > out.ps
OptionMeaning
infileThe GDI print file.
--printer {pdfx,msprint}Windows driver whose page geometry to reproduce. pdfx (default) is PDF-XChange: an A4 device page (595 × 842 pt) placed on the Letter sheet per //PDFX,Paper.*; with the default Fit that is a 0.9406 scale. msprint is Microsoft Print to PDF: Letter with a 0.25″ physical margin.
--pqi FILEA-Shell printer ini/pqi file. Honours LPP, CPP (including the first-15-lines rule), PITCH=AUTO, FONT, FONTHEIGHT, XORIGIN, YORIGIN, WEIGHT and ORIENTATION. TMARGIN, LMARGIN and FONTWIDTH only produce a warning.
--imgdir DIRExtra directory to search for //IMAGE files (repeatable). The current directory is always searched.
--fonts {auto,liberation,urw}Font set. auto uses Liberation (metric clones of Arial, Times New Roman, Courier New) when installed, otherwise the URW base-35 fonts.
--page W HOutput sheet size in points. Default 612 792 (Letter).
--device-page W HOverride the page size the driver reports, in points.
--margin X YOverride the driver's physical margin, in points.
--scale SExtra scale factor, for calibration only. Default 1.
--dpi NDevice resolution used for GDI pixel rounding and the pixel map mode. Default 600.

gdi2pdf.py options

python3 gdi2pdf.py [options] infile

Accepts every gdi2ps.py option above, plus:

OptionMeaning
--pathmap WIN=NATIVEMap a Windows path prefix used in //PDFX paths to a Linux path, e.g. --pathmap 'C:\vm=/home/me/vm' (repeatable).
--keepKeep the intermediate doc.ps and per-step PDFs in a temp directory and report where.
The output location comes from //PDFX,Save.*; without it the PDF is written next to the input with a .pdf extension. If that file exists, the PDFX default Save.WhenExists=ShowWarning refuses to overwrite it (there is no dialog on Linux): delete it, or use Overwrite or AutoNumber.

Rendering PostScript by hand

gs -q -dNOPAUSE -dBATCH -dALLOWPSTRANSPARENCY -sDEVICE=pdfwrite -o out.pdf out.ps

-dALLOWPSTRANSPARENCY is needed only for watermark opacity.

Linux packages

Ubuntu/Debian package names. The Python code uses only the standard library; nothing needs pip.

PackageProvidesNeeded
python3Runs the scriptsRequired preinstalled on Ubuntu
ghostscriptgs, renders PostScript to PDFRequired
fonts-urw-base35Standard PostScript fonts (Helvetica, Times, Courier equivalents)Required usually installed with ghostscript
fonts-liberationMetric-compatible Arial, Times New Roman, Courier NewRecommended used automatically when present
fontconfigfc-list; how Ghostscript finds the Liberation fontsRequired normally already installed
qpdfOverlay, Append/InsertBefore, encryptiongdi2pdf.py
poppler-utilspdfinfo, pdftotext, pdftoppm for inspecting outputOptional
cups-clientlp, to send PostScript straight to a printerOptional

Install everything at once

sudo apt install python3 ghostscript fonts-urw-base35 fonts-liberation fontconfig qpdf poppler-utils

Run the scripts as python3 gdi2ps.py …. If the files were copied from Windows, CRLF line endings stop ./gdi2ps.py from running via its #! line.

What is supported

GDI directives

  • SETMAPMODE, XORIGIN/YORIGIN, XOFFSET/YOFFSET (twips)
  • SETFONT (all 8 parameters), SETTEXTALIGN, SETTEXTCOLOR
  • TEXTOUT (−1 = current position), TEXTCENTER, TEXTINDENT
  • TEXTRECTANGLE, both syntaxes
  • SETPEN, SETPENEX, MOVETO, LINETO, RECTANGLE, ELLIPSE
  • IMAGE (JPEG, PNG), RESETLPP, SETVMI

//PDFX

  • DocInfo, explicit Bookmarks, Links
  • Watermarks.DefineText
  • Paper.Fit, Center, LeftPos, TopPos, PageScale
  • Overlay, Save, Security (gdi2pdf.py)
  • Not yet: Email, digital signatures, image watermarks, n-up/booklet

Line spacing follows A-Shell's CalcSpacing(): LPP-based spacing, or tmHeight + tmExternalLeading after RESETLPP, truncated to whole map-mode units. Test files match their Windows PDFs within about 2 pt vertically and 1% in character width.