C# 在asp.net中使用itextsharp将html字符串转换为pdf

C# 在asp.net中使用itextsharp将html字符串转换为pdf,c#,html,.net,pdf,itextsharp,C#,Html,.net,Pdf,Itextsharp,我在项目中使用iTextSharp,并使用此方法将html字符串解析为pdf public static MemoryStream MakePdf(string htmlCode) { MemoryStream msOutput = new MemoryStream(); TextReader reader = new StringReader(htmlCode); Document document =

我在项目中使用
iTextSharp
,并使用此方法将html字符串解析为pdf

public static MemoryStream MakePdf(string htmlCode)
        {
            MemoryStream msOutput = new MemoryStream();
            TextReader reader = new StringReader(htmlCode);

            Document document = new Document(PageSize.A4, 30, 30, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
            HTMLWorker worker = new HTMLWorker(document);
            document.Open();
            worker.StartDocument();
            worker.Parse(reader); // EXCEPTION IN THIS LINE!!!!
            worker.EndDocument();
            worker.Close();
            document.Close();

            return msOutput;
        }
这对我来说不管用。它在所选行抛出异常

不支持URI格式

我怎样才能解决这个问题

这里是我需要解析的html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="Boomer" />
    <style type="text/css">
        body 
        {
           font: 12px/18px Arial, Tahoma, Verdana, sans-serif;
           width: 100%;
           font-family: Myriad Pro;
           font-style: italic;
           background: #f3f3f3;
        }

        @font-face 
        {
            font-family: 'Myriad Pro';
            src: url('../fonts/myriadpro.eot');
            src: url('../fonts/myriadpro.eot?#iefix') format('embedded-opentype'),
                 url('../fonts/myriadpro.woff') format('woff'),
                 url('../fonts/myriadpro.ttf') format('truetype');
            font-weight: normal;
            font-style: normal;
        }

        .clear
        {
            clear: both;
        }

        a,.blue_text
        {
            color:#2aa2f6;
            text-decoration: none;
        }

        a:hover
        {
            text-decoration: underline;
        }

        .simple_title
        {
           font-size: 22px; 
        }

        .text750
        {
            width: 750px;
            margin: 20px 0;
        }

        .full_width_text
        {
            margin-right: 35px;
            text-align: justify;
        }

        .grey_text 
        {
            color:#afafaf;
        }

        .email_page
        {
            width: 1025px;
            margin: 20px auto;
            background: #fff;
            padding: 20px 0;
        }

        .email_page p
        {
            font-size: 18px;
        }

        .email_div
        {
            margin: 0 auto;
            background: #fff;
            width: 998px;
            border: 1px solid #ebebeb;
            border-radius: 10px;
           -webkit-border-radius:10px;
           -border-radius:10px;
           -moz-border-radius:10px;
           -o-border-radius:10px;
            display: table;
        }

        .email_content 
        {
            margin-left: 60px;
            margin-top: 40px;
        }

        .email_logo
        {
            float: left;
        }

        .email_title
        {
            float: left;
            margin-left: 130px;
            font-size: 22px;
            color:#f7941d;
            margin-top: 50px;
        }

        .email_img_container
        {
            float: right;
            width: 270px;
        }

        .email_img_container .text_description
        {
            font-size: 18px;
            color:#000000;
            margin-bottom: 15px;
        }

        .email_img_container img
        {
            border: 1px solid #ebebeb;
            border-radius: 10px;
           -webkit-border-radius:10px;
           -border-radius:10px;
           -moz-border-radius:10px;
           -o-border-radius:10px;
        }

        .text600
        {
            width: 600px;
            float: left;
        }
    </style>
    <title>Voucher</title>
</head>

<body>
    <div class="email_page">

    <div class="email_div">
        <div class="email_content">
           <div class="text600">
                <div class="email_logo">
                    <a href="#"> 
                        <img src="{EmailLogo}"  />
                    </a>

                </div>
                <div class="email_title">
                    Instant Gift Certificate
                </div>  
                <div class="clear"></div>
                <div>
                    <p>To: <span class="blue_text">{RecipientName}</span></p>
                    <p>
                    <div>{GiftVoucherName}</div>
                    <div>{GiftVoucherDescription}</div>
                    </p>
                    <p>
                        This gift is from:  <span class="blue_text">{SenderName}</span>
                    </p>
                </div>

            </div>

            <div class="email_img_container">
                <div class="text_description">
                    <div>SG Code: {SGCode}</div>
                    <div>Purchased on: {PurchaseDate}</div>
                </div>
                <img src="{MerchantImage}" alt="" />
            </div>
         <div class="clear"></div>
         <div class="text750">
            <p>
                This gift must be redeemed by: <span class="blue_text">{Date}</span><br />
                Redeemable at the following locations: <span class="blue_text">{Locations}</span><br />
                For other details and terms and conditions, please see the other attachment. 



            </p>

            <div class="simple_title">
                Disclaimer
            </div>
            <p>
                Test.com is not responsible for the content of the message or the selection of the gift by the sender.
                Once the sender enters the information, the instant gift is automatically generated and sent to
                the recipient.
            </p>
            <p>
                Visit us at: <a href="http://www.Test.com">www.Test.com</a>
            </p>
         </div>


        </div>


    </div>
</div>

</body>
</html>

身体
{
字体:12px/18px Arial,Tahoma,Verdana,无衬线;
宽度:100%;
字体系列:Myriad Pro;
字体:斜体;
背景:#f3;
}
@字体
{
字体系列:“Myriad Pro”;
src:url('../font/myriadpro.eot');
src:url('../font/myriadpro.eot?#iefix')格式('embedded-opentype'),
url('../fonts/myriadpro.woff')格式('woff'),
url('../fonts/myriadpro.ttf')格式('truetype');
字体大小:正常;
字体风格:普通;
}
清楚的
{
明确:两者皆有;
}
a、 .blue_文本
{
颜色:#2aa2f6;
文字装饰:无;
}
a:悬停
{
文字装饰:下划线;
}
.简单标题
{
字体大小:22px;
}
.text750
{
宽度:750px;
利润率:20px0;
}
.全文
{
右边距:35px;
文本对齐:对齐;
}
.grey_文本
{
颜色:#afafaf;
}
.电子邮件页面
{
宽度:1025px;
保证金:20px自动;
背景:#fff;
填充:20px0;
}
.电邮地址(第p页)
{
字号:18px;
}
.电邮地址
{
保证金:0自动;
背景:#fff;
宽度:998px;
边框:1px实心#ebebebeb;
边界半径:10px;
-webkit边界半径:10px;
-边界半径:10px;
-moz边界半径:10px;
-o-边界半径:10px;
显示:表格;
}
.电子邮件内容
{
左边距:60像素;
边缘顶端:40px;
}
.E.U标志
{
浮动:左;
}
.电邮名称
{
浮动:左;
左边距:130像素;
字体大小:22px;
颜色:#f7941d;
边缘顶部:50px;
}
.email\u img\u容器
{
浮动:对;
宽度:270px;
}
.email\u img\u container.text\u description
{
字号:18px;
颜色:#000000;
边缘底部:15px;
}
.电子邮件\u img\u容器img
{
边框:1px实心#ebebebeb;
边界半径:10px;
-webkit边界半径:10px;
-边界半径:10px;
-moz边界半径:10px;
-o-边界半径:10px;
}
.text600
{
宽度:600px;
浮动:左;
}
代金券
即时礼品券
收件人:{RecipientName}

{GiftVoucherName} {GiftVoucherDescription}

此礼物来自:{SenderName}

SG代码:{SGCode} 购买日期:{PurchaseDate} 此礼物必须在以下日期前赎回:{Date}
可在以下地点兑换:{locations}
有关其他详细信息以及条款和条件,请参阅其他附件。

免责声明 Test.com不对邮件内容或发件人选择的礼物负责。 一旦发送者输入信息,即时礼物将自动生成并发送至 收件人。

访问我们:


要解决此问题,您可以使用
SetProviders
方法将提供程序添加到您的HTMLWorker,该方法采用IDictionary。HTMLWorker类具有字符串键的常量。在您的案例中,请查看ILinkProvider和/或IImageProvider。它们提供了一种自己处理HTML中的图像和URL的方法,将它们转换为PDF的可用部分。

HTMLWorker被弃用,取而代之的是XML Worker。这是Java术语,表示您正在使用不再受支持的功能,因为它已被新功能取代。我不知道对应的C#术语。我尝试过使用新的XmlWorker,XmlWorkerHelper,但它再次不起作用。好吧,也许你遇到了一些不受支持的东西(URI格式?),但很难根据“它不起作用”这一唯一信息来判断什么不起作用我们需要更多的信息来了解哪里出了问题。我已经编辑了我的问题并添加了我的html。。。希望你们能在那个里找到错误的部分……真奇怪。在我开始写下一本书之前,我帮不了你。目前我没有时间(我正在重写关于数字签名的书)。所以我也要求我“请避免在评论中进行长时间的讨论。”我想你必须向其他人寻求帮助。