Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Demo/WPF/DemoWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private async void OnGeneratePdf_Click(object sender, RoutedEventArgs e)
config.PageSize = PageSize.A4;
config.SetMargins(20);

var doc = await PdfGenerator.GeneratePdf(_mainControl.GetHtml(), config, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoadPdfSharp);
var doc = await PdfGenerator.GeneratePdfAsync(_mainControl.GetHtml(), config, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoadPdfSharp);

var tmpFile = Path.GetTempFileName();
var pdfFile = Path.ChangeExtension(tmpFile, ".pdf");
Expand Down
2 changes: 1 addition & 1 deletion Source/Demo/WinForms/DemoForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private async void OnGeneratePdf_Click(object sender, EventArgs e)
config.PageSize = PageSize.A4;
config.SetMargins(20);

var doc = await PdfGenerator.GeneratePdf(_mainControl.GetHtml(), config, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoadPdfSharp);
var doc = await PdfGenerator.GeneratePdfAsync(_mainControl.GetHtml(), config, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoadPdfSharp);

var tmpFile = Path.GetTempFileName();
var pdfFile = Path.ChangeExtension(tmpFile, ".pdf"); // Preserves the full path
Expand Down
2 changes: 1 addition & 1 deletion Source/Demo/WinForms/PerfForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void Run(bool layout, bool paint)
{
foreach (var html in _perfTestSamples)
{
htmlContainer.SetHtml(html);
htmlContainer.SetHtmlAsync(html);

if (layout)
htmlContainer.PerformLayout(g);
Expand Down
2 changes: 1 addition & 1 deletion Source/Demo/WinUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private async void OnGeneratePdf_Click(object sender, RoutedEventArgs e)
// Off the UI thread, as the image conversion blocks on Win2D
var pdfFile = await Task.Run(async () =>
{
var doc = await PdfGenerator.GeneratePdf(html, config, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoadPdfSharp);
var doc = await PdfGenerator.GeneratePdfAsync(html, config, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoadPdfSharp);
var file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".pdf");
doc.Save(file);
return file;
Expand Down
18 changes: 9 additions & 9 deletions Source/HtmlRenderer.PdfSharp/Adapters/PdfSharpAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ private PdfSharpAdapter()
}

/// <summary>
/// Also recognizes a family registered via <see cref="AddFontFace"/> - those live entirely in
/// Also recognizes a family registered via <see cref="AddFontFaceAsync"/> - those live entirely in
/// <see cref="FontResolver"/>, invisible to the shared <c>FontsHandler</c> the base implementation
/// checks (see <see cref="AddFontFace"/>'s own doc comment for why). Without this override,
/// checks (see <see cref="AddFontFaceAsync"/>'s own doc comment for why). Without this override,
/// <see cref="Core.Parse.CssParser.ParseFontFamily"/> would never see an <c>@font-face</c>-only
/// family as "existing" and would silently substitute <see cref="Core.Utils.CssConstants.DefaultFont"/>
/// for it before layout ever gets a chance to resolve the real face.
Expand Down Expand Up @@ -186,23 +186,23 @@ protected override RFont CreateFontInt(RFontFamily family, double size, RFontSty
}

/// <summary>
/// Never called: this backend overrides <see cref="AddFontFace"/> directly (see its own doc
/// Never called: this backend overrides <see cref="AddFontFaceAsync"/> directly (see its own doc
/// comment) rather than routing through the base's <c>LoadFontFaceFontInt</c>-based path.
/// </summary>
protected override RFontFamily LoadFontFaceFontInt(byte[] fontBytes, string filePath)
{
throw new NotSupportedException("PdfSharpAdapter overrides AddFontFace directly and never calls LoadFontFaceFontInt.");
throw new NotSupportedException("PdfSharpAdapter overrides AddFontFaceAsync directly and never calls LoadFontFaceFontInt.");
}

/// <summary>
/// Bypasses the base <see cref="RAdapter.AddFontFace"/>/shared <c>FontsHandler</c> registry
/// Bypasses the base <see cref="RAdapter.AddFontFaceAsync"/>/shared <c>FontsHandler</c> registry
/// entirely: PDFsharp's <see cref="IFontResolver"/> needs raw font bytes for PDF embedding, which
/// <see cref="FontResolver.AddFont(Stream,string,int?,bool?,int?,IReadOnlyList{CodepointRange})"/>
/// already stores and matches against directly - no platform font-family handle is involved.
/// </summary>
public override async Task<bool> AddFontFace(string familyName, RUri uri, int weight, bool isItalic, int stretch, IReadOnlyList<CodepointRange> ranges)
public override async Task<bool> AddFontFaceAsync(string familyName, RUri uri, int weight, bool isItalic, int stretch, IReadOnlyList<CodepointRange> ranges)
{
var networkResponse = await GetResourceStream(uri).ConfigureAwait(false);
var networkResponse = await GetResourceStreamAsync(uri).ConfigureAwait(false);
if (networkResponse?.ResourceStream == null)
{
return false;
Expand All @@ -222,14 +222,14 @@ public override async Task<bool> AddFontFace(string familyName, RUri uri, int we
}
}

/// <summary>Bypasses the shared registry, for the same reason as <see cref="AddFontFace"/> - see its doc comment.</summary>
/// <summary>Bypasses the shared registry, for the same reason as <see cref="AddFontFaceAsync"/> - see its doc comment.</summary>
public override bool AddFontFaceFromLocalFamily(string familyName, string localFamilyName, int weight, bool isItalic, int stretch, IReadOnlyList<CodepointRange> ranges)
{
return _fontResolver.AddLocalFontFamily(familyName, localFamilyName, weight, isItalic, stretch, ranges);
}

/// <summary>
/// Bypasses the shared registry, for the same reason as <see cref="AddFontFace"/> - see its doc
/// Bypasses the shared registry, for the same reason as <see cref="AddFontFaceAsync"/> - see its doc
/// comment. PDFsharp's own <see cref="XFont(string,double,XFontStyle,XPdfFontOptions)"/>
/// construction only ever resolves by (family name, bold, italic) internally - there is no public
/// PDFsharp API surface to hand it an already-chosen numeric weight/stretch/face directly - so a
Expand Down
4 changes: 2 additions & 2 deletions Source/HtmlRenderer.PdfSharp/HtmlContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ public string SelectedHtml
/// </summary>
/// <param name="htmlSource">the html to init with, init empty if not given</param>
/// <param name="baseCssData">optional: the stylesheet to init with, init default if not given</param>
public Task SetHtml(string htmlSource, CssData baseCssData = null)
public Task SetHtmlAsync(string htmlSource, CssData baseCssData = null)
{
return _htmlContainerInt.SetHtml(htmlSource, baseCssData);
return _htmlContainerInt.SetHtmlAsync(htmlSource, baseCssData);
}

/// <summary>
Expand Down
24 changes: 10 additions & 14 deletions Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,9 @@ public static void RegisterCustomFontDirectory(string fontDirectory)
/// <param name="stylesheet">the stylesheet source to parse</param>
/// <param name="combineWithDefault">true - combine the parsed CSS data with default CSS data, false - return only the parsed CSS data</param>
/// <returns>the parsed CSS data</returns>
public static Task<CssData> ParseStyleSheet(string stylesheet, bool combineWithDefault = true)
public static CssData ParseStyleSheet(string stylesheet, bool combineWithDefault = true)
{
// CssData.Parse -> CssParser.ParseStyleSheet is not itself async yet (its @import resolution
// bridges into the async resource-loading pipeline synchronously for now - it's also called
// directly from several UI controls' public API, out of scope for this conversion) - wrapped
// in Task.FromResult here so this method's own shape matches the rest of this async-only API.
return Task.FromResult(CssData.Parse(PdfSharpAdapter.Instance, stylesheet, combineWithDefault));
return CssData.Parse(PdfSharpAdapter.Instance, stylesheet, combineWithDefault);
}

/// <summary>
Expand All @@ -99,12 +95,12 @@ public static Task<CssData> ParseStyleSheet(string stylesheet, bool combineWithD
/// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
/// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
/// <returns>the generated image of the HTML</returns>
public static async Task<PdfDocument> GeneratePdf(string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
public static async Task<PdfDocument> GeneratePdfAsync(string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
{
var config = new PdfGenerateConfig();
config.PageSize = pageSize;
config.SetMargins(margin);
return await GeneratePdf(html, config, cssData, stylesheetLoad, imageLoad).ConfigureAwait(false);
return await GeneratePdfAsync(html, config, cssData, stylesheetLoad, imageLoad).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -116,13 +112,13 @@ public static async Task<PdfDocument> GeneratePdf(string html, PageSize pageSize
/// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
/// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
/// <returns>the generated image of the HTML</returns>
public static async Task<PdfDocument> GeneratePdf(string html, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
public static async Task<PdfDocument> GeneratePdfAsync(string html, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
{
// create PDF document to render the HTML into
var document = new PdfDocument();

// add rendered PDF pages to document
await AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad).ConfigureAwait(false);
await AddPdfPagesAsync(document, html, config, cssData, stylesheetLoad, imageLoad).ConfigureAwait(false);

return document;
}
Expand All @@ -138,12 +134,12 @@ public static async Task<PdfDocument> GeneratePdf(string html, PdfGenerateConfig
/// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
/// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
/// <returns>the generated image of the HTML</returns>
public static Task AddPdfPages(PdfDocument document, string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
public static Task AddPdfPagesAsync(PdfDocument document, string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
{
var config = new PdfGenerateConfig();
config.PageSize = pageSize;
config.SetMargins(margin);
return AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad);
return AddPdfPagesAsync(document, html, config, cssData, stylesheetLoad, imageLoad);
}

/// <summary>
Expand All @@ -156,7 +152,7 @@ public static Task AddPdfPages(PdfDocument document, string html, PageSize pageS
/// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
/// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
/// <returns>the generated image of the HTML</returns>
public static async Task AddPdfPages(PdfDocument document, string html, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
public static async Task AddPdfPagesAsync(PdfDocument document, string html, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
{
XSize orgPageSize;
// get the size of each page to layout the HTML in
Expand Down Expand Up @@ -184,7 +180,7 @@ public static async Task AddPdfPages(PdfDocument document, string html, PdfGener

container.Location = new XPoint(config.MarginLeft, config.MarginTop);
container.MaxSize = new XSize(pageSize.Width, 0);
await container.SetHtml(html, cssData).ConfigureAwait(false);
await container.SetHtmlAsync(html, cssData).ConfigureAwait(false);
container.PageSize = pageSize;
container.MarginBottom = config.MarginBottom;
container.MarginLeft = config.MarginLeft;
Expand Down
2 changes: 1 addition & 1 deletion Source/HtmlRenderer.PdfSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Program
{
private static void Main(string[] args)
{
PdfDocument pdf = PdfGenerator.GeneratePdf("<p><h1>Hello World</h1>This is html rendered text</p>", PageSize.A4);
PdfDocument pdf = PdfGenerator.GeneratePdfAsync("<p><h1>Hello World</h1>This is html rendered text</p>", PageSize.A4);
pdf.Save("document.pdf");
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private WpfAdapter()
// DataUriNetworkLoader-only - safer for unattended/server-side PDF generation, matching
// PeachPDF's own default), WPF is an interactive UI backend where fetching a real http(s):
// image or stylesheet out of the box is the expected behavior. data:/file: URIs still resolve
// the same way regardless (RAdapter.GetResourceStream intercepts both before ever consulting
// the same way regardless (RAdapter.GetResourceStreamAsync intercepts both before ever consulting
// NetworkLoader), so only http(s): actually reaches this loader in practice.
NetworkLoader = new HttpClientNetworkLoader(_sharedHttpClient, (Uri)null);

Expand Down
4 changes: 2 additions & 2 deletions Source/HtmlRenderer.WPF/HtmlContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ public void ClearSelection()
/// </summary>
/// <param name="htmlSource">the html to init with, init empty if not given</param>
/// <param name="baseCssData">optional: the stylesheet to init with, init default if not given</param>
public Task SetHtml(string htmlSource, CssData baseCssData = null)
public Task SetHtmlAsync(string htmlSource, CssData baseCssData = null)
{
return _htmlContainerInt.SetHtml(htmlSource, baseCssData);
return _htmlContainerInt.SetHtmlAsync(htmlSource, baseCssData);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions Source/HtmlRenderer.WPF/HtmlControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class HtmlControl : Control

/// <summary>
/// Tracks the in-flight <see cref="SetTextAsync"/> call, if any, so a newer call can supersede an
/// older one still awaiting <see cref="HtmlContainer.SetHtml"/> - the stale call's post-await side
/// older one still awaiting <see cref="HtmlContainer.SetHtmlAsync"/> - the stale call's post-await side
/// effects (invalidate/error reporting) are skipped once it resumes, checked by reference equality
/// against this field.
/// </summary>
Expand Down Expand Up @@ -479,7 +479,7 @@ protected virtual void InvokeMouseMove()
/// </summary>
/// <remarks>
/// A new call before a previous one finishes supersedes it: the previous call's
/// <see cref="HtmlContainer.SetHtml"/> keeps running (it has no mid-flight cancellation point of
/// <see cref="HtmlContainer.SetHtmlAsync"/> keeps running (it has no mid-flight cancellation point of
/// its own) but its post-completion side effects - invalidate, error reporting - are discarded
/// once it resumes, so only the newest call's results ever reach the control.
/// </remarks>
Expand All @@ -493,7 +493,7 @@ public async Task SetTextAsync(string html, CancellationToken cancellationToken

try
{
await _htmlContainer.SetHtml(html, _baseCssData);
await _htmlContainer.SetHtmlAsync(html, _baseCssData);
}
catch (Exception ex)
{
Expand Down
Loading
Loading