-
-
Save cleverca22/a261d7c01641228604ab3105608a81ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List (sortOn) | |
import qualified Data.Text as ST | |
import Text.LaTeX (LaTeXT, execLaTeXM, render, fromString) | |
import Data.Functor.Identity (Identity) | |
import Text.Pandoc (readerParseRaw, writerTemplate, readLaTeX, def, writeLaTeX) | |
import Text.Pandoc.PDF (makePDF) | |
import Data.Monoid ((<>)) | |
import qualified Data.ByteString.Lazy as LBS | |
import qualified Data.Text.Lazy as LT | |
import qualified Data.Text.Lazy.Encoding as LT | |
import Text.Printf (printf) | |
import Formatting (format, commas) | |
class RowType r where | |
doFileRun :: (RowType a, Ord i) => (a -> LaTeXT Identity ()) -> (a -> i) -> [ a ] -> String -> FilePath -> i -> IO () | |
doFileRun makeRow f allRows template outputFile minAmount = do | |
let | |
filtered = filter (\v -> (f v) >= minAmount) allRows | |
sorted = reverse $ sortOn f filtered | |
raw_latex = ((mapM_ makeRow sorted) |> execLaTeXM |> render) :: ST.Text | |
result = readLaTeX (def { readerParseRaw = True }) $ ST.unpack raw_latex | |
pandoc = either (error "unable to parse") id result | |
putStrLn $ outputFile <> " b " <> (show $ length filtered) | |
res <- makePDF "pdflatex" writeLaTeX (def { writerTemplate = Just template }) pandoc | |
let | |
rawPdf = either ( error . LT.unpack . LT.decodeUtf8) id res | |
LBS.writeFile outputFile rawPdf | |
putStrLn $ "wrote output to: " <> outputFile | |
pure () | |
text :: (Monad m) => ST.Text -> LaTeXT m () | |
text = fromString . ST.unpack | |
makeRow :: Monad m => Row -> LaTeXT m () | |
makeRow r = do | |
let | |
list :: [ (T.Text, Row -> T.Text) ] | |
list = [ | |
("Last Name:", lastName) | |
, ("First Name:", firstName) | |
, ("Middle Name:", middleName) | |
, ("Address :", address1) | |
] | |
formatPage :: Monad m => [ (T.Text, Row -> T.Text) ] -> LaTeXT m () | |
formatPage fmt = mapM_ (\(a,b) -> do { text a & text (b r) ; lnbk }) fmt | |
tabular Nothing [ LeftColumn, LeftColumn ] $ do | |
formatPage list | |
newpage | |
"\n" | |
elsewhere: | |
doFileRun makeRow fieldAccessor (_rows file) template "output.pdf" 10000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment