git.iko.soy/typeable/pandoc
on master help sign in
PDF Writer: Attempts to convert images to pdf renderable formats

Now depends on the JuicyPixels library. Will attempt to convert an image (gif, tiff, bmp) to png when converting to pdf.

commit57bebe26df8ee0429eec41b828e1acb904cc84fb authorMatthew Pickering <matthewtpickering@gmail.com> dateTue 12 Aug 2014, 15:09 (12 years ago) parent4c88e64
2 files changed+31−4
pandoc.cabal+2−1 src/Text/Pandoc/PDF.hs+29−3
pandoc.cabal+2−1
@@ -253,7 +253,8 @@ Library
253253 SHA >= 1.6 && < 1.7,
254254 haddock-library >= 1.1 && < 1.2,
255255 old-time,
256− deepseq-generics >= 0.1 && < 0.2
256+ deepseq-generics >= 0.1 && < 0.2,
257+ JuicyPixels >= 3.1.6.1 && < 3.2
257258 if flag(https)
258259 Build-Depends: http-client >= 0.3.2 && < 0.4,
259260 http-client-tls >= 0.2 && < 0.3,
src/Text/Pandoc/PDF.hs+29−3
@@ -39,7 +39,8 @@ import System.FilePath
3939import System.Directory
4040import Data.Digest.Pure.SHA (showDigest, sha1)
4141import System.Environment
42−import Control.Monad (unless)
42+import Control.Monad (unless, (<=<))
43+import Control.Applicative ((<$>), (<$))
4344import Data.List (isInfixOf)
4445import Data.Maybe (fromMaybe)
4546import qualified Text.Pandoc.UTF8 as UTF8
@@ -47,9 +48,10 @@ import Text.Pandoc.Definition
4748import Text.Pandoc.Walk (walkM)
4849import Text.Pandoc.Shared (fetchItem', warn, withTempDir)
4950import Text.Pandoc.Options (WriterOptions(..))
50−import Text.Pandoc.MIME (extensionFromMimeType)
51+import Text.Pandoc.MIME (extensionFromMimeType, getMimeType)
5152import Text.Pandoc.Process (pipeProcess)
5253import qualified Data.ByteString.Lazy as BL
54+import qualified Codec.Picture as JP
5355#ifdef _WINDOWS
5456import Data.List (intercalate)
5557#endif
@@ -73,7 +75,7 @@ handleImages :: WriterOptions
7375 -> FilePath -- ^ temp dir to store images
7476 -> Pandoc -- ^ document
7577 -> IO Pandoc
76−handleImages opts tmpdir = walkM (handleImage' opts tmpdir)
78+handleImages opts tmpdir = walkM (convertImages tmpdir) <=< walkM (handleImage' opts tmpdir)
7779
7880handleImage' :: WriterOptions
7981 -> FilePath
@@ -98,6 +100,30 @@ handleImage' opts tmpdir (Image ils (src,tit)) = do
98100 return $ Image ils (src,tit)
99101handleImage' _ _ x = return x
100102
103+convertImages :: FilePath -> Inline -> IO Inline
104+convertImages tmpdir (Image ils (src, tit)) = do
105+ img <- convertImage tmpdir src
106+ newPath <-
107+ case img of
108+ Left _ -> src <$ (warn $ "Unable to convert image `" ++ src ++ "'")
109+ Right (fp, action) -> fp <$ action
110+ return (Image ils (newPath, tit))
111+convertImages _ x = return x
112+
113+-- Convert formats which do not work well in pdf to png
114+convertImage :: FilePath -> FilePath -> IO (Either String ((FilePath, IO ())))
115+convertImage tmpdir fname =
116+ case mime of
117+ Just "image/png" -> doNothing
118+ Just "image/jpeg" -> doNothing
119+ Just "application/pdf" -> doNothing
120+ _ -> savePng <$> JP.readImage fname
121+ where
122+ fileOut = replaceDirectory (replaceExtension fname (".png")) tmpdir
123+ savePng = fmap (\x -> (fileOut, JP.savePngImage fileOut x))
124+ mime = getMimeType fname
125+ doNothing = return (Right $ (fname, return ()))
126+
101127tex2pdf' :: FilePath -- ^ temp directory for output
102128 -> String -- ^ tex program
103129 -> String -- ^ tex source