diff --git a/readme.md b/readme.md index afc020c..35deac8 100644 --- a/readme.md +++ b/readme.md @@ -91,7 +91,7 @@ static void AddPage(PageDescriptor page) }); } ``` -snippet source | anchor +snippet source | anchor @@ -139,6 +139,27 @@ public Task VerifyDocument() +## 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`](https://github.com/VerifyTests/Verify/blob/main/docs/converter.md#excluding-targets) drops it from a verification and skips the generation, while the rendered pages and info still verify: + + + +```cs +[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: @@ -154,7 +175,7 @@ public Task PagesToInclude() .PagesToInclude(1); } ``` -snippet source | anchor +snippet source | anchor @@ -173,5 +194,5 @@ public Task PagesToIncludeDynamic() .PagesToInclude(pageNumber => pageNumber == 2); } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 943a3a5..abc76b3 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;NU1608;NU1109 - 2.6.0 + 2.7.0 1.0.0 preview enable diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index e970e3c..27daceb 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -11,12 +11,12 @@ - + - + - + diff --git a/src/Tests/Samples.ExcludePdf#00.verified.png b/src/Tests/Samples.ExcludePdf#00.verified.png new file mode 100644 index 0000000..b2c2fac Binary files /dev/null and b/src/Tests/Samples.ExcludePdf#00.verified.png differ diff --git a/src/Tests/Samples.ExcludePdf#01.verified.png b/src/Tests/Samples.ExcludePdf#01.verified.png new file mode 100644 index 0000000..0c9b249 Binary files /dev/null and b/src/Tests/Samples.ExcludePdf#01.verified.png differ diff --git a/src/Tests/Samples.ExcludePdf.verified.txt b/src/Tests/Samples.ExcludePdf.verified.txt new file mode 100644 index 0000000..2de2447 --- /dev/null +++ b/src/Tests/Samples.ExcludePdf.verified.txt @@ -0,0 +1,10 @@ +{ + Pages: 2, + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } +} \ No newline at end of file diff --git a/src/Tests/Samples.cs b/src/Tests/Samples.cs index e73c319..95357c3 100644 --- a/src/Tests/Samples.cs +++ b/src/Tests/Samples.cs @@ -45,6 +45,18 @@ public Task VerifyDocumentWithMetadata() #endregion + #region ExcludePdf + + [Test] + public Task ExcludePdf() + { + var document = GenerateDocument(); + return Verify(document) + .ExcludeTargets("pdf"); + } + + #endregion + #region PagesToInclude [Test] diff --git a/src/Verify.QuestPDF/VerifyQuestPdf.cs b/src/Verify.QuestPDF/VerifyQuestPdf.cs index 1fd0a30..7979e64 100644 --- a/src/Verify.QuestPDF/VerifyQuestPdf.cs +++ b/src/Verify.QuestPDF/VerifyQuestPdf.cs @@ -32,20 +32,23 @@ public static void Initialize() pagesToInclude = _ => true; } // QuestPDF stamps DateTimeOffset.Now into the PDF CreationDate/ModifiedDate on - // every generation. Pin them to a fixed value so the pdf target is byte-stable. + // every generation. Pin them to a fixed value so the pdf target and the info are stable. var metadata = document.GetMetadata(); metadata.CreationDate = deterministicDate; metadata.ModifiedDate = deterministicDate; - // The pdf snapshot is always the full document, regardless of PagesToInclude: - // PagesToInclude only trims the rendered png pages below. - var pdf = document.GeneratePdf(); - List targets = - [ - new("pdf", new MemoryStream(pdf), performConversion:false) - { - BypassComparersForSubsequentOnDifference = true - } - ]; + List targets = []; + // Generating the pdf is expensive, so skip it entirely when the pdf target is excluded. + // The rendered pages and info are unaffected. The pdf snapshot is always the full + // document, regardless of PagesToInclude: PagesToInclude only trims the png pages below. + if (!settings.IsTargetExcluded("pdf")) + { + var pdf = document.GeneratePdf(); + targets.Add( + new("pdf", new MemoryStream(pdf), performConversion: false) + { + BypassComparersForSubsequentOnDifference = true + }); + } for (var index = 0; index < pages.Count; index++) {