Add to example programs to show how the lib can be used
This commit is contained in:
parent
699f830a45
commit
9ab010de61
3 changed files with 68 additions and 0 deletions
|
@ -32,3 +32,21 @@ library
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
default-language: Haskell2010
|
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
|
||||||
|
|
13
examples/equivalent.hs
Normal file
13
examples/equivalent.hs
Normal file
|
@ -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
|
37
examples/getObj.hs
Normal file
37
examples/getObj.hs
Normal file
|
@ -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
|
Loading…
Reference in a new issue