使用asp.net中的itextsharp从html字符串转换为pdf

时间:2013-02-04 11:35:57

标签: 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 = 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格式。

我该如何解决这个问题?

P.S。这是我需要解析的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>

1 个答案:

答案 0 :(得分:1)

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