Repository navigation
Questions about PDF printing and PDF generation in Avalonia #22480
|
I have almost finished porting my WPF application to Avalonia, and I am considering purchasing an Avalonia Pro subscription. The main features I am interested in are PDF preview and printing. I am aware that Avalonia provides a built-in PDF preview component, but I would like to clarify a couple of things before making a decision.
Does Avalonia Pro, or Avalonia more generally, provide a cross-platform printing API that supports the following?
This functionality is particularly important for my application, as I need to print documents directly from the application without interrupting the user's workflow.
Is there a supported way to generate PDFs directly from Avalonia UI controls or AXAML, similar to how WPF can use XPS to represent and print visual content? If this functionality is not currently available, are there any plans to support it? At the moment, the only approach I can think of is rendering Avalonia UI content to images and then combining those images into a PDF. However, I would prefer a solution that preserves text and vector graphics rather than rasterizing the entire document, particularly for documents that need to be printed. Any clarification regarding the current capabilities of Avalonia Pro, existing alternatives, or planned features would be greatly appreciated. I would like to understand whether these requirements can be met before deciding on a subscription. |
Replies: 2 comments 2 replies
|
I can't speak for what Pro includes. Ask Avalonia sales directly before you buy. I can tell you where open-source Avalonia stands, though, because it shapes your options either way. Printing: Avalonia has no printing API, and nothing to list printers or print silently. Most people call the OS directly:
Hide both behind a small PDF from AXAML, vector, no rasterizing: this already works. Skia can write PDFs, and Avalonia can draw any visual onto a Skia canvas: using var stream = File.Create("out.pdf");
using var doc = SKDocument.CreatePdf(stream);
var size = new Size(595, 842); // A4 in points
control.Measure(size);
control.Arrange(new Rect(size));
using (var canvas = doc.BeginPage((float)size.Width, (float)size.Height))
{
await DrawingContextHelper.RenderAsync(canvas, control);
}
doc.EndPage();
doc.Close();
Generate the PDF this way, then print it with the OS path above. That covers the WPF/XPS workflow without depending on Pro. |
|
NativeWebView (Avalonia.Controls.WebView) includes APIs to print current page. It's not primarily designed for PDF, but can work with it. It also has pretty limited customization. |
I can't speak for what Pro includes. Ask Avalonia sales directly before you buy. I can tell you where open-source Avalonia stands, though, because it shapes your options either way.
Printing: Avalonia has no printing API, and nothing to list printers or print silently. Most people call the OS directly:
lpstat -elists printers andlp -d <printer> file.pdfprints with no dialog. Run them withProcess.Start, or P/Invokelibcupsif you want it cleaner.PrinterSettings.InstalledPrintersgives you the list. For silent PDF printing, render pages with PDFium (for example PDFtoImage or a PDFium wrapper) and send them throughPrintDocument, or sh…