ninjarobot / FsPdf

Builds PDF's from pure F# (and needs a more interesting name)
MIT License
121 stars 4 forks source link

FsPdf

Builds PDF's from pure F# (and needs a more interesting name)

Build and Test FsPdf on Nuget

Generates PDF content to enable developers to build PDF's programatically. PDF's are helpful for offline documentation, printing, certificates of achievement, TPS reports, customer invoices, taxes, or being an actuary.

What works?

What's in progress

Then what?

Example

Here is an example of a PDF generated by this test.

F# API

A PdfFile type is the main type to create, and it contains document metadata as well as a "catalog" which holds the pages themselves. The catalog has a default page size that will apply to all pages unless they override it.

Each Page holds Resources (currently just fonts), the contents as a list of PDF stream instructions, and optionally can override the page media to have a different page size. Because the instructions themselves are low level, there are some helpful functions for building shapes, wrapping strings, etc.

The result looks roughly like this:

let pdf =
    {
        Catalog =
            {
                PageLayout = SinglePage
                DefaultMedia = Media.Letter
                Pages =
                    [
                        {
                            Resources =
                                Map.empty
                                |> Map.add "F1" (FontResource (Type1, "Helvetica"))
                            Contents =
                                [
                                    BeginText
                                    Leading (20)
                                    FontSize ("F1", 12.)
                                    NextLineTranslate (50, 600)
                                    ShowText "hello world"
                                    EndText
                                ]
                            MediaSize = Some (Letter)
                        }
                    ]
            }
        Info = None
    }

This creates a PDF with a single page. The page contains some text, set at 50 points from the left side and 600 points from the bottom of the page.