我使用 HTML Renderer 从 HTML 生成 PDF,所有图像都来自流
var pngBinaryDataLogoLeft = File.ReadAllBytes(folderPath + "logo.png");
var ImgDataURILogoLeft = @"data:image/png;base64," + Convert.ToBase64String(pngBinaryDataLogoLeft);
html = html.Replace("[logoLeft]", ImgDataURILogoLeft);
var pngBinaryDataBGImg = File.ReadAllBytes(folderPath + "background.png");
var ImgDataURIBGImg = @"data:image/png;base64," + Convert.ToBase64String(pngBinaryDataBGImg);
html = html.Replace("[BGImg]", ImgDataURIBGImg);
var config = new PdfGenerateConfig();
config.PageOrientation = PageOrientation.Landscape;
config.PageSize = PdfSharpPageSize.A4;
string cssStr = File.ReadAllText(folderPath + "1.css");
CssData css = PdfGenerator.ParseStyleSheet(cssStr);
PdfSharp.Pdf.PdfDocument pdf = PdfGenerator.GeneratePdf(html, config, css);
MemoryStream stream = new MemoryStream();
pdf.Save(stream, false);
byte[] bytes = stream.ToArray();
File.WriteAllBytes(folderPath + "document.pdf", bytes);
在我的 HTML 中,被流替换的 img 工作正常
<img class="logo-left" src="[logoLeft]" />
但是背景图像不起作用:
<body style="background: url('[BGImg]')">
我没有来自 URL 的图像,因为它来自数据库(保存为流)
我还尝试先将背景图像添加到新的 PDF 页面,如下所示:
XGraphics gfx = XGraphics.FromPdfPage(pdf.Pages[0]);
XImage image = XImage.FromFile(bgImgPath);
gfx.DrawImage(image, x, y, width, height);
但后来我无法将 HTML 中的内容添加到同一页面
也尝试使用iTextSharp,也无法解决。