Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

UPDATE: 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.

share|improve this question
    
You should use for ONELINE in * instead of ls | while read ONELINE. Read this. –  paraxor Jun 8 '13 at 19:48

6 Answers 6

up vote 8 down vote accepted

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.

share|improve this answer
1  
thanks.: pastebin.com/raw.php?i=XQnRjtnR –  LanceBaynes Jul 26 '11 at 8:11
1  
doesn anybody has UTF-8 support for enscript? –  LanceBaynes Jul 26 '11 at 8:12
1  
@LanceBaynes Cedilla, or gnome-u2ps, or u2ps. –  Gilles Jul 26 '11 at 20:42

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
share|improve this answer
    
A thing to note: enscript does not support utf-8. –  maxschlepzig Aug 16 at 13:51

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
share|improve this answer
    
Looks nice, but the massive amount of haskell dependencies pandoc wants scared me away :/ –  lkraav Jun 28 '13 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). –  evilsoup Jun 28 '13 at 11:52

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
share|improve this answer

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

share|improve this answer
    
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

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.

share|improve this answer
    
Interesting. Didn't know about unoconv and didn't even know OO had APIs. –  Faheem Mitha Jul 28 '11 at 13:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.