Rendering local images using TuesPechkin?

2.9k Views Asked by At

I'm trying to generate documents using TuesPechkin and so far it's working flawlessly when it comes to URL images. However, when it comes to local images it doesn't seem to generate the image at all as shown here:

https://i.stack.imgur.com/UVjjr.jpg

Document Generation:

public static void ConvertDoc(String html)
    {         
        var document = new HtmlToPdfDocument
        {
            GlobalSettings =
            {
                ProduceOutline = true,
                DocumentTitle = "Form documents",                    
                Margins =
                {
                    Top = 1.8,
                    Left = 2.5,
                    Right = 2.5,
                    Bottom = 2.5,
                    Unit = Unit.Centimeters
                }
            },
            Objects =
            {
                new ObjectSettings { HtmlText = html, 
                    WebSettings = new WebSettings() {
                        LoadImages = true
                }
                }

            }
        };

        IConverter converter =  new ThreadSafeConverter(
            new PdfToolset(
                new Win32EmbeddedDeployment(
                    new TempFolderDeployment())));

        byte[] result = converter.Convert(document);

        File.WriteAllBytes("C:/Users/jmann_000/Desktop/Test/program-test-2015.pdf", result);
    }
}
}

As seen above, I have also tried setting load images to true which did not work. Html in question:

    <td>
         <table border="0" cellpadding="0" cellspacing="0" class="doc-header">
                    <tr>
                        <td width="300" align="left"><img src="img/logo.gif" width="240" alt="AeroCare"></td>
                        <td width="375"></td>
                    </tr>
         </table>
    </td>

Is there a correct way to generate the image if its local?

1

There are 1 best solutions below

0
On BEST ANSWER

wkhtmltopdf is not running in the same folder as your root project , so any path relative to your project will not be correct for wkhtmltopdf.

This Creates problems with relative images and also css stylesheets. In any case you need to provide the fully qualified path to the file.

Solution 1

For MVC and Razor

is to provide the full Path to your Hosted Application in order to acquire the file form your host.

@String root = http://localhost:port/
<img src="@String.Format("{0}img/logo.gif",root)" width="240" alt="AeroCare">

Solution 2

Acquire the file using the filesystem path.

@String root = @HostingEnvironment.MapPath("~");

In case you are running a console application replace with

String root = AppDomain.CurrentDomain.BaseDirectory;

But then you need an engine to replace all links within the HTML file with the fully qualified paths.

Solution 3

Find out from which directory wkhtmltopdf is running and try to provide links relative to that directory.