From 15f700d8edc7c3a860766de7c6fef572b533dab2 Mon Sep 17 00:00:00 2001
From: Mauro Bieg <mb21@users.noreply.github.com>
Date: Mon, 12 Mar 2018 02:21:15 +0100
Subject: [PATCH] html2pdf: inject base tag wih current working directory
 (#4443)

fixes #4413
---
 src/Text/Pandoc/PDF.hs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/Text/Pandoc/PDF.hs b/src/Text/Pandoc/PDF.hs
index 5f41d6c55..82c2312fe 100644
--- a/src/Text/Pandoc/PDF.hs
+++ b/src/Text/Pandoc/PDF.hs
@@ -56,6 +56,8 @@ import System.IO.Error (IOError, isDoesNotExistError)
 #else
 import System.IO.Error (isDoesNotExistError)
 #endif
+import Text.HTML.TagSoup
+import Text.HTML.TagSoup.Match
 import Text.Pandoc.Definition
 import Text.Pandoc.Error (PandocError (PandocPDFProgramNotFoundError))
 import Text.Pandoc.MIME (getMimeType)
@@ -353,7 +355,13 @@ html2pdf  :: Verbosity    -- ^ Verbosity level
           -> [String]     -- ^ Args to program
           -> Text         -- ^ HTML5 source
           -> IO (Either ByteString ByteString)
-html2pdf verbosity program args source = do
+html2pdf verbosity program args htmlSource = do
+  cwd <- getCurrentDirectory
+  let tags = parseTags htmlSource
+      (hd, tl) = break (tagClose (== "head")) tags
+      baseTag = TagOpen "base"
+        [("href", T.pack cwd <> T.singleton pathSeparator)] : [TagText "\n"]
+      source = renderTags $ hd ++ baseTag ++ tl
   pdfFile <- withTempFile "." "html2pdf.pdf" $ \fp _ -> return fp
   let pdfFileArgName = ["-o" | program == "prince"]
   let programArgs = args ++ ["-"] ++ pdfFileArgName ++ [pdfFile]