yesod-content-pdf (empty) → 0.1.0.0
raw patch · 5 files changed
+134/−0 lines, 5 filesdep +basedep +blaze-htmldep +bytestringsetup-changed
Dependencies added: base, blaze-html, bytestring, directory, process, temporary, yesod-content-pdf, yesod-core
Files
- LICENSE +28/−0
- Setup.hs +2/−0
- src/Yesod/Content/PDF.hs +59/−0
- test/Spec.hs +2/−0
- yesod-content-pdf.cabal +43/−0
+ LICENSE view
@@ -0,0 +1,28 @@+Copyright (c) 2015, Alex Kyllo++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+ list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++* Neither the name of yesod-content-pdf nor the names of its+ contributors may be used to endorse or promote products derived from+ this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Yesod/Content/PDF.hs view
@@ -0,0 +1,59 @@+{-# OPTIONS -fno-warn-orphans #-}+-- | Utilities for serving PDF from Yesod+-- Uses and depends on command line utility wkhtmltopdf to render PDF from HTML++module Yesod.Content.PDF where+import Prelude+import Yesod.Core.Content+import Data.ByteString+import Text.Blaze.Html+import Text.Blaze.Html.Renderer.String+import System.Process+import System.IO.Temp+import System.IO+import System.IO.Unsafe++type PDF = ByteString++typePDF :: ContentType+typePDF = "application/pdf"++instance HasContentType PDF where+ getContentType _ = typePDF++instance ToTypedContent PDF where+ toTypedContent = TypedContent typePDF . toContent++instance HasContentType (IO PDF) where+ getContentType _ = typePDF++instance ToTypedContent (IO PDF) where+ toTypedContent ioPDF = TypedContent typePDF $ toContent ioPDF++instance ToContent (IO PDF) where+ toContent = toContent . unsafePerformIO++-- | Use wkhtmltopdf to render a PDF given the URL to an HTML document+url2PDF :: String -> IO PDF+url2PDF url = withSystemTempFile "output.pdf" (url2PDF' url)++url2PDF' :: String -> FilePath -> Handle -> IO PDF+url2PDF' url tempPDFFile tempHandle = do+ hClose tempHandle+ (_,_,_, pHandle) <- createProcess (proc "wkhtmltopdf" ["--quiet", url, tempPDFFile])+ _ <- waitForProcess pHandle+ Data.ByteString.readFile tempPDFFile++-- | Use wkhtmltopdf to render a PDF from an HTML (Text.Blaze.Html) type+html2PDF :: Html -> IO PDF+html2PDF html = withSystemTempFile "output.pdf" (html2PDF' html)++html2PDF' :: Html -> FilePath -> Handle -> IO PDF+html2PDF' html tempPDFFile tempPDFHandle = do+ hClose tempPDFHandle+ withSystemTempFile "input.html" $ \tempHtmlFile tempHtmlHandle -> do+ System.IO.hPutStrLn tempHtmlHandle $ renderHtml html+ hClose tempHtmlHandle+ (_,_,_, pHandle) <- createProcess (proc "wkhtmltopdf" ["--quiet", tempHtmlFile, tempPDFFile])+ _ <- waitForProcess pHandle+ Data.ByteString.readFile tempPDFFile
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"
+ yesod-content-pdf.cabal view
@@ -0,0 +1,43 @@+name: yesod-content-pdf+version: 0.1.0.0+synopsis: PDF Content Type for Yesod+description: Please see README.md+homepage: https://github.com/alexkyllo/yesod-content-pdf#readme+license: BSD3+license-file: LICENSE+author: Alex Kyllo+maintainer: alex.kyllo@gmail.com+copyright: 2015 Alex Kyllo+category: Web+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Yesod.Content.PDF+ default-extensions: OverloadedStrings+ , TypeSynonymInstances+ , FlexibleInstances++ build-depends: base >= 4.7 && < 5+ , yesod-core+ , process+ , bytestring+ , blaze-html+ , temporary+ , directory+ default-language: Haskell2010++test-suite yesod-content-pdf-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ build-depends: base+ , yesod-content-pdf+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/alexkyllo/yesod-content-pdf