Skip to content

Api2Pdf/api2pdf.dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

api2pdf.dotnet

.NET bindings for the Api2Pdf REST API.

Api2Pdf.com is a powerful REST API for document generation, file conversion, and automated content extraction in .NET applications. It supports HTML to PDF, URL to PDF, HTML to image, URL to image, Microsoft Office document conversion, email and image file conversion, PDF page extraction, PDF password protection, file zipping, barcode and QR code generation, markdown conversion, structured PDF data extraction, and image previews or thumbnails for PDF, office, and email files. Api2Pdf is built on proven engines and libraries including wkhtmltopdf, Headless Chrome, PdfSharp, LibreOffice, and related tools to provide reliable PDF generation, document processing, and file transformation workflows through a single API.

The package targets netstandard2.0 and wraps the current Api2Pdf service groups for Chrome, wkhtmltopdf, LibreOffice, Markitdown, OpenDataLoader, PdfSharp, Zip, Zebra, and utility endpoints.

Installation

dotnet add package Api2Pdf
Install-Package Api2Pdf -Version 2.2.0

Quick Start

Create an account at portal.api2pdf.com to get your API key.

using Api2Pdf;

var client = new Api2Pdf("YOUR-API-KEY");

var result = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
    Html = "<html><body><h1>Hello, world!</h1></body></html>"
});

if (result.Success)
{
    Console.WriteLine(result.FileUrl);
}
else
{
    Console.WriteLine(result.Error);
}

Async usage works the same way:

using Api2Pdf;

var client = new Api2Pdf("YOUR-API-KEY");

var result = await client.Chrome.UrlToPdfAsync(new ChromeUrlToPdfRequest
{
    Url = "https://www.api2pdf.com"
});

Custom Domains

The default constructor uses https://v2.api2pdf.com.

If you want to route requests to a different Api2Pdf domain, use the apiKey + baseUrl constructor:

using Api2Pdf;

var client = new Api2Pdf("YOUR-API-KEY", "https://your-custom-domain.api2pdf.com");

The package also exposes a constant for the XL cluster:

using Api2Pdf;

var client = new Api2Pdf("YOUR-API-KEY", Api2PdfBaseUrls.V2Xl);

v2-xl.api2pdf.com provides much larger compute resources and is intended for heavier workloads, with additional cost compared to the default cluster.

Understanding Responses

Most API methods return an Api2PdfResult.

{
  "fileUrl": "https://link-to-file-available-for-24-hours",
  "mbOut": 0.08830547332763672,
  "cost": 0.00017251586914062501,
  "seconds": 2,
  "success": true,
  "error": null,
  "responseId": "6e46637a-650d-46d5-af0b-3d7831baccbb"
}

Important members:

  • Success: whether the request succeeded.
  • Error: error text when the request fails.
  • FileUrl: URL of the generated file when the API returns standard JSON.
  • ResponseId: identifier used by Utilities.Delete(...).
  • GetFileBytes(): returns bytes already in memory for binary responses, or downloads from FileUrl when needed.

Common Request Features

Most request models support these properties:

  • FileName: set the output file name.
  • Inline: true to open in the browser, false to trigger download behavior.
  • UseCustomStorage and Storage: send the output directly to your own storage target.
  • OutputBinary: when the endpoint supports it, request binary content instead of the standard JSON payload.
  • ExtraHTTPHeaders: forward custom headers when Api2Pdf fetches a source URL.

Example custom storage configuration:

var request = new ChromeHtmlToPdfRequest
{
    Html = "<p>Hello World</p>",
    UseCustomStorage = true,
    Storage = new CustomStorageOptions
    {
        Method = "PUT",
        Url = "https://your-presigned-upload-url"
    }
};

Chrome

HTML or URL to PDF

var htmlPdf = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
    Html = "<p>Hello World</p>",
    Options = new ChromeHtmlToPdfOptions
    {
        Delay = 3000,
        DisplayHeaderFooter = true,
        HeaderTemplate = "<div style=\"font-size:12px;\">Header</div>",
        FooterTemplate = "<div style=\"font-size:12px;\">Footer</div>",
        Landscape = true,
        PreferCSSPageSize = true
    }
});

var urlPdf = client.Chrome.UrlToPdf(new ChromeUrlToPdfRequest
{
    Url = "https://www.api2pdf.com",
    ExtraHTTPHeaders = new Dictionary<string, string>
    {
        ["Authorization"] = "Bearer token-for-the-source-site"
    },
    Options = new ChromeUrlToPdfOptions
    {
        PuppeteerWaitForMethod = "WaitForNavigation",
        PuppeteerWaitForValue = "Load"
    }
});

For simple GET-style URL conversion, you can also call:

var result = client.Chrome.UrlToPdf("https://www.api2pdf.com");

Markdown to PDF

var result = client.Chrome.MarkdownToPdf(new ChromeMarkdownToPdfRequest
{
    Markdown = "# Invoice\n\nThis PDF was generated from markdown."
});

HTML, URL, or Markdown to image

