Since wkhtmltopdf
is just a browser under the hood, you can use any HTML entities to properly encode special characters:
<td>0Ω</td>
Question
I'm trying to convert the following HTML to PDF. How can I get wkhtmltopdf to encode the Ω symbol?
<tr>
<td>1</td>
<td>Front door1</td>
<td>Fire Exit 2</td>
<td>-</td>
<td>Non EOL</td>
<td>-</td>
<td>0Ω</td>
</tr>
but the rendered PDF is appearing with the 0Ω as:
This is how I'm converting the HTML to PDF:
using (var htmlStream = GenerateStreamFromString(renderedText))
{
try
{
using (var pdfStream = new FileStream(_fileName, FileMode.OpenOrCreate))
{
Printer.GeneratePdf(htmlStream, pdfStream);
}
}
}
public static Stream GenerateStreamFromString(string s)
{
UTF8Encoding utf8 = new UTF8Encoding();
string unicodeString = s;
byte[] encodedBytes = utf8.GetBytes(unicodeString);
var encodedHtmlString = utf8.GetString(encodedBytes);
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(encodedHtmlString);
writer.Flush();
stream.Position = 0;
}
Where GeneratePDF is defined as:
public static void GeneratePdf(Stream html, Stream pdf)
{
Process process;
StreamWriter stdin;
var psi = new ProcessStartInfo();
psi.FileName = "path/to/wkhtmltopdf.exe"
psi.WorkingDirectory = Path.GetDirectoryName(psi.FileName);
// run the conversion utility
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.Arguments = "-q -n --disable-smart-shrinking - -";
process = Process.Start(psi);
try
{
stdin = process.StandardInput;
stdin.AutoFlush = true;
stdin.Write(new StreamReader(html).ReadToEnd());
stdin.Dispose();
process.StandardOutput.BaseStream.CopyTo(pdf);
process.StandardOutput.Close();
pdf.Position = 0;
process.WaitForExit(10000);
}
catch (Exception ex)
{
throw ex;
}
finally
{
process.Dispose();
}
}
Solution
Since wkhtmltopdf
is just a browser under the hood, you can use any HTML entities to properly encode special characters:
<td>0Ω</td>