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.
How it fits together
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
| File | Role |
|---|---|
gdi2ps.py | Translator: 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.ps | PostScript 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.py | Library 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.py | Full 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
| Option | Meaning |
|---|---|
infile | The 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 FILE | A-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 DIR | Extra 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 H | Output sheet size in points. Default 612 792 (Letter). |
--device-page W H | Override the page size the driver reports, in points. |
--margin X Y | Override the driver's physical margin, in points. |
--scale S | Extra scale factor, for calibration only. Default 1. |
--dpi N | Device 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:
| Option | Meaning |
|---|---|
--pathmap WIN=NATIVE | Map a Windows path prefix used in //PDFX paths to a Linux path, e.g. --pathmap 'C:\vm=/home/me/vm' (repeatable). |
--keep | Keep the intermediate doc.ps and per-step PDFs in a temp directory and report where. |
//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.
| Package | Provides | Needed |
|---|---|---|
python3 | Runs the scripts | Required preinstalled on Ubuntu |
ghostscript | gs, renders PostScript to PDF | Required |
fonts-urw-base35 | Standard PostScript fonts (Helvetica, Times, Courier equivalents) | Required usually installed with ghostscript |
fonts-liberation | Metric-compatible Arial, Times New Roman, Courier New | Recommended used automatically when present |
fontconfig | fc-list; how Ghostscript finds the Liberation fonts | Required normally already installed |
qpdf | Overlay, Append/InsertBefore, encryption | gdi2pdf.py |
poppler-utils | pdfinfo, pdftotext, pdftoppm for inspecting output | Optional |
cups-client | lp, to send PostScript straight to a printer | Optional |
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,SETTEXTCOLORTEXTOUT(−1 = current position),TEXTCENTER,TEXTINDENTTEXTRECTANGLE, both syntaxesSETPEN,SETPENEX,MOVETO,LINETO,RECTANGLE,ELLIPSEIMAGE(JPEG, PNG),RESETLPP,SETVMI
//PDFX
DocInfo, explicitBookmarks,LinksWatermarks.DefineTextPaper.Fit,Center,LeftPos,TopPos,PageScaleOverlay,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.