bburdette / pdf-element / PdfElement

This Elm module contains the necessary machinery for communicating with javascript, where the actual pdf wrangling will take place. Use the Cmds to open and close documents, listen for Msgs indicating the Cmd results, and display the document pages with pdfPage.

pdfPage : String -> Basics.Int -> PdfDims -> Html msg

pdfPage makes a 'custom element' that displays the pdf for the document indicated by 'name'. Before calling this function, you should open the document with a PdfCmd and wait for a Loaded msg.


type PdfDims
    = Scale Basics.Float
    | Width Basics.Int
    | Height Basics.Int
    | WidthHeight Basics.Int Basics.Int

Specify the size of the pdf document. If you specify height or width, the scale will be computed to fit.


type PdfCmd
    = OpenUrl ({ name : String, url : String })
    | OpenString ({ name : String, string : String })
    | Close ({ name : String })

PdfCmds go from from elm out to javascript to be processed. Each pdf document should have a unique name. You can make multiple pdfPage controls that reference a single pdf document.


type PdfMsg
    = Error ({ name : String, error : String })
    | Loaded ({ name : String, pageCount : Basics.Int })

PdfMsgs are responses from javascript to elm after PdfCmd operations. The name should be the same string you used in OpenUrl or OpenString.

receive : (Result Json.Decode.Error PdfMsg -> msg) -> Json.Decode.Value -> msg

make a subscription function with receive and a port, like so:

  port receivePdfMsg : (JD.Value -> msg) -> Sub msg

  pdfreceive : Sub Msg
  pdfreceive =
      receivePdfMsg <| PdfElement.receive PdfMsg

Where PdfMessage is defined in your app like this:

  type Msg
      = PdfMsg (Result JD.Error PdfElement.PdfMsg)
      | <other message types>

then in your application subscriptions:

  subscriptions =
      \_ -> pdfreceive

send : (Json.Encode.Value -> Platform.Cmd.Cmd msg) -> PdfCmd -> Platform.Cmd.Cmd msg

use send to make a websocket convenience function, like so:

  port sendPdfCommand : JE.Value -> Cmd msg

  pdfsend =
      PdfElement.send sendPdfCommand

then you can call (makes a Cmd):

  pdfsend <|
      PdfElement.OpenString
          { name = "mypdf"
          , string = dta
          }

decodeMsg : Json.Decode.Decoder PdfMsg

decode incoming messages from the websocket javascript.

encodeCmd : PdfCmd -> Json.Encode.Value

encode websocket commands into json.