DinkToPdf 在 HtmlContent 中包含图像
DinkToPdf including image in HtmlContent
我有一张使用 .Net Core 3 Web API
和 DinkToPdf
库创建的 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.netCore 的工作目录与web 目录不同。您需要在项目根目录下添加文件夹wwwroot。它是网站的根目录。
在controller中,通过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);
}
}
我有一张使用 .Net Core 3 Web API
和 DinkToPdf
库创建的 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.netCore 的工作目录与web 目录不同。您需要在项目根目录下添加文件夹wwwroot。它是网站的根目录。
在controller中,通过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);
}
}