Image is not showing in PDF file using DinkToPDF in .NET Core 6

271 Views Asked by At

I have a .NET Core 6 project and DinkToPDF library installed, my project is working fine, but the issue is that when i export he the file in PDF, Id, Name, Contact is showing but image is not showing in PDF, below is my code.

`[HttpPost] public IActionResult CreatePDF() {

var globalSettings = new GlobalSettings
{
    ColorMode = ColorMode.Color,
    Orientation = Orientation.Portrait,
    PaperSize = PaperKind.A4,
    Margins = new MarginSettings { Top = 10 },
    DocumentTitle = "PDF Report",
    Out = @"D:\PDFCreator\Employee_Report.pdf"


};
var objectSettings = new ObjectSettings
{
    PagesCount = true,

    HtmlContent = GetHTMLString(),
    WebSettings = { DefaultEncoding = "utf-8", LoadImages = true, UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },
    HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },
    FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "MyData Company" }
};
var pdf = new HtmlToPdfDocument()
{
    GlobalSettings = globalSettings,[[enter image description here](https://i.stack.imgur.com/n66Rk.png)](https://i.stack.imgur.com/ZtOgW.png)
    Objects = { objectSettings }
};
_converter.Convert(pdf);
return Ok("Successfully created PDF document.");

}`

    public string GetHTMLString()
    {
        //var mc = new TemplateGenerator();
        var student = _context.Students.ToList();
        var sb = new StringBuilder();
        sb.Append(@"
                    <html>
                        <head>
                        </head>
                        <body>
                            <div class='header'><h1>MyData PDF Report!!!</h1></div>
                            <table align='center'>
                                <tr>
                                    <th>Id</th>
                                    <th>Name</th>
                                    <th>Contact</th>
                                    <th>Image</th>
                                </tr>");
        foreach (var stu in student)
        {
            //var imageString = string.Format("data:image/png;base64,{3}", Convert.ToBase64String(stu.Image));
            sb.AppendFormat(@"<tr>
                                <td>{0}</td>
                                <td>{1}</td>
                                <td>{2}</td>
                                <td><img height='100px' width='100px' src={3} /> </td>

                              </tr>", stu.Id, stu.Name, stu.Contact, stu.Image);
        }
        sb.Append(@"
                            </table>
                        </body>
                    </html>");
        return sb.ToString();
    }
0

There are 0 best solutions below