Are you an LLM? You can read better optimized documentation at /concepts/generating-output.md for this page in Markdown format
Generating output
The primary goal of the QuestPDF library is to generate PDF files. However, it also supports other output formats such as SVG and images.
WARNING
Please be aware that certain features may not be available on formats other than PDF.
Generating PDF files
There are several overloads for generating PDF files:
csharp
var document = Document.Create(document =>
{
document.Page(page =>
{
page.Content().Text("Your invoice content");
});
});
// generate PDF and save it to a file
document.GeneratePdf("document.pdf");
// generate PDF and return it as a byte array
var byteArray = document.GeneratePdf();
// generate PDF and save it to a stream
using var stream = new FileStream("document.pdf", FileMode.Create);
document.GeneratePdf(stream);Generating SVG files
The library also supports generating SVG files. Each page is represented as a separate SVG file.
csharp
ICollection<string> svgFiles = document.GenerateSvgFiles();Generating images
The library also supports generating images. Each page is represented as a separate image file.
csharp
// generate images and return them as byte arrays
IEnumerable<byte[]> imagesAsByteArrays = document.GenerateImages();
// save images to files
document.GenerateImages(imageIndex => $"image{imageIndex}.png");Optionally, you can provide additional generation settings:
| Property | Description |
|---|---|
| ImageFormat | The file format used to encode the images. Default is PNG. |
| ImageCompressionQuality | Encoding quality controls the trade-off between size and quality. The default value is high. |
| RasterDpi | The DPI (pixels-per-inch) at which the document will be rasterized. This controls the resolution of produced images. Higher DPI results in superior image quality but may increase the output file size. Default value is 288. |
| UseTransparentBackground | When enabled, the generated images have a transparent background instead of a default white one. Applies only to image formats that support transparency (PNG and WEBP). |
csharp
using QuestPDF.Infrastructure;
var imageGenerationSettings = new ImageGenerationSettings
{
ImageFormat = ImageFormat.Png,
ImageCompressionQuality = ImageCompressionQuality.High,
RasterDpi = 288,
UseTransparentBackground = false
};
IEnumerable<byte[]> imagesAsByteArrays = document.GenerateImages(imageGenerationSettings);
// or
document.GenerateImages(imageIndex => $"image{imageIndex}.png", imageGenerationSettings);Layout errors
When the document content contains size constraints that are impossible to meet, generation fails with the DocumentLayoutException. The library determines the element that most likely causes the problem and enriches the exception message with its location and layout measurements. This search is performed only when the document fails to generate, so it does not affect the performance of successfully generated documents.
WARNING
The message may include data from the document's content, such as fragments of TextBlock text. This helps with diagnosis, but may also expose private information, so take care when logging or forwarding these exceptions.
TIP
For enhanced development and debugging experience, please consider using the QuestPDF Companion App.
