2

Hi any idea how I get this to work?
I have a .txt file and want to print it as an .pdf file.

$pdf_file = "C:\table.pdf"
Get-Content "C:\Projekt\erg\table.txt" | Format-Table | Out-String | & "C:\Program Files\gs\gs10.01.1\bin\gswin64c.exe" -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$pdf_file" -

I get this error message:

Error: /undefined in NameOperand stack:

Execution stack:%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1990 1 3 %oparray_pop 1989 1 3 %oparray_pop 1977 1 3 %oparray_pop 1833 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval--Dictionary stack:--dict:770/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)--Current allocation mode is localLast OS error: No such file or directoryGPL Ghostscript 10.01.1: Unrecoverable error, exit code 1

I have an easy bash script that worked with ghostscript, but need this script in powershell.

In bash it worked with

pr -t -e20 table.txt | enscript -B -o - | ps2pdf - "$pdf_file"
zx485
  • 28,498
  • 28
  • 50
  • 59
ZoniK
  • 31
  • 2
  • 1
    Neither `pr` nor `enscript` have counterparts in PowerShell. `pr -t -e20 table.txt | enscript -B -o -` produces PostScript output, whereas `Format-Table | Out-String` does not - it produces _text_ output meant for to-display output. – mklement0 Jun 13 '23 at 03:43

1 Answers1

1

I think you are under a misapprehension that since on unix enscript will produce good results converting console.TXT to PDF that it may work similar in Windows using PS.

Consider we have this text table

+-------+
| Hello |
+-------+
| World |
+-------+

You may think GS could convert that as a PDF too via P.S. on Windows, however the best run may well produce this result

enter image description here

Now I could spend time explaining that that is due to differences between Dos legacy thus Windows, Linux and Mac, but it will not change the poor result. The topic has been covered many times that GhostScript Maintainers Rep state that on Windows this is not the way to try PostScript convert TXT to PDF:-

The easy way to turn a 'plaintext document' into PDF is to open the document in your favourite text editor and then 'save as PDF' or 'Print to PDF' from there. It's far more reliable than trying to use an ancient PostScript program which (as is clearly demonstrated by the fact it doesn't work for you) is lacking in features. Recent versions of Linux, Windows and Mac all have this capability and avoids the kind of problems you are seeing. https://stackoverflow.com/a/59963011/10802527

Windows has two inbuilt TXT handlers and both can convert TXT to PDF different ways. Native NotePad and WordPad can be programmed via registry settings to use fonts and page layout etc. Such that we can simply command:-

write /pt "C:\Projekt\erg\table.txt" "Microsoft Print to PDF" "Microsoft Print to PDF" "C:\table.pdf"

Or from PowerShell Prompt

PS C:\> cmd /r write /pt 'C:\Projekt\erg\table.txt' 'Microsoft Print to PDF' 'Microsoft Print to PDF' 'C:\table.pdf'
PS C:\> Invoke-Item "C:\table.pdf"

enter image description here

There is a different older enscript variant for windows called nenscript and we could combine that with windows abilities to prep the PostScript and run through GhostScript perhaps in this fashion but there are many posibilities enter image description here

PStext.cmd

@echo off
more %~f0 +4 >"%temp%\tmp.txt" &&nenscript -TA4 -B -p- "%temp%\tmp.txt" >"%temp%\tmp.ps"
gswin32c.lnk -sDEVICE=pdfwrite -o"%temp%\tmp.pdf" -f "%temp%\tmp.ps"
pause && "%temp%\tmp.pdf" && exit /B
+-------+
| Hello |
+-------+
| World |
+-------+

However Nenscript is much more limited than older Enscript for Windows as proposed in other answer to similar question with using Tabs.
https://stackoverflow.com/a/76503311/10802527

K J
  • 8,045
  • 3
  • 14
  • 36
  • 1
    I missed a lot of angles originally, and I've removed the comments that no longer apply (as well as my answer). I appreciate that you updated the answer with PowerShell syntax. Note that you don't need `cmd /r` - just invoke `write /pt ...` directly. – mklement0 Jun 13 '23 at 03:46