I'm playing with iText7 as a proof of concept project.
I need to prove that iText7 can utilise CSS3 and HTML5 as they suggest on their site.
However, Im finding when i generate the pdf, the css3 properties are lost.
Below is the .Net code that generates the pdf file.
public class HtmlToPdfService : IHtmlToPdfService
{
public async Task CreatePdfFromHtmlFileStreamAsync(IFormFile file)
{
try
{
if (file == null)
{
throw new ArgumentException("file cannot be null or empty.");
}
var filename = $"{Path.GetFileNameWithoutExtension(file.FileName)}.pdf";
var stream = new MemoryStream();
var html = string.Empty;
using (var reader = new StreamReader(file.OpenReadStream()))
{
var converterProperties = new ConverterProperties();
html = reader.ReadToEnd();
using (var pdfWriter = new PdfWriter(stream))
{
pdfWriter.SetCloseStream(false);
HtmlConverter.ConvertToPdf(html, pdfWriter, converterProperties);
}
}
stream.Position = 0;
using (var fileStream = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot/pdf", filename), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
await stream.CopyToAsync(fileStream);
}
}
catch (Exception x)
{
throw x;
}
}
}
}
Here is the HTML I have tried convert to PDF:-
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
p {
width: 200px;
border: 1px solid;
padding: 2px 5px;
/* BOTH of the following are required for text-overflow */
white-space: nowrap;
overflow: hidden;
}
.overflow-visible {
white-space: initial;
}
.overflow-clip {
text-overflow: clip;
}
.overflow-ellipsis {
text-overflow: ellipsis;
}
.overflow-string {
/* Not supported in most browsers,
see the 'Browser compatibility' section below */
text-overflow: " [..]";
}
</style>
</head>
<body>
<p class="overflow-visible">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p class="overflow-clip">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p class="overflow-ellipsis">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p class="overflow-string">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</body>
</html>
font-weight: boldor is it just specific properties? If its just some, I would contact their support and ask what portion of the CSS3 spec is allowed (no browsers implement the full spec either)