c# - WKHTMLTOPDF 不呈现 Base64 图像

标签 c# pdf base64 wkhtmltopdf

我有以下简单的 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 header 中放置空格。

关于c# - WKHTMLTOPDF 不呈现 Base64 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25062224/

相关文章:

mysql - 使用 Base64 Spring Boot 将图像上传到数据库

.net - GZipStream:为什么压缩后要转换为 Base 64?

c# - 如何使用CSS在电子邮件正文中发送网页

c# - 将 MFC (VC6) 应用程序迁移到 .NET 2008

http - 应用程序/pdf 内容类型 http 响应 header 的正确字符集是什么?

java - Java 和 PHP 之间的 Base64_encode 不同

c# - 在 RowDeleteing 事件中显示 ASPX Popup 控件 (ASPX Gridview)

C# 用户控件自定义属性

c# - 从控制台应用程序请求 MVC 页面,并传入包含 FormsAuthentication cookie 的上下文

javascript - 无法使用 react 和 href 元素在本地主机中下载 pdf 文件