我正在尝试将以下HTML转换为PDF。如何让wkhtmltopdf对Ω符号进行编码?
<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>
但渲染的PDF出现时的0Ω为:
这就是我将HTML转换为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;
}
其中GeneratePDF定义为:
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();
}
}