+ For printing an HTML element by ID there are multiple options.
+ The default option is to scan the current effective styles will be and what is printed will closely match what the user currently sees. However, before and after pseudo-elements aren't included.
+ Alternatively scanning current styles can be disabled and URLs for CSS file(s) can be provided, this enables applying different styles when printing.
+
+
+
+
+
This section between the horizontal lines will be printed.
+
Normally not displayed, only printed when Bootstrap is included.
+
This is normally displayed but not printed when Bootstrap is included.
+
This element has an ::after pseudo-element.
+
Element with background colour, with default print-color-adjust setting.
+
Element with background colour, with print-color-adjust set to exact.
+
+ Element with different styles set in print media query in scoped CSS, ScanStyles need to be disabled for different style to take effect.
+ Also, this element demonstrates that having ScanStyles enabled can cause issues when an element requires more vertical space when printing.
+ Dependent on the browser window width this text might be wrapped over more lines when being printed, overlapping the element below when ScanStyles is used.
+
+
Some text which might be overlapped by the above text when ScanStyles is used.
+
+
+
+
+ Print section default options
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Print section with selected options.
+
+
+
+@code
+{
+ private Task PrintDefault() => PrintingService.Print("print-me", PrintType.Html);
+
+ private class DemoModel
+ {
+ public bool IncludeBootstrapCss { get; set; } = true;
+ public bool IncludeApplicationCss { get; set; } = true;
+ public bool ScanStyles { get; set; }
+ }
+
+ private readonly DemoModel _model = new();
+
+ private Task Print()
+ {
+ var options = new PrintOptions { Printable = "print-me", Type = PrintType.Html, ScanStyles = _model.ScanStyles };
+ if (_model.IncludeBootstrapCss) options.CssUrls.Add("css/bootstrap/bootstrap.min.css");
+ if (_model.IncludeApplicationCss) options.CssUrls.AddRange(new[] { "css/site.css", "Append.Blazor.Printing.Server.styles.css" });
+
+ return PrintingService.Print(options);
+ }
+}
diff --git a/samples/Append.Blazor.Printing.Server/Pages/Index.razor.css b/samples/Append.Blazor.Printing.Server/Pages/Index.razor.css
new file mode 100644
index 0000000..fb11f41
--- /dev/null
+++ b/samples/Append.Blazor.Printing.Server/Pages/Index.razor.css
@@ -0,0 +1,26 @@
+.demo1::after {
+ content: "Content of after pseudo-element."
+}
+
+.demo2 {
+ color: white;
+ background-color: orange;
+}
+
+.demo3 {
+ font-weight: bold;
+ text-decoration: underline;
+}
+
+.print-colors {
+ -webkit-print-color-adjust: exact;
+ print-color-adjust: exact;
+}
+
+@media print {
+ .demo3 {
+ font-size: 24px;
+ font-weight: normal;
+ text-decoration: none;
+ }
+}
diff --git a/source/Append.Blazor.Printing/PrintOptions.cs b/source/Append.Blazor.Printing/PrintOptions.cs
index d8ab0b8..a7a34b6 100644
--- a/source/Append.Blazor.Printing/PrintOptions.cs
+++ b/source/Append.Blazor.Printing/PrintOptions.cs
@@ -1,4 +1,6 @@
-namespace Append.Blazor.Printing
+using System.Collections.Generic;
+
+namespace Append.Blazor.Printing
{
///
/// Options for a specific print.
@@ -41,5 +43,13 @@ public PrintOptions(string printable, string modalMessage, PrintType printType =
/// Used when printing PDF documents passed as base64 data.
///
public bool Base64 { get; set; }
+ ///
+ /// This allows us to pass one or more css files URLs that should be applied to the html being printed.
+ ///
+ public List CssUrls { get; } = new();
+ ///
+ /// When set to false, the library will not process styles applied to the html being printed. Useful when using .
+ ///
+ public bool ScanStyles { get; set; } = true;
}
}
diff --git a/source/Append.Blazor.Printing/PrintOptionsAdapter.cs b/source/Append.Blazor.Printing/PrintOptionsAdapter.cs
index ea31d79..3f1b02b 100644
--- a/source/Append.Blazor.Printing/PrintOptionsAdapter.cs
+++ b/source/Append.Blazor.Printing/PrintOptionsAdapter.cs
@@ -12,6 +12,8 @@ internal record PrintOptionsAdapter
public bool ShowModal { get; init; }
public string ModalMessage { get; init; } = "Retrieving Document...";
public bool? Base64 { get; set; }
+ public string[] Css { get; set; } = null;
+ public bool ScanStyles { get; set; } = true;
public string TargetStyles { get; set; } = "['*']";
public PrintOptionsAdapter(PrintOptions options)
@@ -21,6 +23,9 @@ public PrintOptionsAdapter(PrintOptions options)
ShowModal = options.ShowModal;
ModalMessage = options.ModalMessage;
Base64 = options.Base64 == true ? true : null;
+ if (options.CssUrls.Count > 0)
+ Css = options.CssUrls.ToArray();
+ ScanStyles = options.ScanStyles;
}
}
}