This message is based on a recent log entry of mine.  It describes
how to print to PDF on FreeDOS.  In my example, i take a
screenshot and print it using the COPY command.  However, the
method should work with most DOS programs that can print to an
Epson printer.  All of the utilities used in this post include source
except for Ladybug LOGO, which is what i took a screenshot of, and
it is merely freeware.  You can fetch the gopher links using either
curl or lynx.

-Ben

# Printing to PDF on FreeDOS

I wanted to print from DOS applications, but i no longer have a
hardware printer.  After some tinkering, i came up with the following
solution in FreeDOS.  Note that this process does not work in dosbox
nor in dosbox-x.

The first thing i learned is that the most common type of printer for
DOS is Epson Esc/P.  See the link below for a detailed reference to
Esc/P printer language.

* Esc/P Reference
  https://files.support.epson.com/pdf/general/escp2ref.pdf

First, the ingredients for this recipe:

* Ghostscript (FreeDOS)
  
http://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/util/unix/ghostscript/8.71/gs871.7z
* Ladybug LOGO (Simtelnet)
  http://cd.textfiles.com/simtel/simtel20/MSDOS/LOGO/LADYBUG.ZIP
* NetPBM (DJGPP)
  http://www.delorie.com/pub/djgpp/current/v2apps/npbm916b.zip
* PRN2FILE (Garbo)
  http://cd.textfiles.com/garbo/PC/PRINTER/PRN2FIL3.ZIP
* SNARF screen shot utility
  http://pixelmetrics.com/Snarf/Snarf.zip
* EscParse Esc/P to PDF/PS/SVG converter by Nikita Zimin
  gopher://tilde.pink/1/~bencollver/files/dos/util/escparse/escparse.zip
* Filter to fix Epson printer graphics output by NetPBM
  gopher://tilde.pink/1/~bencollver/files/dos/util/escpfltr/escpfltr.zip

For this example, i chose to use Ladybug LOGO, which i described in
an earlier log entry:

* Ladybug Quest
  gopher://tilde.pink/1/~bencollver/log/2018-01-27-ladybug-quest/

I unzipped all of the "ingredients" and made sure they were in my
path.

I saved a simple LOGO program called flwrlife.bug:
```
C:\>cd \ladybug
C:\ladybug>copy con flwrlife.bug
to circle :size :angle
repeat ( 360 / :angle ) [ fd :size rt :angle ]
end
home cs ht rt 90 repeat 6 [ circle 1 1 rt 60 ]
^Z
```

I ran the snarf screen shot utility, then ran the LOGO program:

```
C:\ladybug>snarf.exe
C:\ladybug>ladybug.exe
? load flwrlife.bug
```

I pressed Alt+S to take a screenshot.

* Ladybug flower of life screenshot
  
gopher://tilde.pink/1/~bencollver/log/2022-06-15-print-quest/ladybug-screenshot.png

I quit LOGO:

```
? bye
```

I converted the screenshot to Esc/P data using NetPBM.  By the way,
when i run NetPBM's Epson graphics through a printer emulator, the
graphical output is staggered and incoherent.  For this reason i
wrote escpfltr to add a carriage return before each line of graphics
and unstagger them.

```
C:\ladybug>bmptoppm.exe SNARF000.BMP >flwrlife.pnm
bmptoppm.exe: Windows BMP, 320x200x4
C:\ladybug>ppmtopgm.exe flwrlife.pnm >flwrlife.pgm
C:\ladybug>pgmtopbm.exe flwrlife.pgm >flwrlife.pbm
C:\ladybug>pnmmerge.exe pnminvert flwrlife.pbm >inverted.pbm
C:\ladybug>pbmmerge.exe pbmtoepson inverted.pbm >inverted.esc
C:\ladybug>escpfltr.com <inverted.esc >flwrlife.esc
```

I found an old utility named prn2file that captures the print job
being sent to the printer.  It works even when there is no printer
attached.  I used options to save to C:\print.out using a 64KB
buffer.  Note that prn2file does not work in dosbox nor in dosbox-x.

```
C:\ladybug>prn2file.com /b64 /f C:\print.out
 PRN2FILE 1.1 (c) 1987 Ziff Communications Co.
Modifications (c) 1991 Automated Answers
LPT1 Redirected to: C:\print.out
```

Then i used the COPY command to "print" the screenshot.  It is
important to pass the /b option.  This uses an ioctl() to set binary
mode.  Otherwise, DOS will truncate the print data the first time it
sees a Ctrl-Z (ASCII character 26, SUB).  I used prn2file to ensure
that the whole print job was flushed to disk.

```
C:\ladybug>copy /b flwrlife.esc prn
C:\ladybug>prn2file.com
 PRN2FILE 1.1 (c) 1987 Ziff Communications Co.
Modifications (c) 1991 Automated Answers
LPT1 Redirected to: PRN
```

Then i compared the captured print job data to the original file.

```
C:\ladybug>comp flwrlife.esc \print.out
Files compare OK.
```

What can i do with these captured print jobs on DOS?  The "escparse"
utility can convert Esc/P to PDF.  I could copy the PDF to a thumb
drive and print it from another computer.  Because the DOS PDF
viewers don't work well for me, i'll also convert the PDF to JPEG
page images to preview using the links browser.

```
C:\ladybug>escparse.exe -pdf C:\print.out >flwrlife.pdf
ESCParser utility  by Nikita Zimin  Jun 15 2022 17:08:37
Pages total: 2
Page 2
C:\ladybug>gs.exe -r72x72 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sDEVICE=jpeg 
-dJPEGQ=90 -o page-%03d.jpg flwrlife.pdf
GPL Ghostscript 8.71 (2010-02-10)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 2.
Page 1
Page 2
C:\ladybug>c:\freedos\net\links\links.exe -g -mode 640x480x64k page-001.jpg
```

* Screenshot of page image in links browser
  
gopher://tilde.pink/1/~bencollver/log/2022-06-15-print-quest/links-screenshot.jpg

Now that was a pretty convoluted process but it has a few advantages.

* This will run on bare metal.  It does not require any VM software.
* This generally works with DOS apps that use an Epson printer.


_______________________________________________
Freedos-user mailing list
Freedos-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/freedos-user

Reply via email to