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

What is this library used for PDF creation?

IronPDF is a library that enables the creation and editing of PDF files within .NET applications, including F# projects. It allows developers to convert HTML to PDF while retaining formatting.

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

You can install IronPDF in an F# project via NuGet Package Manager, NuGet Package Manager Console, by modifying the .fsproj file directly, or by downloading and manually adding the DLL to your project.

Can I use this PDF library with other .NET languages?

Yes, IronPDF can be used with other .NET languages such as C# and VB.NET. There are specific guides available for these languages.

How do I convert an HTML string to PDF using this library in F#?

To convert an HTML string to PDF in F#, use the ChromePdfRenderer object from IronPDF. Call the RenderHtmlAsPdf method with your HTML string and then save the resulting PDF document.

Is it possible to convert an HTML file to PDF with this library in F#?

Yes, you can convert an HTML file to PDF using IronPDF in F#. Use the ChromePdfRenderer's RenderHtmlFileAsPdf method with the file path as a parameter.

What are the advanced features of this PDF library for F#?

IronPDF for F# allows advanced features such as setting custom render options, adding headers and footers, and applying CSS styles and JavaScript to the PDF rendering process.

How do I use a license key with this PDF library?

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

Can I customize PDF document styles with this library in F#?

Yes, IronPDF allows you to customize PDF document styles in F# by setting options like CSS media type, margins, and input encoding using ChromePdfRenderOptions.

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

You can create a PDF from a URL in F# using IronPDF by initializing a ChromePdfRenderer with desired render options and calling the RenderUrlAsPdf method with the URL.

What is the recommended development environment for using this PDF library with F#?

The recommended development environment for using IronPDF with F# is Visual Studio, where you can manage packages, edit code, and build your project effectively.

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.