1

我有使用.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);

启动.cs:

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

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

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

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

还有其他想法吗?谢谢

4

1 回答 1

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);
    }
}
于 2020-10-30T02:02:04.667 回答