76

I want to convert .txt files to .pdf. I'm using this:

ls | while read ONELINE; do convert -density 400 "$ONELINE" "$(echo "$ONELINE" | sed 's/.txt/.pdf/g')"; done

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
LanceBaynes
  • 40,135
  • 97
  • 255
  • 351

12 Answers12

58

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
evilsoup
  • 6,807
  • 3
  • 34
  • 40
  • 7
    Looks nice, but the massive amount of haskell dependencies pandoc wants scared me away :/ – lkraav Jun 28 '13 at 11:46
  • 1
    @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). – evilsoup Jun 28 '13 at 11:52
  • 1
    pandoc (1.16.0.2) replaces my newlines with tabs. – Sparhawk Jan 26 '16 at 02:34
  • 7
    Pandoc will default to Markdown. For a general plain-text this will fail. – s.ouchene May 24 '21 at 12:24
  • 5
    This removes new-lines and dramatically changes formatting. – Gabriel Staples Jan 08 '22 at 18:21
  • Completely useless results. It squeezed things in a column of 10 chars wide?! And all newlines were removed. – Carlo Wood May 17 '22 at 12:18
  • @CarloWood, here (Fedora 37, pandoc-2.14.0.3-18.fc37.x86_64) I get no such results. – vonbrand Nov 24 '22 at 10:40
32

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 > {filename} 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.

27

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.

Keith
  • 7,914
  • 1
  • 28
  • 29
22

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.

Caleb
  • 70,105
20

LibreOffice works for this. Usage:

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

The output will be called file.pdf.

jbrock
  • 1,141
13

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<font>      use PostScript <font> (must be in standard 14, default: Courier)
  -I            use ISOLatin1Encoding
  -s<size>      use font at given pointsize (default 10)
  -v<dist>      use given line spacing (default 12 points)
  -l<lines>     lines per page (default 60, determined automatically
                if unspecified)
  -c<chars>     maximum characters per line (default 80)
  -t<spaces>    spaces per tab character (default 8)
  -F            ignore formfeed characters (^L)
  -A4           use A4 paper (default Letter)
  -A3           use A3 paper (default Letter)
  -x<width>     independent paper width in points
  -y<height>    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
ceinmart
  • 501
  • 6
    is there an utf8 capable fork of this? – Wolfgang Fahl Jun 04 '15 at 02:42
  • @WolfgangFahl Alas, this program is not legally forkable "as is", as the file header says: "You may not alter the source in any way [...] without the permission of the author, Phil Smith phil@bagobytes.co.uk" – Totor Nov 15 '22 at 17:44
  • @WolfgangFahl Alas, this program is not legally forkable "as is", as the file header says: "You may not alter the source in any way [...] without the permission of the author, Phil Smith phil@bagobytes.co.uk". This program is compiling & working on my Linux, but is from 1996 and generates PDF file version 1.1. It is fast though. – Totor Nov 15 '22 at 17:58
  • For UTF-8 encoding, I am going to use puppeteer for Node.js. – Lance Feb 11 '24 at 04:01
11

There is also a UTF-8 to PostScript converter called paps.

Caleb
  • 70,105
tunit
  • 111
  • 4
    It works, and with TrueType fonts, but it should be noted that it produces a document with bitmaps as a result, instead of using the fonts natively. (I guess that's because of Postscript?) – njsg Jan 29 '13 at 22:08
  • Couldn't compile it on Cygwin32 on my Windoze... – jarnosc Feb 18 '16 at 23:03
  • Not too simple, not too complicated. It will read from stdin, for example pr -e3 -t test.txt | paps – PJ Brunet Apr 30 '23 at 04:51
8

Use enscript to created a .ps file, and then ps2pdf (or ps2pdfwr) to convert to .pdf

The following script creates a .pdf file with 10 pt left and right margins, and uses a courier font that is 7.3 pts wide and 10 pts high, so a 132 col printout fits on an 8 1/2 X 11 page. Use enscript to setup your page, fonts, etc.

$ enscript -B --margins=10:10: -o outputfile.ps -f Courier@7.3/10 inputfile
$ ps2pdfwr outputfile.ps newfile.pdf
$ rm outputfile.ps
slm
  • 369,824
Dan Walker
  • 81
  • 1
  • 1
3

Since it's included by default in many distributions, a shorter way to call libreoffice from the cli:

soffice --convert-to pdf input.txt

Supported extensions by soffice, we can convert from/to:

ODF Spreadsheet                   .ods
ODF Spreadsheet Template          .ots
Flat XML ODF Spreadsheet          .fods
Unified Office Format Spreadsheet .uos
Microsoft Excel 2007-2013 XML     .xlsx
Microsoft Excel 2003 XML          .xml
Microsoft Excel 97-2003           .xls
Microsoft Excel 97-2003 Template  .xlt
Data Interchange Format           .dif
dBase                             .dbf
HTML Document (Calc)              .html
SYLK                              .slk
Text CSV                          .csv
Office Open XML Spreadsheet       .xlsx
Microsoft Excel 2007-2016 XML     .xlsm
NVRM
  • 398
3

I am adding this a2ps as another alternative. a2ps produces postscript file, then could be converted into pdf using ps2pdf. Both a2ps and ps2pdf should be in the major Linux distros repository.

a2ps input.txt -o output.ps
ps2pdf output.ps output.pdf
Kheng
  • 41
1

Easiest way using calibre

  • install (on .deb-based distributions):
    sudo apt-get install calibre
    
  • then run:
    ebook-convert /path/to/input.txt path/to/ouput.pdf
    
jahly
  • 33
  • 1
    My apologies, what I meant with "*" is any name, but now I get it this wildcard could be translated to multiple inputs, I'm going to edit it now. – jahly Apr 23 '23 at 05:48
  • Ok, then I understand the original intention. The edit really helps to clarify! – AdminBee Apr 24 '23 at 06:45
0

It is also possible to wrap the text from the input text file into a helper Latex document:

\documentclass{letter}
\begin{document}
\input{input.txt}  % text file with the input text
\end{document}

and then compile it directly to PDF using pdflatex:

$ pdflatex helper.tex