DinkToPdf,包括HtmlContent中的图像

问题描述

我有一个使用.Net Core 3 Web APIDinkToPdf库创建的pdf发票。

现在我需要将pdf图像作为签名插入。我创建了项目的Resource文件夹,并添加copy to Output Directory设置为allways的图像。但是我不知道如何在包含所有pdf数据的字符串值中使用它。

 var htmlDoc = new StringBuilder();
 ...
 htmlDoc.AppendLine("<td>");

 // here I need to insert the image
 Stream imgStream = Assembly.GetExecutingAssembly()
  .GetManifestResourceStream("Project.Resources.signature.jpeg"); // is allways null
 or
 htmlDoc.AppendLine("<img src=\"Project.Resources.signature.jpeg\" />");

 htmlDoc.AppendLine("</td>");

 var doc = new HtmlToPdfDocument()
 {
     GlobalSettings = {
         ColorMode = ColorMode.Color,Orientation = Orientation.Landscape,PaperSize = PaperKind.A4Plus,Margins = new MarginSettings() { Top = 10,Left = 30,Right = 30 }
     },Objects = {
         new ObjectSettings() {
             PagesCount = true,HtmlContent = htmlDoc.ToString(),WebSettings = { DefaultEncoding = "utf-8" }
         }
     }
 };

 var pdf = converter.Convert(doc);
 var stream = new MemoryStream(pdf);
 var contentType = @"application/pdf";
 var fileName = $"Invoice_{entity.Id}.pdf";
 stream.Seek(0,SeekOrigin.Begin);
 
 return File(stream,contentType,fileName);

Startup.cs:

services.AddSingleton(typeof(IConverter),new SynchronizedConverter(new PdfTools()));

编辑:我发现绝对路径有效

 htmlDoc.AppendLine("<img src=\"C/User/Application/Project/Resources/signature.jpeg\" />");

但是,这不是最佳解决方案,因为本地环境的绝对路径与生产环境不同。

还有其他想法吗?谢谢

解决方法

Asp.net Core的工作目录不同于Web目录。您需要在项目根目录中添加文件夹 wwwroot 。这是网站的根目录。

enter image description here

在控制器中,通过IWebHostEnvironment获取项目根目录。这是示例。

[ApiController]
[Route("dink/")]
public class DinkController:ControllerBase
{
    IConverter converter;
    public IConfiguration Configuration;
    IWebHostEnvironment webHostEnvironment;
    public DinkController(IConverter _converter,IConfiguration configuration,IWebHostEnvironment environment)
    {
        converter = _converter;
        Configuration = configuration;
        webHostEnvironment = environment;
    }
    //[Route("get")]
    public IActionResult get()
    {
        var htmlDoc = new StringBuilder();
        htmlDoc.AppendLine("<td>");
        var path = webHostEnvironment.WebRootPath+"\\1.png";//It fetches files under wwwroot
        htmlDoc.AppendLine($"<img src=\"{path}\" />");
        htmlDoc.AppendLine("</td>");

        var doc = new HtmlToPdfDocument()
        {
            GlobalSettings = {
                 ColorMode = ColorMode.Color,Orientation = Orientation.Landscape,PaperSize = PaperKind.A4Plus,Margins = new MarginSettings() { Top = 10,Left = 30,Right = 30 }
             },Objects = {
                 new ObjectSettings() {
                     PagesCount = true,HtmlContent = htmlDoc.ToString(),WebSettings = { DefaultEncoding = "utf-8" }
                 }
             }
        };

        var pdf = converter.Convert(doc);
        var stream = new MemoryStream(pdf);
        var contentType = @"application/pdf";
        var fileName = $"Invoice_Id.pdf";
        stream.Seek(0,SeekOrigin.Begin);

        return File(stream,contentType,fileName);
    }
}