简体   繁体   中英

Convert from html string to pdf using itextsharp in asp.net

I'm using iTextSharp in my project and using this method to parse html string to 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;
        }

And it doesn't work for me. It throws an exception at selected line

URI formats are not supported.

How can I solve this problem?

PS Here is html I need to parse

<!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>

To solve this issue you can add Providers to your HTMLWorker using the SetProviders method which takes an IDictionary the HTMLWorker class has constants for the string keys. In your case have a look at the ILinkProvider and/or IImageProvider. These provide a way to handle the images and URLs inside of your HTML yourself, translating them into usable parts for the PDF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM