From 737c0a684eaa03774b862ddcaf8c1ad7ef147571 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sat, 2 Feb 2019 11:31:29 -0800
Subject: [PATCH] PDF: use system temp dir and set TEXMFOUTPUT.

Previously the temp directory was created inside the working
directory, so that programs like epstopdf.pl would be allowed
to run in restricted mode.  However, setting TEXMFOUTPUT allows
these programs to run in the tmpdir inside the system temp
directory.

This is a better solution than cd51983.  Using the system
temp dir prevents problems when pandoc is run inside a synced
directory (e.g. dropbox).

Partially addresses #1192.
---
 src/Text/Pandoc/PDF.hs | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/Text/Pandoc/PDF.hs b/src/Text/Pandoc/PDF.hs
index 3484699c0..f7c7f744e 100644
--- a/src/Text/Pandoc/PDF.hs
+++ b/src/Text/Pandoc/PDF.hs
@@ -50,7 +50,7 @@ import System.Environment
 import System.Exit (ExitCode (..))
 import System.FilePath
 import System.IO (stdout)
-import System.IO.Temp (withTempDirectory, withTempFile)
+import System.IO.Temp (withSystemTempDirectory, withTempFile)
 #if MIN_VERSION_base(4,8,3)
 import System.IO.Error (IOError, isDoesNotExistError)
 #else
@@ -100,14 +100,9 @@ makePDF program pdfargs writer opts doc = do
       verbosity <- getVerbosity
       liftIO $ ms2pdf verbosity program args source
     baseProg -> do
-      -- With context and latex, we create a temp directory within
-      -- the working directory, since pdflatex sometimes tries to
-      -- use tools like epstopdf.pl, which are restricted if run
-      -- on files outside the working directory.
-      let withTemp = withTempDirectory "."
       commonState <- getCommonState
       verbosity <- getVerbosity
-      liftIO $ withTemp "tex2pdf." $ \tmpdir -> do
+      liftIO $ withSystemTempDirectory "tex2pdf." $ \tmpdir -> do
         source <- runIOorExplode $ do
                     putCommonState commonState
                     doc' <- handleImages tmpdir doc
@@ -291,7 +286,9 @@ runTeXProgram verbosity program args runNumber numRuns tmpDir source = do
     let texinputs = maybe (tmpDir' ++ sep) ((tmpDir' ++ sep) ++)
           $ lookup "TEXINPUTS" env'
     let env'' = ("TEXINPUTS", texinputs) :
-                  [(k,v) | (k,v) <- env', k /= "TEXINPUTS"]
+                ("TEXMFOUTPUT", tmpDir') :
+                  [(k,v) | (k,v) <- env'
+                         , k /= "TEXINPUTS" && k /= "TEXMFOUTPUT"]
     when (verbosity >= INFO && runNumber == 1) $ do
       putStrLn "[makePDF] temp dir:"
       putStrLn tmpDir'