Matt2907
Matt2907

Reputation: 39

Ghostscript txt file to pdf

Is it possible to convert txt file to pdf using Ghostscript? I already manage to make it working for jpg to pdf, pdf to txt, but I'm having a hard time to make it working for simple txt files..

So far I tried solutions like: gswin64.exe -dNOSAFER -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile="test.pdf" "test.txt"

but with no luck.

Thanks!

Upvotes: 3

Views: 4022

Answers (1)

K J
K J

Reputation: 11880

When you run one of the executable commands GS[win##][c] -h (= help)

It will reply

"Ghostscript is an interpreter for PostScript® and Portable Document Format (PDF) files."

and go on to describe options such as

Input formats: PostScript PostScriptLevel1 PostScriptLevel2 PostScriptLevel3 PDF

Thus we see it is designed to accept filename.ps or filename.pdf i.e. not filename.txt Thus the very short answer to your question would be NO!

However, there are many ways to feed text to Ghostscript since it is part of a larger package called GhostPDL which includes print drivers that can output to different formats, and we can use PDL printing to build multiformat converters.

An alternative method is to use a PostScript program to process the text file and there is an example described in How to modify this plaintext-to-PDF-converting PostScript from 1992 to actually specify a page size?

We could also build a specific Polyglot of the text with a PostScript programable header to process through Ghostscript e.g. Text.PS. But life is short...

The above methods are akin to simple printing a text file into a file.pdf. Thus the simplest way of all in Windows, using either the Ghostscript based print driver OR even more simply if the "Microsoft Print to PDF" driver, is default Right Click a TXT file and select Print. It should then flash through notepad as the default text handler.

At this point most windows users say but I want it automated with xyz formatting! Well Windows users have two inbuilt text handlers and WordPad is the more powerful one that allows for RichTextFormatting (RTF).

So at this 1st stage try

C:\Windows\NOTEPAD.EXE /pt test.txt "Microsoft Print to PDF"

You will normally be asked to provide the folder and filename and it is possible to set your own copy of the MS PDF driver to save to a fixed filename, so as to avoid the port output prompting.

BUT with WordPad we can more easily specify the output filename:-

"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE" /pt "c:\path\test.txt" "Microsoft Print to PDF" "Microsoft Print to PDF" "c:\path\test.pdf"

The input can be .txt and also .rtf, .doc, .docx, .odt etc. However, only files that can be viewed well in WordPad can print a reasonable PDF with styled text and images.

So in answer to the original question How to use Ghostscript to convert TXT to PDF? IF you installed the Hi Resolution 4000dpi Ghostscript Print driver you can use in just two lines:-

"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE" /pt "c:\path\test.txt" "Ghostscript PS" "Ghostscript PS" "c:\path\test.ps"
gswin64c.exe -dNOSAFER -sDEVICE=pdfwrite -o "c:\path\test.pdf" "c:\path\test.ps"

NOTE:- on the second line I use gswin64C.exe and with -o there is no need for batch or nopause and I retain your -dNOSAFER since I trust your .txt has no bombs.

BUT it is so much easier to use the 600dpi Microsoft inbuilt PDF driver with the one line command, especially if it is a %1 drag and drop shortcut and/or add it as a Right Click Context Verb for any txt/rtf/odt/doc(x) file.

Upvotes: 4

Related Questions