var htmlImage = client.Chrome.HtmlToImage(new ChromeHtmlToImageRequest
{
    Html = "<p>Hello image</p>",
    Options = new ChromeHtmlToImageOptions
    {
        FullPage = true,
        ViewPortOptions = new ViewPortOptions
        {
            Width = 1440,
            Height = 900
        }
    }
});

var urlImage = client.Chrome.UrlToImage("https://www.api2pdf.com");

var markdownImage = client.Chrome.MarkdownToImage(new ChromeMarkdownToImageRequest
{
    Markdown = "# Screenshot\n\nGenerated from markdown."
});

Wkhtmltopdf

var htmlPdf = client.Wkhtml.HtmlToPdf(new WkhtmlHtmlToPdfRequest
{
    Html = "<p>Hello World</p>",
    EnableToc = true,
    Options = new Dictionary<string, string>
    {
        ["orientation"] = "landscape",
        ["pageSize"] = "Letter"
    },
    TocOptions = new Dictionary<string, string>
    {
        ["disableDottedLines"] = "true"
    }
});

var urlPdf = client.Wkhtml.UrlToPdf(new WkhtmlUrlToPdfRequest
{
    Url = "https://www.api2pdf.com"
});

For the simple GET route:

var result = client.Wkhtml.UrlToPdf("https://www.api2pdf.com");

LibreOffice

Use LibreOffice endpoints for file and Office conversions.

Convert a file URL to PDF:

var result = client.LibreOffice.AnyToPdf(new LibreFileConversionRequest
{
    Url = "https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
});

Generate a thumbnail:

var result = client.LibreOffice.Thumbnail(new LibreFileConversionRequest
{
    Url = "https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
});

Convert HTML or a URL to DOCX or XLSX:

var docx = client.LibreOffice.HtmlToDocx(new LibreHtmlOrUrlConversionRequest
{
    Html = "<html><body><h1>Hello Word</h1></body></html>"
});

var xlsx = client.LibreOffice.HtmlToXlsx(new LibreHtmlOrUrlConversionRequest
{
    Url = "https://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html"
});

Markitdown

Convert a file URL to markdown:

var result = client.Markitdown.ConvertToMarkdown(new MarkitdownRequest
{
    Url = "https://example.com/sample.docx"
});

OpenDataLoader

Extract structured content from a PDF URL:

var json = client.OpenDataLoader.PdfToJson(new OpenDataLoaderRequest
{
    Url = "https://example.com/sample.pdf"
});

var markdown = client.OpenDataLoader.PdfToMarkdown(new OpenDataLoaderRequest
{
    Url = "https://example.com/sample.pdf"
});

var html = client.OpenDataLoader.PdfToHtml(new OpenDataLoaderRequest
{
    Url = "https://example.com/sample.pdf"
});

PdfSharp

Merge PDFs:

var result = client.PdfSharp.MergePdfs(new PdfMergeRequest
{
    Urls = new List<string>
    {
        "https://LINK-TO-PDF-1",
        "https://LINK-TO-PDF-2"
    }
});

Set a password:

var result = client.PdfSharp.SetPassword(new PdfPasswordRequest
{
    Url = "https://LINK-TO-PDF",
    UserPassword = "password",
    OwnerPassword = "owner-password"
});

Extract a page range:

var result = client.PdfSharp.ExtractPages(new PdfExtractPagesRequest
{
    Url = "https://LINK-TO-PDF",
    Start = 0,
    End = 2
});

Zip

Create a zip from multiple files:

var result = client.Zip.GenerateZip(new ZipRequest
{
    Files = new List<ZipFileInfo>
    {
        new ZipFileInfo
        {
            Url = "https://example.com/report.pdf",
            FileName = "docs/report.pdf"
        },
        new ZipFileInfo
        {
            Url = "https://example.com/image.png",
            FileName = "images/image.png"
        }
    },
    OutputBinary = true
});

byte[] zipBytes = result.GetFileBytes();

Zebra

Generate a barcode or QR code:

var result = client.Zebra.GenerateBarcode(new ZebraRequest
{
    Format = "QR_CODE",
    Value = "https://www.api2pdf.com",
    Width = 300,
    Height = 300,
    ShowLabel = false
});

Utilities

Delete a generated file:

var result = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
    Html = "<p>Hello World</p>"
});

client.Utilities.Delete(result.ResponseId);

Check status or remaining balance:

string status = client.Utilities.Status();
string balance = client.Utilities.Balance();

Working With Files

Save to disk:

var result = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
    Html = "<p>Hello World</p>"
});

result.SaveFile("path-to-local-file.pdf");

Get bytes directly:

var result = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
    Html = "<p>Hello World</p>",
    OutputBinary = true
});

byte[] bytes = result.GetFileBytes();

Development

The repo uses a conventional layout:

  • src/Api2Pdf for the shipping library
  • tests/Api2Pdf.Tests for contract and regression tests

Resources

About

C# client library for the Api2Pdf.com REST API - Convert HTML to PDF, URL to PDF, Office Docs to PDF, Merge PDFs, HTML to Image, URL to Image, HTML to Docx, HTML to Xlsx, PDF to HTML, Thumbnail preview of office files

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages