Output Formats

FolioPDF generates PDF, SVG, and raster image output from layout code, converts images to PDFs, and renders existing PDF pages to PNG, JPEG, or WebP.

Overview

FormatAPIUse Case
PDF Document.GeneratePdf() Primary output — print, archive, distribute
SVG Document.GenerateSvg() Embed in HTML, vector editing, web previews
PNG / JPEG / WebP Document.GenerateImages() Thumbnails, social previews, email previews
Image → PDF ImageToPdfConverter.Convert() Scanned documents, photo collections
Existing PDF → Image PdfiumPage.RenderToPng() Page previews, OCR preprocessing, visual testing

PDF Output

Every document created with Document.Create can be output as PDF in three ways:

using FolioPDF;
using FolioPDF.Fluent;
using FolioPDF.Helpers;

var document = Document.Create(doc =>
{
    doc.Page(page =>
    {
        page.Size(PageSizes.A4);
        page.Margin(50);
        page.Content().Text("Hello, PDF!").FontSize(20);
    });
});

// 1. Save to file
document.GeneratePdf("output.pdf");

// 2. Write to stream (e.g. ASP.NET response body)
using var stream = new MemoryStream();
document.GeneratePdf(stream);

// 3. Get raw bytes (e.g. store in database, email attachment)
byte[] pdfBytes = document.GeneratePdf();

GeneratePdfAndShow

During development, open the PDF immediately in the system default viewer:

document.GeneratePdfAndShow();

This writes a temporary file and launches it with Process.Start(UseShellExecute = true). The temp file is not cleaned up automatically — the OS handles it.

SVG Output

Generate self-contained SVG markup for each page. Each string is a complete <svg> document with embedded fonts and base64-encoded images — no external dependencies.

// Get SVG strings (one per page)
IReadOnlyList<string> svgPages = document.GenerateSvg();

// Display first page in a Blazor component, Razor view, etc.
string firstPageSvg = svgPages[0];

// Save each page to a file
document.GenerateSvg(pageIndex => $"page_{pageIndex + 1}.svg");

SVG output uses Skia's SVG canvas backend. Text is emitted as <text> elements with embedded font data; images as inline base64 <image> elements.

SVG is best for single-page content. Multi-page documents produce one SVG per page. If you need a single file for the whole document, use PDF. SVG is ideal for web previews, embedding in HTML, or vector editing workflows.

Image Output

Rasterize each page of a document to PNG, JPEG, or WebP images.

ImageGenerationSettings

PropertyTypeDefaultDescription
ImageFormat ImageFormat Png Output format: Png, Jpeg, or Webp
RasterDpi int 150 Resolution in dots per inch. 72 = screen, 150 = web preview, 300 = print quality
Quality int 90 JPEG/WebP compression quality (1–100). Ignored for PNG.
// PNG at 300 DPI (print quality)
var pngImages = document.GenerateImages(new ImageGenerationSettings
{
    ImageFormat = ImageFormat.Png,
    RasterDpi = 300
});

foreach (var (imageBytes, index) in pngImages.Select((b, i) => (b, i)))
    File.WriteAllBytes($"page_{index + 1}.png", imageBytes);

// JPEG at 150 DPI, 80% quality (web preview)
var jpegImages = document.GenerateImages(new ImageGenerationSettings
{
    ImageFormat = ImageFormat.Jpeg,
    RasterDpi = 150,
    Quality = 80
});

// WebP at 200 DPI (modern web, smallest files)
var webpImages = document.GenerateImages(new ImageGenerationSettings
{
    ImageFormat = ImageFormat.Webp,
    RasterDpi = 200,
    Quality = 85
});

// Save to files with a callback
document.GenerateImages(
    pageIndex => $"output/page_{pageIndex + 1}.png",
    new ImageGenerationSettings { RasterDpi = 150 });

ImageFormat Enum

ValueDescriptionCompression
PngPortable Network GraphicsLossless
JpegJPEG (best for photos)Lossy
WebpWebP (modern, smallest files)Lossy
GifGIF (palette-based)Lossless (256 colours)
BmpBitmap (uncompressed)None
TiffTagged Image File FormatVarious

DPI and pixel dimensions. An A4 page (595 × 842 pt) at 150 DPI produces a 1240 × 1754 pixel image. At 300 DPI, it doubles to 2480 × 3508 pixels. Higher DPI means better quality but larger files and slower rendering.

Image-to-PDF Conversion

Convert raster images (PNG, JPEG, WebP, BMP, GIF) into PDF documents. Each image becomes one page.

using FolioPDF.Toolkit;

// Single image
byte[] pdf = ImageToPdfConverter.Convert("photo.jpg");

// Multiple images → multi-page PDF
byte[] pdf = ImageToPdfConverter.Convert("scan1.png", "scan2.png", "scan3.png");

// From byte arrays
byte[] pdf = ImageToPdfConverter.Convert(new[] { imageBytes1, imageBytes2 });

// Save directly to file
ImageToPdfConverter.ConvertToFile("scans.pdf", "scan1.png", "scan2.png");

// Fit images to A4 with margins
byte[] pdf = ImageToPdfConverter.Convert(
    new[] { "scan1.jpg", "scan2.jpg" },
    new ImageToPdfOptions
    {
        PageSize = PageSizes.A4,
        Margin = 40
    });

ImageToPdfOptions

PropertyTypeDefaultDescription
PageSize PageSize? null When null, each page is sized to the image's natural dimensions (pixels = PDF points at 72 DPI). When set, images are scaled to fit.
Margin float 0 Margin in PDF points applied on all sides. Only used when PageSize is set.

PdfEditor Output

When editing existing PDFs with PdfEditor, output the result as bytes or save to a file:

using FolioPDF.Fluent;

// Save to file
PdfEditor.Open("input.pdf")
    .SetTitle("Updated Title")
    .Save("output.pdf");

// Get bytes (e.g. for HTTP response)
byte[] result = PdfEditor.Open("input.pdf")
    .SetTitle("Updated Title")
    .ToBytes();

PdfiumPage Rendering

Render individual pages of an existing PDF to raster images. This uses PDFium for parsing and rasterization, then Skia for image encoding.

using FolioPDF.Toolkit.Pdfium;

using var doc = PdfiumDocument.Load(File.ReadAllBytes("report.pdf"));
using var page = doc.GetPage(0);

// PNG at 150 DPI
byte[] png = page.RenderToPng(dpi: 150);
File.WriteAllBytes("page1.png", png);

// PNG at fixed pixel dimensions (e.g. 200x282 thumbnail)
byte[] thumbnail = page.RenderToPng(widthPx: 200, heightPx: 282);

// JPEG at 200 DPI, quality 85
byte[] jpeg = page.RenderToJpeg(dpi: 200, quality: 85);
File.WriteAllBytes("page1.jpg", jpeg);

// JPEG at fixed pixel dimensions
byte[] jpegThumb = page.RenderToJpeg(widthPx: 400, heightPx: 566, quality: 80);

RenderToPng Overloads

SignatureDescription
RenderToPng(int dpi = 150) Render at the specified DPI. Pixel dimensions are computed from page size and DPI.
RenderToPng(int widthPx, int heightPx) Render at exact pixel dimensions. Useful for thumbnails of a specific size.

RenderToJpeg Overloads

SignatureDescription
RenderToJpeg(int dpi = 150, int quality = 85) Render at the specified DPI with JPEG quality 1–100.
RenderToJpeg(int widthPx, int heightPx, int quality = 85) Render at exact pixel dimensions with JPEG quality.

When to Use Each Format

ScenarioRecommended FormatAPI
Print, archive, distribute PDF GeneratePdf()
Embed in web page / HTML email SVG or PNG GenerateSvg() or GenerateImages()
Thumbnail for file browser JPEG (small) or PNG (crisp) PdfiumPage.RenderToJpeg()
Vector editing (Illustrator, Figma) SVG GenerateSvg()
Scan documents into PDF Image → PDF ImageToPdfConverter.Convert()
Social media / Open Graph preview PNG or WebP GenerateImages()
OCR preprocessing PNG (lossless) PdfiumPage.RenderToPng(dpi: 300)
ASP.NET download endpoint PDF (bytes → FileResult) GeneratePdf()File(bytes, "application/pdf")