F# PDF Library (Full Tutorial)

This tutorial will guide you through the steps of how to create and edit PDF files in F# using IronPDF. All you will need is Visual Studio installed and an F# project.

To see how to use IronPDF with C# see this guide.

To see how to use IronPDF with VB.NET see this guide.

Install the F# PDF Library

Install via NuGet Package Manager

In Visual Studio, right-click on your project solution explorer and select "Manage NuGet Packages...". From there simply search for IronPDF and install the latest version. Click OK to any dialog boxes that come up. This will work in any .NET Project.

Install via NuGet Package Manager Console

You may also choose to add IronPDF through the package manager console which can be done with this command:

Install-Package IronPdf

Install Directly in the .fsproj

Another option is to paste the following ItemGroup into your .fsproj file:

<ItemGroup>
  <PackageReference Include="IronPdf" Version="*" />
</ItemGroup>
<ItemGroup>
  <PackageReference Include="IronPdf" Version="*" />
</ItemGroup>
XML

Install via DLL

Alternatively, the IronPDF DLL can be downloaded and manually installed to the project or GAC from https://ironpdf.com/packages/IronPdf.zip

Remember to add this statement to the top of any .fs class file using IronPDF:

open IronPdf
open IronPdf
F#

Create a PDF from HTML with F

Begin by opening the IronPDF library within your namespace using open. After that, create a ChromePdfRenderer object and pass an HTML string into its RenderHtmlAsPdf method. If you already have an HTML file ready, you can pass the file path string as a parameter to RenderHtmlFileAsPdf.

HTML String to PDF in F

open IronPdf

let html = "<p>Hello World</p>"

// Initialize the PDF Renderer
let renderer = ChromePdfRenderer()

// Render HTML as PDF
let pdf = html |> renderer.RenderHtmlAsPdf

// Save the PDF document
pdf.SaveAs("document.pdf") |> ignore
open IronPdf

let html = "<p>Hello World</p>"

// Initialize the PDF Renderer
let renderer = ChromePdfRenderer()

// Render HTML as PDF
let pdf = html |> renderer.RenderHtmlAsPdf

// Save the PDF document
pdf.SaveAs("document.pdf") |> ignore
F#

HTML File to PDF in F

open IronPdf

let htmlFilePath = "C:/designs/html/layout.html"

// Initialize the PDF Renderer
let renderer = ChromePdfRenderer()

// Render HTML file as PDF
let pdf = htmlFilePath |> renderer.RenderHtmlFileAsPdf

// Save the PDF document
pdf.SaveAs("document.pdf") |> ignore
open IronPdf

let htmlFilePath = "C:/designs/html/layout.html"

// Initialize the PDF Renderer
let renderer = ChromePdfRenderer()

// Render HTML file as PDF
let pdf = htmlFilePath |> renderer.RenderHtmlFileAsPdf

// Save the PDF document
pdf.SaveAs("document.pdf") |> ignore
F#

Advanced IronPDF F# Template

Here is a more advanced example of creating a function that will format and style a PDF from a URL based on some rules and procedures:

open IronPdf

let CreateCompanyStandardDocument (url : string) =

    // Setup Render Options with desired settings
    let renderOptions = ChromePdfRenderOptions(
        CssMediaType = Rendering.PdfCssMediaType.Screen,
        EnableJavaScript = true,
        PrintHtmlBackgrounds = true,
        InputEncoding = System.Text.Encoding.UTF8,
        MarginTop = 0.39,
        MarginBottom = 0.38,
        MarginLeft = 0.39,
        MarginRight = 0.38
    )

    // Create Header Template for the PDF
    let companyStyleHeader = HtmlHeaderFooter()
    companyStyleHeader.HtmlFragment <- "<img src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'>"
    companyStyleHeader.DrawDividerLine <- true

    // Apply the header to the Render Options
    renderOptions.HtmlHeader <- companyStyleHeader

    // Initialize Renderer with customized options
    let renderer = ChromePdfRenderer(RenderingOptions = renderOptions)

    // Generate PDF from URL without additional styles
    let htmlPdfWithoutStyle = url |> renderer.RenderUrlAsPdf

    // Add the styled header to the PDF document
    htmlPdfWithoutStyle.AddHtmlHeaders companyStyleHeader |> ignore

    // Return the created PDF document
    htmlPdfWithoutStyle

let IronPdfUrlToPdf (url : string) =
    // Create a styled PDF document from the given URL
    let pdf = url |> CreateCompanyStandardDocument

    // Save the PDF document to the file system
    pdf.SaveAs("document.pdf") |> ignore

