diff --git a/QuestPDF/Drawing/DocumentGenerator.cs b/QuestPDF/Drawing/DocumentGenerator.cs index cd9b339..3fe373c 100644 --- a/QuestPDF/Drawing/DocumentGenerator.cs +++ b/QuestPDF/Drawing/DocumentGenerator.cs @@ -135,11 +135,10 @@ namespace QuestPDF.Drawing $"In this case, please increase the value {nameof(DocumentMetadata)}.{nameof(DocumentMetadata.DocumentLayoutExceptionThreshold)} property configured in the {nameof(IDocument.GetMetadata)} method. " + $"2) The layout configuration of your document is invalid. Some of the elements require more space than is provided." + $"Please analyze your documents structure to detect this element and fix its size constraints."; - - throw new DocumentLayoutException(message) - { - ElementTrace = debuggingState?.BuildTrace() ?? "Debug trace is available only in the DEBUG mode." - }; + + var elementTrace = debuggingState?.BuildTrace() ?? "Debug trace is available only in the DEBUG mode."; + + throw new DocumentLayoutException(message, elementTrace); } } diff --git a/QuestPDF/Drawing/Exceptions/DocumentComposeException.cs b/QuestPDF/Drawing/Exceptions/DocumentComposeException.cs index 972dbe8..eeefb4e 100644 --- a/QuestPDF/Drawing/Exceptions/DocumentComposeException.cs +++ b/QuestPDF/Drawing/Exceptions/DocumentComposeException.cs @@ -4,17 +4,7 @@ namespace QuestPDF.Drawing.Exceptions { public class DocumentComposeException : Exception { - public DocumentComposeException() - { - - } - - public DocumentComposeException(string message) : base(message) - { - - } - - public DocumentComposeException(string message, Exception inner) : base(message, inner) + internal DocumentComposeException(string message) : base(message) { } diff --git a/QuestPDF/Drawing/Exceptions/DocumentDrawingException.cs b/QuestPDF/Drawing/Exceptions/DocumentDrawingException.cs index cebb76c..a650d90 100644 --- a/QuestPDF/Drawing/Exceptions/DocumentDrawingException.cs +++ b/QuestPDF/Drawing/Exceptions/DocumentDrawingException.cs @@ -4,17 +4,7 @@ namespace QuestPDF.Drawing.Exceptions { public class DocumentDrawingException : Exception { - public DocumentDrawingException() - { - - } - - public DocumentDrawingException(string message) : base(message) - { - - } - - public DocumentDrawingException(string message, Exception inner) : base(message, inner) + internal DocumentDrawingException(string message, Exception inner) : base(message, inner) { } diff --git a/QuestPDF/Drawing/Exceptions/DocumentLayoutException.cs b/QuestPDF/Drawing/Exceptions/DocumentLayoutException.cs index ff83755..acdc1f0 100644 --- a/QuestPDF/Drawing/Exceptions/DocumentLayoutException.cs +++ b/QuestPDF/Drawing/Exceptions/DocumentLayoutException.cs @@ -4,21 +4,11 @@ namespace QuestPDF.Drawing.Exceptions { public class DocumentLayoutException : Exception { - public string ElementTrace { get; set; } - - public DocumentLayoutException() - { - - } + public string? ElementTrace { get; } - public DocumentLayoutException(string message) : base(message) + internal DocumentLayoutException(string message, string? elementTrace = null) : base(message) { - - } - - public DocumentLayoutException(string message, Exception inner) : base(message, inner) - { - + ElementTrace = elementTrace; } } } \ No newline at end of file diff --git a/QuestPDF/Drawing/Exceptions/InitializationException.cs b/QuestPDF/Drawing/Exceptions/InitializationException.cs new file mode 100644 index 0000000..5be5dd2 --- /dev/null +++ b/QuestPDF/Drawing/Exceptions/InitializationException.cs @@ -0,0 +1,20 @@ +using System; + +namespace QuestPDF.Drawing.Exceptions +{ + public class InitializationException : Exception + { + internal InitializationException(string documentType, Exception innerException) : base(CreateMessage(documentType), innerException) + { + + } + + private static string CreateMessage(string documentType) + { + return $"Cannot create the {documentType} document using the SkiaSharp library. " + + $"This exception usually means that, on your operating system where you run the application, SkiaSharp requires installing additional dependencies. " + + $"Such dependencies are available as additional nuget packages, for example SkiaSharp.NativeAssets.Linux. " + + $"Please refer to the SkiaSharp documentation for more details."; + } + } +} \ No newline at end of file diff --git a/QuestPDF/Drawing/PdfCanvas.cs b/QuestPDF/Drawing/PdfCanvas.cs index ac703eb..cdc6a2d 100644 --- a/QuestPDF/Drawing/PdfCanvas.cs +++ b/QuestPDF/Drawing/PdfCanvas.cs @@ -1,4 +1,6 @@ -using System.IO; +using System; +using System.IO; +using QuestPDF.Drawing.Exceptions; using QuestPDF.Helpers; using SkiaSharp; @@ -7,11 +9,23 @@ namespace QuestPDF.Drawing internal class PdfCanvas : SkiaDocumentCanvasBase { public PdfCanvas(Stream stream, DocumentMetadata documentMetadata) - : base(SKDocument.CreatePdf(stream, MapMetadata(documentMetadata))) + : base(CreatePdf(stream, documentMetadata)) { } - + + private static SKDocument CreatePdf(Stream stream, DocumentMetadata documentMetadata) + { + try + { + return SKDocument.CreatePdf(stream, MapMetadata(documentMetadata)); + } + catch (TypeInitializationException exception) + { + throw new InitializationException("PDF", exception); + } + } + private static SKDocumentPdfMetadata MapMetadata(DocumentMetadata metadata) { return new SKDocumentPdfMetadata diff --git a/QuestPDF/Drawing/XpsCanvas.cs b/QuestPDF/Drawing/XpsCanvas.cs index 2c3f65e..2d08f48 100644 --- a/QuestPDF/Drawing/XpsCanvas.cs +++ b/QuestPDF/Drawing/XpsCanvas.cs @@ -1,4 +1,6 @@ -using System.IO; +using System; +using System.IO; +using QuestPDF.Drawing.Exceptions; using QuestPDF.Helpers; using SkiaSharp; @@ -7,9 +9,21 @@ namespace QuestPDF.Drawing internal class XpsCanvas : SkiaDocumentCanvasBase { public XpsCanvas(Stream stream, DocumentMetadata documentMetadata) - : base(SKDocument.CreateXps(stream, documentMetadata.RasterDpi)) + : base(CreateXps(stream, documentMetadata)) { } + + private static SKDocument CreateXps(Stream stream, DocumentMetadata documentMetadata) + { + try + { + return SKDocument.CreateXps(stream, documentMetadata.RasterDpi); + } + catch (TypeInitializationException exception) + { + throw new InitializationException("XPS", exception); + } + } } } \ No newline at end of file diff --git a/QuestPDF/Resources/ReleaseNotes.txt b/QuestPDF/Resources/ReleaseNotes.txt index aa0bf2b..0679b9f 100644 --- a/QuestPDF/Resources/ReleaseNotes.txt +++ b/QuestPDF/Resources/ReleaseNotes.txt @@ -1,3 +1,4 @@ Implemented support for the text shaping algorithm that fixes rendering more advanced languages such as Arabic. +Improved exception message when SkiaSharp throws the TypeInitializationException (when additional dependencies are needed). Fixed: a rare case when the Row.AutoItem does not calculate properly the width of its content. Fixed: the QuestPDF Previewer does not work with content-rich documents.