首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WKHTMLTOPDF未呈现Base64图像

WKHTMLTOPDF未呈现Base64图像
EN

Stack Overflow用户
提问于 2014-07-31 14:56:32
回答 3查看 7.8K关注 0票数 6

我有以下简单的HTML页面:

代码语言:javascript
运行
复制
<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:

代码语言:javascript
运行
复制
  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显示图像不存在时(只是一个小框),我做错了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-08-06 14:20:19

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

http://wkhtmltopdf.org/

票数 2
EN

Stack Overflow用户

发布于 2018-02-01 00:33:17

我有过这个问题。html内容将通过chrome呈现,但通过wkhtmltopdf则不会。

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

它应该是:

代码语言:javascript
运行
复制
data:[<media type>][;charset=<character set>][;base64],<data>

我有过

代码语言:javascript
运行
复制
data: [<media type>][; charset=<character set>][; base64], <data>

Wkhtmltopdf显然比铬更严格。不要在您的base64头中放置空格。

票数 3
EN

Stack Overflow用户

发布于 2019-08-14 20:46:07

对我来说,这是img标签样式的最大宽度和最大高度属性。当它们被移除时,base64图像显示在pdf中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25062224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档