IHtmlToPdfService pdf = new Regira.Office.PDF.MsPlaywright.PdfManager();
IMemoryFile result = await pdf.Create(new HtmlInput
{
HtmlContent = reportHtml,
HeaderHtmlContent = "<div style='text-align:right;font-size:10px'>Confidential</div>",
HeaderHeight = 15,
// Chromium fills the pageNumber and totalPages classes
FooterHtmlContent = "<div style='text-align:center;font-size:10px'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></div>",
FooterHeight = 15,
Format = PageSize.A4,
Orientation = PageOrientation.Portrait,
Margins = new float[] { 20, 20, 20, 20 } // top, right, bottom, left, in units of DPI (96 per inch)
});
await fileService.Save("reports/output.pdf", result.GetBytes()!);
Combine IHtmlParser (Razor) with IHtmlToPdfService for a full template-to-PDF pipeline.
public class InvoicePdfService(IHtmlParser html, IHtmlToPdfService pdf)
{
public async Task<IMemoryFile> Generate(InvoiceDto invoice)
{
string template = await File.ReadAllTextAsync("Templates/Invoice.cshtml");
string rendered = await html.Parse(template, invoice);
return await pdf.Create(new HtmlInput
{
HtmlContent = rendered,
Format = PageSize.A4
});
}
}
IPdfMerger merger = new Regira.Office.PDF.PdfPig.PdfService(imageService);
var pages = new List<IMemoryFile>
{
coverBytes.ToBinaryFile(),
chapterOneBytes.ToBinaryFile(),
appendixBytes.ToBinaryFile()
};
IMemoryFile merged = (await merger.Merge(pages))!;
IPdfSplitter splitter = new Regira.Office.PDF.PdfPig.PdfService(imageService);
int total = await splitter.GetPageCount(pdf);
var ranges = Enumerable.Range(1, total)
.Select(i => new PdfSplitRange { Start = i, End = i });
IEnumerable<IMemoryFile> pages = await splitter.Split(pdf, ranges);
IPdfToImageService converter = new Regira.Office.PDF.PdfPig.PdfService(imageService);
var images = await converter.ToImages(pdf, new PdfToImagesOptions
{
Size = new ImageSize(1200, 1697), // approx A4 at 144 dpi
Format = ImageFormat.Jpeg
});
int page = 1;
foreach (var img in images)
await fileService.Save($"pages/page-{page++}.jpg", img.GetBytes()!);
IPdfTextService extractor = new Regira.Office.PDF.PdfPig.PdfService(imageService);
string fullText = await extractor.GetText(pdf);
IList<string> byPage = await extractor.GetTextPerPage(pdf);
// Remove pages that contain no text (blank separator pages, and scanned pages too)
IMemoryFile cleaned = (await extractor.RemoveEmptyPages(pdf))!;
IPdfPrinter printer = new Regira.Office.PDF.Spire.PdfPrinter();
Console.WriteLine("Available printers:");
foreach (var p in await printer.List())
Console.WriteLine($" {p}");
await printer.Print(new PdfPrinterInput
{
PdfFile = pdfFile,
PrinterName = "HP LaserJet Pro",
PageSize = PageSize.A4,
PageOrientation = PageOrientation.Portrait
});
IDocumentToPdfService converts Word documents, spreadsheets and presentations. Register MiniPdf’s PdfService,
which has no dependencies, and inject the abstraction.
services.AddSingleton<IDocumentToPdfService, Regira.Office.PDF.MiniPdf.PdfService>();
public class ReportPdfService(IDocumentToPdfService converter)
{
public Task<IMemoryFile> ToPdf(byte[] workbook)
=> converter.Create(new DocumentInput
{
Document = workbook.ToMemoryFile(),
Orientation = PageOrientation.Landscape // an .xlsx takes Orientation; Format or Margins throw
});
}