WKHTMLTOPDF Not Rendering Base64 Image
Asked Answered
Z

3

6

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>

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;
        }

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

Zoochore answered 31/7, 2014 at 14:56 Comment(3)
I ran the latest x64 version of wkhtmltopdf with your sample HTML standalone with no problem. Can I start by asking that you've been able to run this with no arguments and produced the desired result?Convert
looks like i was running an older version. just pulled down the latest and gif is now working! Thank you!Zoochore
Not really an answer, but glad it works out for you :) I love this utility. I use it to generate client bills from HTML and convert them to PDF.Convert
Z
2

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

http://wkhtmltopdf.org/

Zoochore answered 6/8, 2014 at 14:20 Comment(0)
E
4

I had this issue. The html content would render via chrome just fine, but through wkhtmltopdf it would not.

This was because i had spaces in between the header items in the base64 string.

It should be:

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

And i had

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

Wkhtmltopdf is evidently stricter about this than chrome. Don't put spaces in your base64 header.

Euclid answered 1/2, 2018 at 0:33 Comment(0)
Z
2

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

http://wkhtmltopdf.org/

Zoochore answered 6/8, 2014 at 14:20 Comment(0)
Y
1

For me it was max-width and max-height properties of the style for the img tag. When they were removed, base64 image got displayed in the pdf

Yod answered 14/8, 2019 at 20:46 Comment(1)
using width or height with a percentage also does not work. Had to explicitly state the width and height in pixels.Allene

© 2022 - 2024 — McMap. All rights reserved.