【发布时间】:2021-02-10 01:22:28
【问题描述】:
我有一张使用 .Net Core 3 Web API 和 DinkToPdf 库创建的 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