Image display in HTML but not in PDF using WKHTMLTOPDF
Asked Answered
M

5

5

Hi am using Wkhtmltopdf to create PDF from Html page using c#, Asp.net. PDF are created smoothly, but when I add images to HTML it does not get displayed on PDF.

     public static string HtmlToPdf(string pdfOutputLocation, string outputFilenamePrefix, string[] urls,
        string[] options = null,
        // string pdfHtmlToPdfExePath = "C:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf.exe")

       string pdfHtmlToPdfExePath = "C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe")
    {
        string urlsSeparatedBySpaces = string.Empty;
        try
        {
            //Determine inputs
            if ((urls == null) || (urls.Length == 0))
                throw new Exception("No input URLs provided for HtmlToPdf");
            else
                urlsSeparatedBySpaces = String.Join(" ", urls); //Concatenate URLs

            string outputFolder = pdfOutputLocation;
            string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name

            var p = new System.Diagnostics.Process()
            {
                StartInfo =
                {
                    FileName = pdfHtmlToPdfExePath,
                    Arguments = ((options == null) ? "" : String.Join(" ", options)) + " " + urlsSeparatedBySpaces + " " + outputFilename,
                    UseShellExecute = false, // needs to be false in order to redirect output
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
                    WorkingDirectory = HttpContext.Current.Server.MapPath(outputFolder),
                    CreateNoWindow = true
                }
            };

            p.Start();

            // read the output here...
            var output = p.StandardOutput.ReadToEnd();
            var errorOutput = p.StandardError.ReadToEnd();

            // ...then wait n milliseconds for exit (as after exit, it can't read the output)
            p.WaitForExit(60000);

            // read the exit code, close process
            int returnCode = p.ExitCode;
            p.Close();

            // if 0 or 2, it worked so return path of pdf
            if ((returnCode == 0) || (returnCode == 2))
                return outputFolder + outputFilename;
            else
                throw new Exception(errorOutput);
        }
        catch (Exception exc)
        {
            throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, exc);
        }
    }

This is my HTML code area to display Image..

<div id="Image" class="right" style="background:#333;height:15%;width:25%;">

  <img id="LogoImage" runat="server" width="100%" height="100%" src="../TempLogo/chafa.jpg" />

Macfadyn answered 23/4, 2013 at 13:22 Comment(1)
The resulting HTML file is display as I need. But Image not displayed in PDF. Please Help.Macfadyn
D
4

I had this problem, and for me the "fix" was to remove the height attribute.

Instead of

<img id="Logo" runat="server" width="100%" height="100%" src="../TempLogo/chafa.jpg" />

Try

<img id="Logo" runat="server" width="100%" src="../TempLogo/chafa.jpg" />

I don't have an explanation for this behaviour, it could be a bug.

Dunk answered 19/11, 2017 at 13:7 Comment(3)
This also fixed it for me. Updating to 0.12.5 seems to have fixed the bug and allowed me to use both width and height without issuesBreathless
This 'solved' the issue for me. I'm running 0.12.5 but still can't use both height and widthSymphonious
Solves for me, even using an updated version.Stewartstewed
G
2

Try adding a full path for the image src. This is often due to "../TempLogo/chafa.jpg" not actually being the correct relative URL for the wkhtmltopdf working directory.

So, instead of "../TempLogo/chafa.jpg" try "C:/yourpath/TempLogo/chafa.jpg" or "file:///C:/yourpath/TempLogo/chafa.jpg" and see if that helps. If it does, your relative path is the problem.

Gaius answered 23/4, 2013 at 19:50 Comment(6)
I will try it & let you know.Macfadyn
I have tried it but it does not help. Please help me to solve this issue.Macfadyn
I have used "file:///E:/AMS/AMS/AMS.WEB/TempLogo/chafa.jpg" & "src="localhost:5822/AMS.WEB/TempLogo/chafa.jpg" also but it does not work. I also tried to change Version from 0.11 to 0.9 but it also not succeedMacfadyn
Dang it. I'll try to think of something else.Gaius
I really don't have any idea yet on what would cause this. Gif images fail all the time, but I've never seen a jpg fail except for the reason I gave... I'm lost at the moment, sorry!!Gaius
file:///E:/AMS/AMS/AMS.WEB/TempLogo/chafa.jpg worked for me. Use absolutUri, HTML renders only absolutUri.Dacron
I
1

Using pdfkit with ruby, I have found that if I send an html file with images on STDIN to wkhtmltopdf the images don't render. (ie wkhtmltopdf - toto.pdf : no images)

If I execute the EXACT same command by hand passing an html file (wkhtmltopdf toto.html toto.html), this works.

No idea why, but, I just wrote a small wrapper to call it that way and that's it.

Ineligible answered 4/8, 2013 at 5:35 Comment(2)
Hey, can you show toto.html? I really want to duplicate this because I've never had a problem with it other than due to path issues.Gaius
This I think might too be a path issue now that I rethink it - did your HTML have relative paths? Because the working directory might be read from the input file during non-stdin conversions.Gaius
P
1

I use Views to store the HTML markup for each pdf template that I want, I pass in the model to the view. Using Server.MapPath() will get you the full path to your image.

<img src="@Server.MapPath("~/path/to/your/image.jpg")" />
Phytohormone answered 27/7, 2017 at 4:48 Comment(0)
G
0

I have recently seen a jpeg file fail conversion - the problem was that the image was a grayscale image.

Note that all gray images are not necessarily grayscale images - check with a program like irfanview if this is the case.

Gaius answered 8/12, 2014 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.