1
47%
MIT
react-pdf bindings for bucklescript

bs-react-pdf npm version

react-pdf bindings for bucklescript

Installation

  1. $ npm i bs-react-pdf
  2. Add bs-react-pdf to bs-dependencies section of your bsconfig.json

Examples

Document

  • RE
  • ML
open ReactPdf.Core;

let styles =
  StyleSheet.create({
    "page": {"flexDirection": "row", "backgroundColor": "#fff"},
    "section": {"margin": 10, "padding": 10, "flexGrow": 1}
  });

let component = ReasonReact.reducerComponent("MyDocument");

let make = (_children) => {
  ...component,
  reducer: ((), _state: unit) => ReasonReact.NoUpdate,
  render: (_self) =>
    <Document>
      <Page size="A4" style=styles##page>
        <View style=styles##section>
          <Text key="hello"> (ReasonReact.stringToElement("Section #1")) </Text>
        </View>
        <View style=styles##section>
          <Text> (ReasonReact.stringToElement("Section #2")) </Text>
        </View>
      </Page>
    </Document>
};
open ReactPdf.Core
let styles =
  StyleSheet.create
    ([%bs.obj
       {
         page =
           ([%bs.obj { flexDirection = "row"; backgroundColor = "#fff" }]);
         section = ([%bs.obj { margin = 10; padding = 10; flexGrow = 1 }])
       }])
let component = ReasonReact.reducerComponent "MyDocument"
let make _children =
  {
    component with
    reducer = (fun ()  -> fun (_state : unit)  -> ReasonReact.NoUpdate);
    render =
      (fun _self  ->
         ((Document.createElement
             ~children:[((Page.createElement ~size:"A4"
                            ~style:(## styles page)
                            ~children:[((View.createElement
                                           ~style:(## styles section)
                                           ~children:[((Text.createElement
                                                          ~key:"hello"
                                                          ~children:[
                                                                    ReasonReact.stringToElement
                                                                    "Section #1"]
                                                          ())[@JSX ])] ())
                                      [@JSX ]);
                                      ((View.createElement
                                          ~style:(## styles section)
                                          ~children:[((Text.createElement
                                                         ~children:[ReasonReact.stringToElement
                                                                    "Section #2"]
                                                         ())[@JSX ])] ())
                                      [@JSX ])] ())[@JSX ])] ())[@JSX ]))
  }

Save in a file

  • RE
  • ML
ReactPdfNode.render(<MyDocument />, "example.pdf")
|> Js.Promise.then_(() => Js.Promise.resolve @@ Js.log("Pdf created"));
let _ =
  (ReactPdfNode.render ((MyDocument.createElement ~children:[] ())[@JSX ])
     "example.pdf")
    |>
    (Js.Promise.then_
       (fun ()  -> Js.Promise.resolve @@ (Js.log "Pdf created")))