From 9ab010de6190dbbdb1eb55dd64cc77599186e864 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Fri, 20 Sep 2019 22:42:17 +0200 Subject: [PATCH] Add to example programs to show how the lib can be used --- Hufflepdf.cabal | 18 ++++++++++++++++++ examples/equivalent.hs | 13 +++++++++++++ examples/getObj.hs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 examples/equivalent.hs create mode 100644 examples/getObj.hs diff --git a/Hufflepdf.cabal b/Hufflepdf.cabal index 3889fdb..d1e0dfa 100644 --- a/Hufflepdf.cabal +++ b/Hufflepdf.cabal @@ -32,3 +32,21 @@ library hs-source-dirs: src ghc-options: -Wall default-language: Haskell2010 + +executable equivalent + main-is: examples/equivalent.hs + build-depends: base + , bytestring + , Hufflepdf + ghc-options: -Wall + default-language: Haskell2010 + +executable getObj + main-is: examples/getObj.hs + build-depends: base + , bytestring + , containers + , Hufflepdf + , zlib + ghc-options: -Wall + default-language: Haskell2010 diff --git a/examples/equivalent.hs b/examples/equivalent.hs new file mode 100644 index 0000000..93132c7 --- /dev/null +++ b/examples/equivalent.hs @@ -0,0 +1,13 @@ +import qualified Data.ByteString.Char8 as BS (readFile) +import qualified Data.ByteString.Lazy.Char8 as Lazy (writeFile) +import PDF (parseDocument, render) +import System.Environment (getArgs) +import System.IO (hPutStrLn, stderr) + +main :: IO () +main = do + [inputFile, outputFile] <- getArgs + result <- parseDocument <$> BS.readFile inputFile + case result of + Left parseError -> hPutStrLn stderr parseError + Right doc -> Lazy.writeFile outputFile $ render doc diff --git a/examples/getObj.hs b/examples/getObj.hs new file mode 100644 index 0000000..d824910 --- /dev/null +++ b/examples/getObj.hs @@ -0,0 +1,37 @@ +{-# LANGUAGE NamedFieldPuns #-} + +import Codec.Compression.Zlib (decompress) +import Data.ByteString.Lazy.Char8 (ByteString) +import qualified Data.ByteString.Char8 as BS (readFile) +import qualified Data.ByteString.Lazy.Char8 as Lazy (concat, fromStrict, putStr, toStrict) +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) +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}) = + Lazy.concat $ display eolStyle . (!objectId) . objects <$> updates + +main :: IO () +main = do + [inputFile, objectId] <- getArgs + result <- parseDocument <$> BS.readFile inputFile + case result of + Left parseError -> hPutStrLn stderr $ show parseError + Right doc -> Lazy.putStr $ extractObject (ObjectId (read objectId)) doc