-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpdft.hs
304 lines (257 loc) · 8.83 KB
/
hpdft.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import PDF.Definition
import PDF.Object
import PDF.DocumentStructure
import PDF.PDFIO
import PDF.Outlines
import System.Environment (getArgs)
import Data.ByteString.UTF8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.Text.Lazy as TL (unpack)
import Data.Text.Lazy.Encoding as TL
import Data.List (nub, find)
import Data.Maybe (fromMaybe)
import Options.Applicative
import Data.Semigroup ((<>))
import Text.Regex.Base.RegexLike ( makeRegex )
import Text.Regex.TDFA.String ( regexec )
import Options.Applicative (strOption)
import Control.Monad (when)
import PDF.Definition (Obj(PdfStream))
import qualified Paths_hpdft as Autogen (version)
import Data.Version (showVersion)
import Debug.Trace
initstate = PSR { linex=0
, liney=0
, absolutex=0
, absolutey=0
, leftmargin=0.0
, text_lm=(1,0,0,1,0,0)
, text_m=(1,0,0,1,0,0)
, text_break=False
, top=0.0
, bottom=0.0
, fontfactor=1
, curfont=""
, fontmaps=[]
, cmaps=[]
, colorspace=""
, xcolorspaces=[]
}
main :: IO ()
main = hpdft =<< execParser opts
where
opts = info (options <**> helper <**> (simpleVersioner versionInfo))
(fullDesc
<> header versionInfo)
versionInfo = "hpdft - a PDF to text converter, version " <> showVersion Autogen.version
-- option parser
data CmdOpt = CmdOpt {
page :: Int,
ref :: Int,
grep :: String,
refs :: Bool,
pdftitle :: Bool,
pdfinfo :: Bool,
pdfoutline :: Bool,
trailer :: Bool,
file :: FilePath
}
options :: Parser CmdOpt
options = CmdOpt
<$> option auto
( long "page"
<> short 'p'
<> value 0
<> metavar "PAGE"
<> help "Page number (nomble)" )
<*> option auto
( long "ref"
<> short 'r'
<> value 0
<> metavar "REF"
<> help "Object reference" )
<*> strOption
( long "grep"
<> short 'g'
<> value ""
<> metavar "RegExp"
<> help "grep PDF" )
<*> switch
( long "refs"
<> short 'R'
<> help "Show object references in page order" )
<*> switch
( long "title"
<> short 'T'
<> help "Show title (from metadata)" )
<*> switch
( long "info"
<> short 'I'
<> help "Show PDF metainfo" )
<*> switch
( long "toc"
<> short 'O'
<> help "Show table of contents (from metadata) " )
<*> switch
( long "trailer"
<> help "Show the trailer of PDF" )
<*> strArgument
( help "input pdf file"
<> metavar "FILE"
<> action "file" )
hpdft :: CmdOpt -> IO ()
hpdft (CmdOpt 0 0 "" False False False False False fn) = pdfToText fn
hpdft (CmdOpt 0 0 "" False True _ _ _ fn) = showTitle fn
hpdft (CmdOpt 0 0 "" False _ True _ _ fn) = showInfo fn
hpdft (CmdOpt 0 0 "" False _ _ True _ fn) = showOutlines fn
hpdft (CmdOpt 0 0 "" False _ _ _ True fn) = print =<< getTrailer fn
hpdft (CmdOpt 0 0 "" True _ _ _ _ fn) = print =<< refByPage fn
hpdft (CmdOpt n 0 "" False _ _ _ _ fn) = showPage fn n
hpdft (CmdOpt 0 r "" False _ _ _ _ fn) = showContent fn r
hpdft (CmdOpt 0 0 r False _ _ _ _ fn) = grepPDF fn r
hpdft _ = return ()
-- | Get a whole text from 'filename'. It works as:
-- (1) grub objects
-- (2) parse within each object, deflating its stream
-- (3) linearize stream
pdfToText filename = do
objs <- getPDFObjFromFile filename
rootref <- getRootRef filename
BSL.putStrLn $ walkdown initstate rootref objs
walkdown :: PSR -> Int -> [PDFObj] -> PDFStream
walkdown st parent objs =
case findObjsByRef parent objs of
Just os -> case findDictOfType "/Catalog" os of
Just dict -> case findPages dict of
Just pr -> walkdown st pr objs
Nothing -> ""
Nothing -> case findDictOfType "/Pages" os of
Just dict -> case findKids dict of
Just kidsrefs -> BSL.concat $ map ((\f -> f objs) . (walkdown st)) kidsrefs
Nothing -> ""
Nothing -> case findDictOfType "/Page" os of
Just dict -> contentsStream dict st objs
Nothing -> ""
Nothing -> ""
data PageTree = Nop | Page Int | Pages [PageTree]
deriving Show
-- | Sort object references in page order.
refByPage filename = do
root <- getRootRef filename
objs <- getPDFObjFromFile filename
return $ pageTreeToList $ pageorder root objs
pageorder :: Int -> [PDFObj] -> PageTree
pageorder parent objs =
case findObjsByRef parent objs of
Just os -> case findDictOfType "/Catalog" os of
Just dict -> case findPages dict of
Just pr -> pageorder pr objs
Nothing -> Nop
Nothing -> case findDictOfType "/Pages" os of
Just dict -> case findKids dict of
Just kidsrefs -> Pages $ map (\f -> f objs) (map pageorder kidsrefs)
Nothing -> Nop
Nothing -> case findDictOfType "/Page" os of
Just dict -> Page parent
Nothing -> Nop
Nothing -> Nop
pageTreeToList :: PageTree -> [Int]
pageTreeToList (Pages ps) = concatMap pageTreeToList ps
pageTreeToList (Page n) = [n]
pageTreeToList Nop = []
-- | Show contents of page 'page' in 'filename'.
showPage filename page = do
pagetree <- refByPage filename
case length pagetree >= page of
True -> contentByRef filename $ pagetree !! (page - 1)
False -> putStrLn $ "hpdft: No Page "++(show page)
-- | Show /Content referenced from the 'ref'ed-object in 'filename'.
contentByRef filename ref = do
objs <- getPDFObjFromFile filename
obj <- getObjectByRef ref objs
BSL.putStrLn $ contentInObject obj objs
where contentInObject obj objs =
case findDictOfType "/Page" obj of
Just dict -> contentsStream dict initstate objs
Nothing -> ""
showContent filename ref = do
objs <- getPDFObjFromFile filename
obj <- getObjectByRef ref objs
let d = fromMaybe [] $ findDict obj
if hasStream obj
then
if hasSubtype d -- then it's not content stream
then printStreamWithDict d obj
else BSL.putStrLn =<< getStream False obj
else print =<< getObjectByRef ref objs
where
hasStream obj = case find isStream obj of
Just _ -> True
Nothing -> False
isStream (PdfStream _) = True
isStream _ = False
hasSubtype d = case find isSubtype d of
Just _ -> True
Nothing -> False
isSubtype (PdfName "/Subtype", _) = True
isSubtype x = False
printStreamWithDict :: Dict -> [Obj] -> IO ()
printStreamWithDict d obj = print (PdfDict d) >> (BSL.putStrLn =<< getStream True obj)
-- | Show /Title from meta information in 'filename'
showTitle filename = do
d <- getInfo filename
let title =
case findObjFromDict d "/Title" of
Just (PdfText s) -> s
Just x -> show x
Nothing -> "No title anyway"
putStrLn title
return ()
-- | Show /Info from meta information in 'filename'
showInfo filename = do
d <- getInfo filename
putStrLn $ toString 0 (PdfDict d)
return ()
-- | Show /Outlines from meta information in 'filename'
showOutlines filename = do
d <- getOutlines filename
putStrLn $ show d
return ()
-- | Find string in each page.
grepPDF filename re = do
root <- getRootRef filename
objs <- getPDFObjFromFile filename
mapM
(\(strm, pagenm) -> (grepByPage pagenm re . contentInObjs objs) strm)
$ zip (pageTreeToList $ pageorder root objs) [1..]
return ()
where
contentInObjs objs ref =
case findObjsByRef ref objs of
Just obj -> case findDictOfType "/Page" obj of
Just dict -> contentsStream dict initstate objs
Nothing -> ""
Nothing -> error $ "No Object with Ref " ++ show ref
grepByPage :: Int -> String -> PDFStream -> IO ()
grepByPage pagenm re txt = do
let matched = filter (not . null) $ map (grepByLine re) $ BSL.lines txt
when (not $ null matched) (showResult pagenm matched)
return ()
where
showResult p m = do
putStrLn $ "At page " <> show p <> "..."
mapM (putStrLn . (" | " <>)) m
return ()
grepByLine :: String -> PDFStream -> String
grepByLine re txt =
case regexec (makeRegex re) $ TL.unpack $ TL.decodeUtf8 txt of
Left _ -> ""
Right m -> case m of
Just (b, m, a, _) -> (b <> (highlight m) <> a)
Nothing -> ""
highlight m = "\ESC[31m" <> m <> "\ESC[0m"