0

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?

P.S. 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>
7
  • HTMLWorker is deprecated in favor of XML Worker. That's the Java terminology for saying you're using functionality that is no longer supported because it has been replaced by new functionality. I don't know the corresponding C# terminology. Commented Feb 4, 2013 at 14:07
  • I've tried with new XmlWorker, XmlWorkerHelper, but it doesn't work again. Commented Feb 4, 2013 at 14:09
  • Well, maybe you've hit something that isn't supported (URI formats?) but it's hard to tell what isn't working based on the sole information that "it isn't working." We need more info to know what's going wrong. Commented Feb 4, 2013 at 14:28
  • I've edited my question and added my html too... Hopefully, you can find wrong part there... Commented Feb 4, 2013 at 14:31
  • 1
    That's strange. I can't help you with that until I start writing my next book. Currently I don't have the time for that (I'm rewriting the book about digital signatures). Also SO asks me to "please avoid extended discussions in comments." I guess you'll have to ask somebody else for help. Commented Feb 5, 2013 at 7:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.