Convert TXT to PDF using ghostscript with PowerShell

huangapple go评论51阅读模式
英文:

Convert TXT to PDF using ghostscript with PowerShell

问题

以下是翻译好的部分:

你有任何关于如何让这个工作的想法吗?
我有一个.txt文件,并想将其打印为.pdf文件。

我收到以下错误消息:

> 错误:NameOperand栈中未定义
> 执行栈:%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--

字典栈:--dict:770/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)--
当前分配模式为本地
上一个操作系统错误:没有此文件或目录
GPL Ghostscript 10.01.1:无法恢复的错误,退出代码1

我有一个简单的bash脚本,使用Ghostscript可以工作,但需要将这个脚本转换成PowerShell。

在bash中,它可以使用以下方式工作:

pr -t -e20 table.txt | enscript -B -o - | ps2pdf - "$pdf_file"
英文:

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"

答案1

得分: 1

我认为你对于在Unix上使用enscriptconsole.TXT转换为PDF效果良好,可能以为在Windows上使用PS也能类似工作存在误解。

考虑到我们有这个文本表格:

+-------+
| 你好  |
+-------+
| 世界  |
+-------+

你可能认为GS可以通过在Windows上使用P.S.将其转换为PDF,然而最好的运行结果可能会产生这种结果:

Convert TXT to PDF using ghostscript with PowerShell

现在我可以花时间解释这是由于Dos遗留与Windows、Linux和Mac之间的差异造成的,但这不会改变糟糕的结果。这个话题已经讨论了很多次,GhostScript维护者表示,在Windows上,这不是尝试将TXT转换为PDF的方法:

将'纯文本文档'转换为PDF的简单方法是在您喜欢的文本编辑器中打开文档,然后从那里'另存为PDF'或'打印为PDF'。这比尝试使用古老的PostScript程序要可靠得多(正如它明显不能为您工作所示),它在功能上有所欠缺。最近的Linux、Windows和Mac版本都具备这个功能,避免了您所看到的问题。https://stackoverflow.com/a/59963011/10802527

Windows有两个内置的TXT处理程序,都可以以不同的方式将TXT转换为PDF。本机的NotePad和WordPad可以通过注册表设置来编程,以使用字体和页面布局等功能。这样我们可以简单地执行以下命令:

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

或者从PowerShell提示符中执行:

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"

Convert TXT to PDF using ghostscript with PowerShell

在Windows上有一个名为nenscript的不同的旧版enscript变体,我们可以将其与Windows的能力结合起来准备PostScript并通过GhostScript运行,也许可以这样做,但有很多可能性:

Convert TXT to PDF using ghostscript with PowerShell

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
+-------+
| 你好  |
+-------+
| 世界  |
+-------+

然而,与在其他回答中提到的在Windows中提出的旧版Enscript相比,Nenscript的功能要有限得多,尤其是在使用制表符时:https://stackoverflow.com/a/76503311/10802527

英文:

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

Convert TXT to PDF using ghostscript with PowerShell

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"

Convert TXT to PDF using ghostscript with PowerShell

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
Convert TXT to PDF using ghostscript with PowerShell

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

huangapple
  • 本文由 发表于 2023年6月13日 03:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定