I am using Aspose.PDF.Drawing for .NET. See the code below:
using Aspose.Pdf;
const string LicenseFile = "Aspose.Pdf.lic";
SetAsposeLicense(LicenseFile);
byte[] pdfBytes = GeneratePdf();
File.WriteAllBytes("GeneratedReport.pdf", pdfBytes);
void SetAsposeLicense(string licensePath)
{
License license = new();
license.SetLicense(licensePath);
}
byte[] GeneratePdf()
{
string html = "<style> @import url('https://fonts.googleapis.com/css2?family=Work+Sans:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap'); #body { font-family: 'Work Sans', sans-serif; font-size: 1rem; margin: 0.5%; font-size: 11px; color: #231F20; } tr.datatable-row { vertical-align: inherit; } h1 { font-size: 17px; } h2 { font-size: 15px; } h3 { font-size: 13px; } .no-wrap { white-space: nowrap; } .info { margin-top: 0px; margin-bottom: 2px; display: block; } table { display: flex; border-collapse: collapse; font-size: 9px; } td { text-align: left; font-weight: normal; padding: 3px; border: none; } th { padding: 10px; padding-right: 5px; text-align: left; } tr.datatable-row:nth-child(even) { background: #fff; } tr.datatable-row:nth-child(odd) { background: #fff; } .datatable-header { display: table-header-group; font-size: 9px; background-color: #293F96; color: #fff; } .top-left { border-top-left-radius: 5px; } .top-right { padding-right: 10px; border-top-right-radius: 5px; } .datatable-row { vertical-align: top; } .datatable-cell { padding: 0.4rem; } .datatable-summary { font-size: 9px; background: #F4F5FF; color: #231F20; } .datatable-summary td { font-weight: bold; } .datatable-row:hover { background-color: #e4f0ff; color: inherit !important; } .hour-field-1 { background-color: #fff; background-clip: padding-box; border: 1px dashed #483d3f; } .hour-field-2 { background-color: #f4f5ff !important; } .hour-field-3 { } .hour-field-4 { color: #C73800; background-color: #f3ebe0 !important; } .hour-field { border-radius: 0.25rem; padding: 7px !important; width: 34px; } .align-right { text-align: right; } .avoid-break-after { page-break-after: avoid; } .datatable-row td:first-child { border-left: 1px solid #e1e8ee; } .datatable-row td:last-child { border-right: 1px solid #e1e8ee; } .datatable-row td { border-bottom: 1px solid #e1e8ee; } td { border: 1px solid #e1e8ee; } tr:first-child td:first-child { border-top-left-radius: 5px; } tr:first-child td:last-child { border-top-right-radius: 5px; } tr:last-child td:first-child { border-bottom-left-radius: 5px; border-left: 1px solid #e1e8ee; } tr:last-child td:last-child { border-bottom-right-radius: 5px; border-right: 1px solid #e1e8ee; } tr:last-child td { border-bottom: 1px solid #e1e8ee; } table { border-collapse: separate; border-spacing: 0; } td { border: 0px; } </style> <div id=\"body\"> <h1>Test</h1> </div>";
Document document = new();
Page page = document.Pages.Add();
page.PageInfo.IsLandscape = true;
page.PageInfo.Margin = new MarginInfo(25, 40, 25, 90);
HtmlFragment htmlFragment = new(html)
{
HtmlLoadOptions = new HtmlLoadOptions
{
IsEmbedFonts = true,
HtmlMediaType = HtmlMediaType.Print,
}
};
page.Paragraphs.Add(htmlFragment);
using MemoryStream pdfOutputStream = new();
document.Save(pdfOutputStream);
return pdfOutputStream.ToArray();
}
The given code works with Aspose.PDF.Drawing version 23.4.0 but breaks when upgrading to any higher version (up to the latest 24.5.0)
It look like the import of the font in the style element doesn’t work properly.