Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions document/test/files/inline_image.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{-# LANGUAGE OverloadedStrings #-}

-- Generate PDF file with inline image

module Main
(
main
)
where

import qualified Data.ByteString.Lazy as BSL
import qualified Data.Vector as Vector
import qualified Data.HashMap.Strict as HashMap
import Control.Monad
import qualified System.IO.Streams as Streams

import Pdf.Core
import Pdf.Core.Writer

main :: IO ()
main = do
let tr = HashMap.fromList [
("Size", Number $ fromIntegral $ length objects),
("Root", Ref catalogRef)
]
objects = [
(Dict catalog, catalogRef),
(Dict rootNode, rootNodeRef),
(Dict page, pageRef),
(Dict font, fontRef)
]
streams = [
(contentDict, contentData, contentRef)
]
catalog = HashMap.fromList [
("Type", Name "Catalog"),
("Pages", Ref rootNodeRef)
]
rootNode = HashMap.fromList [
("Type", Name "Pages"),
("Kids", Array $ Vector.fromList [Ref pageRef]),
("Count", Number 1)
]
page = HashMap.fromList [
("Type", Name "Page"),
("Parent", Ref rootNodeRef),
("Contents", Ref contentRef),
("Resources", Dict resourcesDict),
("MediaBox", Array $ Vector.fromList [
Number 0,
Number 0,
Number 200,
Number 200
])
]
resourcesDict = HashMap.fromList [
("Font", Dict $ HashMap.fromList [
("F1", Ref fontRef)
])
]
font = HashMap.fromList [
("Type", Name "Font"),
("Subtype", Name "Type1"),
("BaseFont", Name "Helvetica")
]
contentDict = HashMap.fromList [
("Length", Number $ fromIntegral $ BSL.length contentData)
]
contentData = "BT /F1 12 Tf 100 100 TD (Hello!!!) Tj ET\n"
<> "q 100 0 0 100 0 0 cm\n"
<> "BI /W 4 /H 4 /CS /RGB /BPC 8\n"
<> "ID\n"
<> "00000z0z00zzz00z0zzz0zzzEI aazazaazzzaazazzzazzz\n"
<> "EI Q\n"
<> "BT /F1 12 Tf 70 70 TD (World!!!) Tj ET\n"
catalogRef = R 1 0
rootNodeRef = R 2 0
pageRef = R 3 0
contentRef = R 4 0
fontRef = R 5 0

writer <- makeWriter Streams.stdout
writeHeader writer
forM_ objects $ \(obj, ref) ->
writeObject writer ref obj
forM_ streams $ \(dict, dat, ref) ->
writeStream writer ref dict dat
writeXRefTable writer 0 tr
Binary file added document/test/files/inline_image.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions document/test/test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ main = hspec $ do
txt `shouldBe` Just
"\nHello World!!!\nXObject is here\nnested XObject is here"

describe "inline_image" $ do
it "should have correct text" $ do
withPdfFile "test/files/inline_image.pdf" $ \pdf -> do
doc <- document pdf
catalog <- documentCatalog doc
root <- catalogPageNode catalog
page <- pageNodePageByNum root 0
-- here timeout breaks infinite loop in case of a bug
txt <- timeout 5000000 $ pageExtractText page
txt `shouldBe` Just
"\nHello!!!\nWorld!!!"

-- | Generate simple PDF file for tests
withSimpleFile :: (Handle -> IO ()) -> IO ()
withSimpleFile action = do
Expand Down