HTML 渲染器 - 从 HTML 生成 PDF,但无法从流 C#

问题描述

我使用 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,也无法解决

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...