rdvojmoc / DinkToPdf

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
MIT License
1.08k stars 415 forks source link

seems like img tag not working #15

Closed cnceniza closed 6 years ago

cnceniza commented 6 years ago

.net core version: 1.0.4 dinktopdf: 1.0.8

I am trying to add an <img> but it seems it does not work. I already have app.UseStaticFiles(); I added the image in my wwwroot folder

I also tried it on 2.0.2 version of dotnet I already have the .dlls in my root folder

A snippet of my code

public IActionResult Pdf()
{
    var converter = new SynchronizedConverter(new PdfTools());
    var doc = new DinkToPdf.HtmlToPdfDocument()
    {
        GlobalSettings =
        {
            ColorMode = DinkToPdf.ColorMode.Color,
            Orientation = DinkToPdf.Orientation.Landscape,
            PaperSize = DinkToPdf.PaperKind.A4Plus,
        },
        Objects =
        {
            new DinkToPdf.ObjectSettings()
            {
                HtmlContent = "<img src='~/long.JPG' asp-append-version='true' />",
                WebSettings = { DefaultEncoding = "utf-8" },
             }
            }
        };

    var pdf = converter.Convert(doc);
    return File(pdf, "application/pdf");
}

How can I add an image to my page then convert it to pdf?

Cheers

rdvojmoc commented 6 years ago

Please check this issue . When I give absolute path to img tag it works as expected: <img src='http://localhost:5000/guide.jpg

If you need relative path please check issues on wkhtmltopdf page.

cnceniza commented 6 years ago

Thank you! It worked! Now, I only need to configure environment variables

Mohemmad-Al-bughdadi commented 2 years ago

you can use HtmlAgilityPack to fix this issue

var htmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(htmlContent);
                foreach (var node in htmlDoc.DocumentNode.SelectNodes("//img"))
                {
                    var src = node.Attributes[@"src"].Value;
                    if (src.StartsWith("/"))
                        node.SetAttributeValue("src", baseUrl + src);
                }

                var newHtml = htmlDoc.DocumentNode.WriteTo();