// Set your IronPDF License Key
IronPdf.License.LicenseKey <- "YOUR_LICENSE_KEY_HERE"

// Example usage: Convert the given URL to a PDF document
IronPdfUrlToPdf "https://ironpdf.com/"
open IronPdf

let CreateCompanyStandardDocument (url : string) =

    // Setup Render Options with desired settings
    let renderOptions = ChromePdfRenderOptions(
        CssMediaType = Rendering.PdfCssMediaType.Screen,
        EnableJavaScript = true,
        PrintHtmlBackgrounds = true,
        InputEncoding = System.Text.Encoding.UTF8,
        MarginTop = 0.39,
        MarginBottom = 0.38,
        MarginLeft = 0.39,
        MarginRight = 0.38
    )

    // Create Header Template for the PDF
    let companyStyleHeader = HtmlHeaderFooter()
    companyStyleHeader.HtmlFragment <- "<img src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'>"
    companyStyleHeader.DrawDividerLine <- true

    // Apply the header to the Render Options
    renderOptions.HtmlHeader <- companyStyleHeader

    // Initialize Renderer with customized options
    let renderer = ChromePdfRenderer(RenderingOptions = renderOptions)

    // Generate PDF from URL without additional styles
    let htmlPdfWithoutStyle = url |> renderer.RenderUrlAsPdf

    // Add the styled header to the PDF document
    htmlPdfWithoutStyle.AddHtmlHeaders companyStyleHeader |> ignore

    // Return the created PDF document
    htmlPdfWithoutStyle

let IronPdfUrlToPdf (url : string) =
    // Create a styled PDF document from the given URL
    let pdf = url |> CreateCompanyStandardDocument

    // Save the PDF document to the file system
    pdf.SaveAs("document.pdf") |> ignore

// Set your IronPDF License Key
IronPdf.License.LicenseKey <- "YOUR_LICENSE_KEY_HERE"

// Example usage: Convert the given URL to a PDF document
IronPdfUrlToPdf "https://ironpdf.com/"
F#

Frequently Asked Questions

How can I generate a PDF file in F# using a library?

To generate a PDF file in F#, you can use the IronPDF library. Start by installing IronPDF via NuGet Package Manager, NuGet Package Manager Console, or by adding the DLL directly. Use the ChromePdfRenderer object and call its RenderHtmlAsPdf method with your HTML content.

How do I install a PDF library in an F# project?

You can install the IronPDF library in an F# project through the NuGet Package Manager by searching for IronPDF and installing it. Alternatively, you can use the NuGet Package Manager Console, edit the .fsproj file directly, or manually add the IronPDF DLL to your project.

Can I convert an HTML string to PDF using F#?

Yes, you can convert an HTML string to a PDF in F# using IronPDF. Initialize a ChromePdfRenderer object and use the RenderHtmlAsPdf method with your HTML string to create a PDF document.

How do I convert an HTML file to PDF in F#?

To convert an HTML file to PDF in F#, utilize IronPDF's ChromePdfRenderer and call the RenderHtmlFileAsPdf method, passing in the file path of your HTML file.

What are some advanced features for PDF styling in F#?

IronPDF supports advanced PDF styling in F#. You can use ChromePdfRenderOptions to set custom render options, like CSS media type, JavaScript execution, and margins. You can also add HTML headers and footers for more professional-looking documents.

How can I add a header to a PDF in F#?

In F#, you can add a header to a PDF using IronPDF by creating an HtmlHeaderFooter object, setting its properties like HtmlFragment, and applying it to the ChromePdfRenderOptions before rendering your PDF.

How do I use a license key with a PDF library in F#?

To use a license key with IronPDF in F#, assign your license key string to the IronPdf.License.LicenseKey property in your F# code.

How can I create a PDF from a URL in F#?

With IronPDF, you can create a PDF from a URL in F# by initializing a ChromePdfRenderer with desired render options, and using the RenderUrlAsPdf method with the URL you want to convert.

What development environment is recommended for F# and PDF libraries?

The recommended development environment for using IronPDF with F# is Visual Studio. It provides comprehensive tools for package management, code editing, and project building, making it suitable for F# projects involving PDF creation.

Is it possible to edit existing PDFs with this library in F#?

Yes, IronPDF allows you to edit existing PDFs in F#. You can modify PDF content, add headers or footers, and apply additional styling using the library's API functions.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.