How to convert TXT to PDF?

But this produces one "error" -- if there's a very long line in the text file, it doesn't get wrapped.

Input text

Screenshot of the input file

Output PDF

Screenshot of the output PDF

-- Also, it would also be great if the output PDF could contain text, instead of images of text. I have many-many-many TXT files. So don't want to do it by hand. I need an automatic solution, like the one I mentioned above.

67.8k 35 35 gold badges 119 119 silver badges 257 257 bronze badges asked Jul 26, 2011 at 6:03 LanceBaynes LanceBaynes 40.7k 97 97 gold badges 255 255 silver badges 352 352 bronze badges You should use for ONELINE in * instead of ls | while read ONELINE . Read this. Commented Jun 8, 2013 at 19:48

I don't have enough rep to answer, but i just want to add that the Kate editor in KDE kubuntu you can print to PDF "printer" which lets you select filename and it produces a .pdf.

Commented Sep 8, 2018 at 22:23 Commented Feb 18, 2022 at 0:17

12 Answers 12

pandoc can do this. It's more focused on converting marked-up text to various formats, but it should have no problems with simple plaintext.

pandoc input.txt -o output.pdf 
answered Apr 5, 2013 at 22:41 6,867 3 3 gold badges 34 34 silver badges 40 40 bronze badges Looks nice, but the massive amount of haskell dependencies pandoc wants scared me away :/ Commented Jun 28, 2013 at 11:46

@Ikraav yeah, it's way overkill for this purpose, but if you have markdown or HTML (or any of the other markups it can accept as input) pandoc would be the way to go. PDF actually requires even more dependencies -- internally, pandoc uses LaTeX to convert to PDF, so you need to install that stuff too -- but the quality is very good (I use it to convert markdown'd text to PDF and EPUB, primarily).

Commented Jun 28, 2013 at 11:52 pandoc (1.16.0.2) replaces my newlines with tabs. Commented Jan 26, 2016 at 2:34 Pandoc will default to Markdown. For a general plain-text this will fail. Commented May 24, 2021 at 12:24 This removes new-lines and dramatically changes formatting. Commented Jan 8, 2022 at 18:21

You can print text to a PostScript file using Vim and then convert it to a PDF, as long as Vim was compiled with the +postscript feature.

For this you use the :hardcopy > command. For example you can open example.txt and execute

:hardcopy > example.ps 

which will produce a file example.ps containing all the text in example.txt . The header of each page in the PostScript file will contain the original filename and the page number.

Then you can convert the PostScript file into a PDF by using the following command

ps2pdf example.ps 

which will create example.pdf .

You can do the same directly from a terminal (without interacting with Vim) by using the following command

vim example.txt -c "hardcopy > example.ps | q"; ps2pdf example.ps 

This opens example.txt in Vim and executes the command passed to the -c option, which in this case is a hardcopy command followed by a quit ( q ) command. Then it executes ps2pdf to produce the final file.

For more options see the help files with :help :hardcopy .

answered Apr 17, 2016 at 1:05 Gonçalo Ribeiro Gonçalo Ribeiro 421 4 4 silver badges 7 7 bronze badges

:hardcopy produces a PostScript file no matter I add .pdf or .ps extensions. I did :hardcpy > example.pdf and with less example.pdf in shell I could see that the file header was %!PS-Adobe-3.0 .

Commented Jul 5, 2017 at 10:31

@taro, you are correct. At the time I wrote this I didn't notice that. I updated my answer. The final command could be improved, but I can't write that right now. Might do it later.

Commented Jul 6, 2017 at 23:04 Note that this does not support UTF-8. Looks like a call to enscript . Commented Sep 2, 2021 at 16:49 Commented Feb 18, 2022 at 1:03

One method is to use CUPS and the PDF psuedo-printer to "print" the text to a PDF file.

Another is to use enscript to encode to postscript and then convert from postscript to PDF using the ps2pdf file from ghostscript package.

answered Jul 26, 2011 at 6:53 7,954 1 1 gold badge 28 28 silver badges 29 29 bronze badges Commented Jul 26, 2011 at 8:11 doesn anybody has UTF-8 support for enscript? Commented Jul 26, 2011 at 8:12 @LanceBaynes Cedilla, or gnome-u2ps, or u2ps. Commented Jul 26, 2011 at 20:42

LibreOffice / OpenOffice as well as most other word processors (Abiword) can do this quite easily.

There is a little utility called unoconv that uses the LibreOffice code base to do file format conversions on the command line. It can read and write any combination of formats that LibreOffice can and makes it very easy to do things like doc to pdf conversions on the command line. Simple txt to pdf would be easy for it.

answered Jul 26, 2011 at 7:55 70.7k 18 18 gold badges 198 198 silver badges 228 228 bronze badges Interesting. Didn't know about unoconv and didn't even know OO had APIs. Commented Jul 28, 2011 at 13:45

Produces high quality results, but, for me (I'm using version 0.5-1 (Debian Wheezy)), I have to first run a unoconv --listener & command.

Commented Jan 28, 2019 at 22:11 Any example commands you can provide? Commented Jan 8, 2022 at 18:21 It'd be great if unoconv had a way to convert links (URL) to clickable. Commented Sep 3 at 21:58

@pevik Use different tools. Use Pandoc and a filter to detect URLs and convert them to links and output to a different format.

Commented 2 days ago

LibreOffice works for this. Usage:

libreoffice --convert-to "pdf" file.txt

The output will be called file.pdf .

answered Feb 1, 2018 at 19:29 1,171 1 1 gold badge 15 15 silver badges 24 24 bronze badges Is there a way to change font size in the resulting PDF? Commented Jun 20, 2019 at 16:44 @Evg I do not know of a way to do so with this method of using LibreOffice. Commented Jun 20, 2019 at 17:01 this only works for me Commented Aug 7, 2020 at 8:33 quicker: unoconv file.txt . calling the same libreoffice. Commented Sep 2, 2021 at 16:52 Thanks. I did not know about that. I just tried it, and it works very nicely. Commented Sep 2, 2021 at 22:06

Just use the text2pdf , which is free and opensource. At the link you can download the source or the pre-compiled binary for windows, solaris, dos.

I'm able to use it into AIX OS without problem. Very simple to compile , just save the text2pdf.c and Makefile into the same directory and type make . (here I set the variable CC=gcc on AIX, on linux this will not be an issue)

$ ./text2pdf -h text2pdf [options] [filename] text2pdf makes a 7-bit clean PDF file (version 1.1) from any input file. It reads from standard input or a named file, and writes the PDF file to standard output. There are various options as follows: -h show this message -f use PostScript (must be in standard 14, default: Courier) -I use ISOLatin1Encoding -s use font at given pointsize (default 10) -v use given line spacing (default 12 points) -l lines per page (default 60, determined automatically if unspecified) -c maximum characters per line (default 80) -t spaces per tab character (default 8) -F ignore formfeed characters (^L) -A4 use A4 paper (default Letter) -A3 use A3 paper (default Letter) -x independent paper width in points -y independent paper height in points -2 format in 2 columns -L landscape mode Note that where one variable is implied by two options, the second option takes precedence for that variable. (e.g. -A4 -y500) In landscape mode, page width and height are simply swapped over before formatting, no matter how or when they were defined. text2pdf v1.1 (c) Phil Smith, 1996 $ ./text2pdf -f"Courier" -s6 -c216 -v6 -L -A4 ./rep3.txt >rep3.pdf