我有以下简单的 HTML 页面:
<html>
<head>
<title></title>
</head>
<body>
<div>
<img id="image" src="data:image/gif;base64,R0lGODlhFwAPAKEAAP///wAAAMzMzLi3tywAAAAAFwAPAAACQIyPqQjtD98RIVpJ66g3hgEYDdVhjThyXSA4aLq2rgp78hxlyY0/ICAIBhu/HrEEKIZUyk4R1Sz9RFEkaIHNFgAAOw==" />
</div>
</body>
</html>
我正在尝试使用以下命令让 PDF 呈现 Base64 编码的 GIF:
public static byte[] HTMLToPDF(string htmlText)
{
Process p;
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = HttpContext.Current.Server.MapPath("~/App_Data/wkhtmltopdf/wkhtmltopdf.exe");
// run the conversion utility
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
// note: that we tell wkhtmltopdf to be quiet and not run scripts
string args = "-q -n ";
args += "--disable-smart-shrinking ";
//args += "--orientation Landscape ";
args += "--outline-depth 0 ";
args += "--page-size A4 ";
args += " - -";
psi.Arguments = args;
p = Process.Start(psi);
try
{
using (StreamWriter stdin = p.StandardInput)
{
stdin.AutoFlush = true;
stdin.Write(htmlText);
}
//read output
byte[] buffer = new byte[32768];
byte[] file;
using (var ms = new MemoryStream())
{
while (true)
{
int read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
break;
ms.Write(buffer, 0, read);
}
file = ms.ToArray();
}
p.StandardOutput.Close();
// wait or exit
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
if (returnCode == 0)
return file;
}
catch (Exception ex)
{
}
finally
{
p.Close();
p.Dispose();
}
return null;
}
然而,当 PDF 显示图像不存在(只是一个小盒子)时,我做错了什么?
我有这个问题。 html 内容可以通过 chrome 渲染得很好,但通过 wkhtmltopdf 则不行。
这是因为我在 base64 字符串的标题项之间有空格。
它应该是:
data:[<media type>][;charset=<character set>][;base64],<data>
我有
data: [<media type>][; charset=<character set>][; base64], <data>
Wkhtmltopdf 对此显然比 chrome 更严格。不要在 Base64 标头中添加空格。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)