2019-09-20 22:42:17 +02:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
|
|
|
|
import Codec.Compression.Zlib (decompress)
|
|
|
|
import Data.ByteString.Lazy.Char8 (ByteString)
|
|
|
|
import qualified Data.ByteString.Char8 as BS (readFile)
|
2019-09-22 01:40:39 +02:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as Lazy (fromStrict, putStr, toStrict)
|
2019-09-20 22:42:17 +02:00
|
|
|
import Data.Map ((!))
|
|
|
|
import qualified Data.Map as Map (lookup)
|
|
|
|
import PDF (Document(..), parseDocument)
|
|
|
|
import qualified PDF.EOL as EOL (Style)
|
|
|
|
import PDF.Object (Content(..), DirectObject(..), Object(..), Name(..))
|
|
|
|
import PDF.Output (ObjectId(..))
|
|
|
|
import qualified PDF.Output as Output (render)
|
2019-09-22 01:40:39 +02:00
|
|
|
import PDF.Update (unify)
|
2019-09-20 22:42:17 +02:00
|
|
|
import System.Environment (getArgs)
|
|
|
|
import System.IO (hPutStrLn, stderr)
|
|
|
|
|
|
|
|
display :: EOL.Style -> Object -> ByteString
|
|
|
|
display eolStyle d@(Direct _) = Output.render eolStyle d
|
|
|
|
display eolStyle s@(Stream {header, streamContent}) = Output.render eolStyle $
|
|
|
|
case Map.lookup (Name "Filter") header of
|
|
|
|
Just (NameObject (Name "FlateDecode")) -> Stream {
|
|
|
|
header
|
|
|
|
, streamContent = Lazy.toStrict . decompress $ Lazy.fromStrict streamContent
|
|
|
|
}
|
|
|
|
_ -> s
|
|
|
|
|
|
|
|
extractObject :: ObjectId -> Document -> ByteString
|
|
|
|
extractObject objectId (Document {eolStyle, updates}) =
|
2019-09-22 01:40:39 +02:00
|
|
|
display eolStyle . (!objectId) $ objects content
|
|
|
|
where
|
|
|
|
content = unify updates
|
2019-09-20 22:42:17 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
[inputFile, objectId] <- getArgs
|
|
|
|
result <- parseDocument <$> BS.readFile inputFile
|
|
|
|
case result of
|
2019-09-22 01:40:39 +02:00
|
|
|
Left parseError -> hPutStrLn stderr parseError
|
2019-09-20 22:42:17 +02:00
|
|
|
Right doc -> Lazy.putStr $ extractObject (ObjectId (read objectId)) doc
|