Skip to content

Latest commit

 

History

875 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.QuestPDF

Discussions Build status NuGet Status

Extends Verify to allow verification of documents via QuestPDF.

See Milestones for release notes.

Designed to help assert the output of projects using QuestPDF to generate PDFs.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Usage

[ModuleInitializer]
public static void Init()
{
    VerifierSettings.UseSsimForPng();
    VerifyQuestPdf.Initialize();
}

snippet source | anchor

This sample uses Verify.ImageMagick to ignore small rendering differences that are expected between differens operating systesm.

Other compares options:

Outputs

Initialize accepts an optional QuestPdfOutputs to control which outputs a document is split into:

  • Png: render each page to a png. When omitted, pages are not rasterized at all.
  • None: none of the above. Only the info and the source document are emitted.
  • All: the default.

The pdf target is not controlled by this setting. Use VerifierSettings.ExcludeTargets("pdf") to exclude it.

[ModuleInitializer]
public static void Init()
{
    QuestPDF.Settings.License = LicenseType.Community;
    // Skip rendering pages to png
    VerifyQuestPdf.Initialize(QuestPdfOutputs.None);
}

snippet source | anchor

Code that generates a document

internal static IDocument GenerateDocument() =>
    Document.Create(container =>
    {
        container.Page(AddPage);
        container.Page(AddPage);
    });

static void AddPage(PageDescriptor page)
{
    page.Size(PageSizes.A5);
    page.Margin(1, Unit.Centimetre);
    page.PageColor(Colors.Grey.Lighten3);
    page.DefaultTextStyle(_ => _.FontSize(20));

    page.Header()
        .Text("Hello PDF!")
        .SemiBold().FontSize(36);

    page.Content()
        .Column(_ => _.Item()
            .Text(Placeholders.LoremIpsum()));

    page.Footer()
        .AlignCenter()
        .Text(_ =>
        {
            _.Span("Page ");
            _.CurrentPageNumber();
        });
}

snippet source | anchor

Verify a Document

[Test]
public Task VerifyDocument()
{
    var document = GenerateDocument();
    return Verify(document);
}

snippet source | anchor

Results

Metadata

{
  Pages: 2,
  Settings: {
    ContentDirection: LeftToRight,
    PDFA_Conformance: None,
    PDFUA_Conformance: None,
    ImageCompressionQuality: High,
    ImageRasterDpi: 288
  }
}

snippet source | anchor

Pdf as image

Exclude the pdf

QuestPDF renders the source pdf, and it is included in the snapshot as a .verified.pdf. Generating it is expensive, and committing it is not always wanted. ExcludeTargets drops it from a verification and skips the generation, while the rendered pages and info still verify:

[Test]
public Task ExcludePdf()
{
    var document = GenerateDocument();
    return Verify(document)
        .ExcludeTargets("pdf");
}

snippet source | anchor

To exclude the pdf for every test, call VerifierSettings.ExcludeTargets("pdf") at initialization.

PagesToInclude

To render only a defined number of pages at the start of a document:

[Test]
public Task PagesToInclude()
{
    var document = GenerateDocument();
    return Verify(document)
        .PagesToInclude(1);
}

snippet source | anchor

Dynamic

To dynamically control what pages are rendered:

[Test]
public Task PagesToIncludeDynamic()
{
    var document = GenerateDocument();
    return Verify(document)
        .PagesToInclude(pageNumber => pageNumber == 2);
}

snippet source | anchor

About

Extends Verify to allow verification of documents via QuestPDF.

Resources

Code of conduct

Stars

16 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages