gpt4 book ai didi

c# - 使用具有包含在 HTML 中的外部 CSS 类的 PdfSharp 从 HTML 片段创建 PDF

转载 作者:太空狗 更新时间:2023-10-29 16:00:12 25 4
gpt4 key购买 nike

我想知道有什么办法可以解决

Create PDF from HTML snippet using PdfSharp having external CSS classes included in the HTML.

我找到了一种使用 HTML 内容生成 pdf 文件的方法,但我的 css 类没有应用到 PDF。

下面是我如何处理简单的 html 字符串:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Drawing;
using TheArtOfDev.HtmlRenderer.PdfSharp;
using PdfSharp;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PdfDocument pdf = PdfGenerator.GeneratePdf("<h1>tests</h1><p>test para</p>", PageSize.A4,20,null,null,null);
pdf.Save(@"C\testPDF.pdf");

}
}

它是这样生成的: enter image description here

但我想要的是像这样包含 HTML 字符串:

<h1>tests</h1><p class='para1'>test para</p>

有什么办法可以实现吗?使用不同的库不是问题,唯一的问题是所使用的库应该是开源的。

最佳答案

您可以将您的样式包含到头部部分,它将正确呈现:

    var testHtml = @"<head>
<style>
.test {
background-color: linen;
color: maroon;
}
</style>
</head>
<body class=""test"">
<p>
<h1>Hello World</h1>
This is html rendered text with css and image.
</p>
<p>
<img src=""http://fc62.deviantart.com/fs11/i/2006/236/d/e/The_Planet_Express_Ship_by_gasclown.jpg"" height=""100"" width=""100"">
</p>
</body>";

此外 - 您只需将 url 添加到样式表就可以了!

关于c# - 使用具有包含在 HTML 中的外部 CSS 类的 PdfSharp 从 HTML 片段创建 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31261981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com