【问题标题】:DinkToPdf including image in HtmlContentDinkToPdf 包括 HtmlContent 中的图像
【发布时间】:2021-02-10 01:22:28
【问题描述】:

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

现在我需要在 pdf 中插入一个图像作为签名。我创建了我的项目的资源文件夹,并添加了将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\" />");

但是,这并不是最优解,因为本地环境的绝对路径与生产环境不同。

还有什么想法吗?谢谢

【问题讨论】:

  • 我想知道这个转换器(var pdf = converter.Convert(doc))是指哪个包。
  • 我更新了代码。 IConverter 来自 DinkToPdf,在需要的地方注入控制器。谢谢

标签: c# asp.net asp.net-core-webapi dinktopdf


【解决方案1】:

Asp.net Core 的工作目录与 web 目录不同。您需要在项目根目录中添加文件夹wwwroot。它是网络的根目录。

在控制器中,通过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);
    }
}

【讨论】:

  • 感谢您的回答。问题是我需要在服务中使用 DinktoPdf,这是一个外部项目(我的 API 的 dll 依赖项),我不能在那里使用 IWebHostEnvironment,因为 Startup.cs 不存在。
  • 你有 wwwroot 文件夹吗?因为服务仍然可以提供接口,所以你可以在控制器中引用它时向它传递参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-19
  • 2022-08-19
  • 1970-01-01
  • 2013-11-27
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多