@@ -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