WKHTMLTOPDF不呈现Base64图像 [英] WKHTMLTOPDF Not Rendering Base64 Image

查看:164
本文介绍了WKHTMLTOPDF不呈现Base64图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的HTML页面:

I have the following simple HTML page:

<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:

I am trying to get the PDF to render the Base64 encoded GIF using the following:

  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显示图像不存在(只是一个小方框)时,我在做什么错了?

However when the PDF displays the image doesn't exist (just a little box) what am I doing wrong?

推荐答案

我已升级到最新版本的WKHTMLTOPDF,该版本已解决了该问题:

I upgraded to the latest version of WKHTMLTOPDF which fixed the issue:

http://wkhtmltopdf.org/

这篇关于WKHTMLTOPDF不呈现Base64图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