I have problem with converting HTML to PDF with c# coding. Namely, I go through next steps:
1) creating HtmlFormatProvider
2) registering event LoadStyleSheetFromUri
3) importing Stream with HTML content. Content contains reference to bootstrap.css, Bootstrap v4.3.1 (https://getbootstrap.com/)
4) creating PdfFormatProvider
5) exporting to pdf.
Complete code is following:
public async static Task<InputSignDocument> CreateDemoDocument(string wwwPath, string fileName, string extension, string recipientName, string recipientEmail, Func<Task<Stream>> getPdfStream)
{
var result = new InputSignDocument()
{
FileName = fileName,
FileExtension = extension,
};
Stream inpuContent = await getPdfStream(); // here is reading html into stream
var htmlProvider = new HtmlFormatProvider();
HtmlImportSettings importSettings = new HtmlImportSettings();
importSettings.LoadStyleSheetFromUri += (s, e) =>
{
string styles = File.ReadAllText($"{wwwPath}\\{e.Uri}");
e.SetStyleSheetContent(styles);
};
importSettings.DefaultStyleSheet = "body {white-space: normal;}";
htmlProvider.ImportSettings = importSettings;
RadFlowDocument doc;
using (StreamReader reader = new StreamReader(inpuContent))
{
string htmlText = reader.ReadToEnd();
doc = htmlProvider.Import(htmlText);
}
var pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
PdfExportSettings exportSettings = new PdfExportSettings()
{
};
pdfProvider.ExportSettings = exportSettings;
result.FileContent = new MemoryStream();
pdfProvider.Export(doc, result.FileContent);
return result;
}
PDF is created however no bootstrap classes are apllied.
In debug session I look in variable (string styles) in event, css file was read properly.
HTML contains two references:
<link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="/css/site.css" />
From event I see that both files are exported.
In HTML, namely, in razor view are used bootstrap classes: container, col-lg-6, col-md-6, col-sm-6, row.
rhey are applied this way:
<div class="container">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-4">
<label>Werknemer</label>
<div>
/fnm1/
</div>
</div>
<label></label>
<div class="col-lg-4 col-md-4 col-sm-4">
<label>Handtekening</label>
<div>
/sn1/
<hr />
</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 docusign-basetab">
<label>Aangetekend op</label>
<div>
/ds1/
</div>
</div>
</div>
</div>
</div>
</div>
It seems that this part css is not applied in PDF File.
What I am missing?
When I run the view from controller it shows me this:
And in pdf it looks like this: