Pdf.hs 1.07 KB
Newer Older
1
module Pdf
2
  ( launchChrome
3 4 5 6
  ) where

import System.Decker.OS

7 8
import Control.Exception
import Data.List
9
import System.Directory
10
import System.Exit
11 12 13
import System.IO
import System.Process

14 15 16
chromeOptions :: FilePath -> FilePath -> [String]
chromeOptions src out =
  ["--headless", "--disable-gpu", pdfOption out, modifySrc src]
17 18 19
  where
    modifySrc path = path ++ "?print-pdf#/"
    pdfOption path = "--print-to-pdf=" ++ path
20

21
launchChrome :: FilePath -> FilePath -> IO (Either String String)
22 23 24
launchChrome src out = do
  command <- chrome
  let options = unwords (chromeOptions src out)
25 26 27 28 29 30 31 32 33 34 35 36 37
  case command of
    Left msg -> return $ Left msg
    Right cmd -> do
      (_, _, _, ph) <-
        createProcess (shell $ cmd ++ " " ++ options) {std_err = CreatePipe}
      code <- waitForProcess ph
      case code of
        ExitFailure _ ->
          return $
          Left
            ("Google Chrome is most likely not installed. " ++
             "Please install Google Chrome to use 'decker pdf' or 'decker pdf-decks'")
        ExitSuccess -> return $ Right ("Completed: " ++ src ++ " -> " ++ out)