You are on page 1of 3

01/12/2016

text processing - How to convert TXT to PDF? - Unix & Linux Stack Exchange

signup

Unix&LinuxStackExchangeisa
questionandanswersiteforusersof
Linux,FreeBSDandotherUn*xlike
operatingsystems.Jointhemitonly
takesaminute:

login

tour

Here'showitworks:

Anybodycanask
aquestion

Signup

help

Anybodycan
answer

Thebestanswersarevoted
upandrisetothetop

HowtoconvertTXTtoPDF?
Iwanttoconvert

.txt

filesto

.pdf

.I'musingthis:

ls | while read ONELINE; do convert -density 400 "$ONELINE" "$(echo "$ONELINE" |


sed 's/.txt/.pdf/g')"; done

Butthisproducesone"error"ifthere'saverylonglineinthetextfile,itdoesn'tgetwrapped.

Inputtext

OutputPDF

Also,itwouldalsobegreatiftheoutputPDFcouldcontaintext,insteadofimagesoftext.
UPDATE:IhavemanymanymanyTXTfiles.Sodon'twanttodoitbyhand.Ineedanautomaticsolution,liketheoneimentionedabove.
/textprocessing

/pdf /imagemagick /conversion


editedJul28'11at10:43

askedJul26'11at6:03

Caleb
39.7k

LanceBaynes
6

107

148

6,257

51

159

284

Youshoulduse for ONELINE in * insteadof ls | while read ONELINE .Readthis.EvanTeitelmanJun


8'13at19:48

7 Answers

OnemethodistouseCUPSandthePDFpsuedoprinterto"print"thetexttoaPDFfile.
AnotheristouseenscripttoencodetopostscriptandthenconvertfrompostscripttoPDF
usingtheps2pdffilefromghostscriptpackage.
answeredJul26'11at6:53

Keith
5,121

15

21

1 thanks.:pastebin.com/raw.php?i=XQnRjtnR LanceBaynes Jul26'11at8:11


2 doesnanybodyhasUTF8supportforenscript? LanceBaynes Jul26'11at8:12
1 @LanceBaynesCedilla,orgnomeu2ps,oru2ps.GillesJul26'11at20:42

pandoccandothis.It'smorefocusedonconvertingmarkeduptexttovariousformats,butit
shouldhavenoproblemswithsimpleplaintext.

http://unix.stackexchange.com/questions/17406/how-to-convert-txt-to-pdf

1/3

01/12/2016

text processing - How to convert TXT to PDF? - Unix & Linux Stack Exchange

pandoc input.txt -o output.pdf


answeredApr5'13at22:41

evilsoup
3,075

10

23

2 Looksnice,butthemassiveamountofhaskelldependenciespandocwantsscaredmeaway:/lkraavJun28'13at
11:46
@Ikraavyeah,it'swayoverkillforthispurpose,butifyouhavemarkdownorHTML(oranyoftheothermarkupsit
canacceptasinput)pandocwouldbethewaytogo.PDFactuallyrequiresevenmoredependenciesinternally,
pandocusesLaTeXtoconverttoPDF,soyouneedtoinstallthatstufftoobutthequalityisverygood(Iuseitto
convertmarkdown'dtexttoPDFandEPUB,primarily).evilsoupJun28'13at11:52
pandoc(1.16.0.2)replacesmynewlineswithtabs.SparhawkJan26at2:34

LibreOffice/OpenOfficeaswellasmostotherwordprocessors(Abiword)candothisquite
easily.
Thereisalittleutilitycalled unoconv thatusestheLibreOfficecodebasetodofileformat
conversionsonthecommandline.Itcanreadandwriteanycombinationofformatsthat
LibreOfficecanandmakesitveryeasytodothingslike doc to pdf conversionsonthe
commandline.Simple txt to pdf wouldbeeasyforit.
answeredJul26'11at7:55

Caleb
39.7k

107

148

Interesting.Didn'tknowabout unoconv anddidn'tevenknowOOhadAPIs.FaheemMithaJul28'11at13:45

Justusethetext2pdf,whichisfreeandopensource.Atthelinkyoucandownloadthesource
ortheprecompiledbinaryforwindows,solaris,dos.
I'mabletouseitintoAIXOSwithoutproblem.Verysimpletocompile,justsavethetext2pdf.c
andMakefileintothesamedirectoryandtype make .(hereIsetthevariableCC=gcconAIX,
onlinuxthiswillnotbeanissue)
$ ./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
-f<font>
-I
-s<size>
-v<dist>
-l<lines>
-c<chars>
-t<spaces>
-F
-A4
-A3
-x<width>
-y<height>
-2
-L

show this message


use PostScript <font> (must be in standard 14, default: Courier)
use ISOLatin1Encoding
use font at given pointsize (default 10)
use given line spacing (default 12 points)
lines per page (default 60, determined automatically
if unspecified)
maximum characters per line (default 80)
spaces per tab character (default 8)
ignore formfeed characters (^L)
use A4 paper (default Letter)
use A3 paper (default Letter)
independent paper width in points
independent paper height in points
format in 2 columns
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
editedApr5'13at17:32

answeredApr5'13at14:02

ceinmart
213

2 isthereanutf8capableforkofthis?WolfgangFahlJun4'15at2:42

ThereisalsoaUTF8toPostScriptconvertercalled

paps

editedJul26'11at12:17

answeredJul26'11at11:23

Caleb
39.7k

tunit
6

107

148

51

http://unix.stackexchange.com/questions/17406/how-to-convert-txt-to-pdf

2/3

01/12/2016

text processing - How to convert TXT to PDF? - Unix & Linux Stack Exchange

2 Itworks,andwithTrueTypefonts,butitshouldbenotedthatitproducesadocumentwithbitmapsasaresult,
insteadofusingthefontsnatively.(Iguessthat'sbecauseofPostscript?)njsgJan29'13at22:08
Couldn'tcompileitonCygwin32onmyWindoze...errekaFeb18at23:03

YoucanprinttexttoaPDFfileusingVim,aslongasitwascompiledwiththe
feature.
Forthisyouusethe :hardcopy
example.txt andexecute

> {filename}

+postscript

command.Forexampleyoucanopen

:hardcopy > example.pdf

whichwillproduceafile example.pdf containingallthetextin example.txt .Theheaderof


eachpageinthePDFwillcontaintheoriginalfilenameandthepagenumber.
Youcandothesamedirectlyfromaterminal(withoutinteractingwithVim)byusingthe
followingcommand
vim example.txt -c "hardcopy > man_xz.pdf | q"

Thisopens example.txt inVimandexecutesthecommandpassedtothe


inthiscaseisa hardcopy commandfollowedbyaquit( q )command.

-c

option,which

The hardcopy commandcanalsoproducePostScriptfiles.Justchangethefileextensionto .ps inthe


commands.Formoreoptionsseethehelpfileswith :help :hardcopy .
answeredApr17at1:05

GonaloRibeiro
151

Useenscripttocreateda.psfile,andthenps2pdf(orps2pdfwr)toconvertto.pdf
Thefollowingscriptcreatesa.pdffilewith10ptleftandrightmargins,andusesacourierfont
thatis7.3ptswideand10ptshigh,soa132colprintoutfitsonan81/2X11page.Use
enscripttosetupyourpage,fonts,etc.
$ enscript -B --margins=10:10: -o outputfile.ps -f Courier@7.3/10 inputfile
$ ps2pdfwr outputfile.ps newfile.pdf
$ rm outputfile.ps
editedJun8'13at20:32

answeredJun8'13at18:52

slm
171k

DanWalker
43

320

489

31

4 Athingtonote:enscriptdoesnotsupportutf8.maxschlepzigAug16'14at13:51

http://unix.stackexchange.com/questions/17406/how-to-convert-txt-to-pdf

3/3

You might also like