Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1036439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:42:14+00:00 2026-05-16T14:42:14+00:00

So I’m rendering a partial view in memory in order to direct the XSL-FO

  • 0

So I’m rendering a partial view in memory in order to direct the XSL-FO data in the view to a PDF renderer.

The PDF renderer can take a string, XmlDocument or Stream as input.

Here is what I’d like to do: (Edited for clarity)

Solution taken from: http://thriftybliss.spaces.live.com/blog/cns!58DA805F37F31F20!170.entry?wa=wsignin1.0&sa=362921628

var viewPage = new ViewPage();
var viewData = new ViewDataDictionary(model);
viewPage.ViewData = viewData;

var control = viewPage.LoadControl(viewName);
viewPage.Controls.Add(control);

using (var inStream = new MemoryStream())
{
    using (var sw = new StreamWriter(inStream))
    {
        using (var tw = new HtmlTextWriter(sw))
        {
            viewPage.RenderControl(tw);
        }
        using (var outStream = new MemoryStream())
        {
            driver.Render(inStream, outStream);
            return outStream.ToArray();
        }
    }
}

Here’s what works, but what I’d rather not do:

var sb = new StringBuilder();

using (var sw = new StringWriter(sb))
{
    using (var tw = new HtmlTextWriter(sw))
    {
        vp.RenderControl(tw);
    }
}

var xml = new XmlDocument();
xml.LoadXml(sb.ToString());

using (var ms = new MemoryStream())
{
    driver.Render(xml, ms);
    return ms.ToArray();
}

Unfortunately the HtmlTextWriter doesn’t seem to want to write to the StreamWriter, or perhaps I just messed something else up.

If I replace StreamWriter with StringWriter it works fine, but I can’t use StringWriter because when I call .ToString() I get all kinds of escape characters that mess up the XSL-FO and the renderer throws an “Illegal Characters” exception.

I got around this by creating an XmlDocument and loading the string into that, but it seems like a very roundabout way of doing things. I’d really just like to just capture the output from the HtmlTextWriter and feed it directly into the Renderer. Is there any way to do this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:42:15+00:00Added an answer on May 16, 2026 at 2:42 pm

    When you say “HtmlTextWriter doesn’t seem to want to write to the StreamWriter” does that mean that driver.Render can’t get any bytes out of inStream? Two possibilities I can think of…

    First is that HtmlTextWriter isn’t flushing the stream after it’s done writing, in which case you might need a sw.Flush() in there before you hand the inStream to driver.Render.

    Second is that after writing to the MemoryStream, the location pointer will be at the end of the stream, and any attempts to read from it will yield zero bytes (since it’s at the end).

    In that case, setting inStream.Position to 0, or calling inStream.Seek(0, SeekOrigin.Begin) before calling driver.Render should fix it.

    I’ve ran into the second situation a few times before, so that’s my best guess.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.