WKHTMLTOPDF不渲染Base64图像

本文关键字:Base64 图像 WKHTMLTOPDF | 更新日期: 2023-09-27 18:16:00

我有以下简单的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显示图像不存在(只是一个小框)我做错了什么?

WKHTMLTOPDF不渲染Base64图像

我有这个问题。html内容可以通过chrome渲染,但是通过wkhtmltopdf就不行。

这是因为base64字符串的头项之间有空格。

应该是:

data:[<media type>][;charset=<character set>][;base64],<data>

And i had

data: [<media type>][; charset=<character set>][; base64], <data>

Wkhtmltopdf显然比chrome更严格。不要在base64页眉中使用空格

我升级到最新版本的WKHTMLTOPDF,修复了这个问题:

http://wkhtmltopdf.org/

对于我来说,它是img标签样式的max-width和max-height属性。当它们被删除后,base64图像显示在pdf