Click here to Skip to main content
14,974,357 members
Articles / Web Development / XHTML
Posted 1 Mar 2011


15 bookmarked

How to do xHTML to xHTML Transformations for PDF Conversion Purposes in ASP.NET.

Rate me:
Please Sign up or sign in to vote.
2.00/5 (1 vote)
1 Mar 2011CPOL3 min read
The article discusses dynamic xHtml to xHtml XSL transformations for PDF output purposes.


Converting HTML pages to different formats and especially to PDF has become a widely spread routine for web developers. The process itself is plenty straightforward, because there are quite a lot of PDF development libraries and services around the web. However, one day you may need not just to make a PDF copy of the page, but to automatically add some modifications to the result PDF output (for example, you may want to access SVG data on the page). In this article, I’m going to show a simple example of accomplishing this task in ASP.NET using some .NET and XSL tips and a PD4ML PDF library.

Step 1: Searching for xHTML Markup

ASP.NET is great for easily creating complicated pages. However, all these controls and other stuff have very little in common with result xHTML markup, which is rendered and sent to the client. That’s why the first thing we are going to do is to somehow bring it to the light. The markup is created with the help of “Render” method of the page’s life cycle, so we need to override this method.

protected override void Render(HtmlTextWriter output)
   //Creating String and Html writers to copy the created HTML markup
   StringWriter writer = new StringWriter();
   HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
   //Creating HTML markup with the help of our "fake" HTmlTextWriter
   //Coping the markup to the string and saving it to the disk
   string htmlMarkup = writer.ToString();
   StreamWriter XMLwriter = new StreamWriter(Server.MapPath("Htmloutput.xml"));
   //Creating actual HTML markup for display

Step 2: Getting Ready for XSL Transformation

Now we need to prepare our XSLT file. ASP.NET produces a valid xHTML markup, hence we just need to change it according to our needs, but there are still some problems you may face:

  • First, don’t forget, that xHTML markup uses a default xmlns= namespace, so we need to create some prefix in our XSLT file, to reach the nodes. That’s why we add xmlns:xhtml= string to our XSLT file and add xhtml to ”exclude-result-prefixes” to remove it from the result document.
  • Second, now we are able to do transformations, but there is another problem: lots of xmlns="" nodes in the output document. To get rid of them, add xmlns= to the XSLT file namespace declaration.
  • Third, HTML pages contain plain text, which is not allowed in XML, therefore it’s not processed by XSLT. To get rid of text nodes, put <xsl:template match="xhtml:body//text()"> template in your XSLT style sheet.
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
                   exclude-result-prefixes="msxsl xhtml">
    <!--the rest of the xsl file --!>

Step 3: Creating PDF File

That is, where we come to our final goal. All we need to do is to perform XSL transformation and create PDF file. I‘ll use – PD4ML HTML to PDF converting library, because it’s possible to use it in different programming languages, like Java, PHP, Ruby, etc. I’m going to use MemoryStream because I don’t want to save any intermediate data to hard drive.

protected void MakePDFButton_Click(object sender, EventArgs e)
   //Doing XSL transformation
   string XSLTFile = Server.MapPath("XSLTFile.xslt");
   string XMLFile = Server.MapPath("HTMLoutput.xml");
   // Allowing DTD in our xHTML markup
   XmlReaderSettings settings = new XmlReaderSettings();
   settings.ProhibitDtd = false;
   XmlReader reader = XmlReader.Create(XMLFile, settings);
   //Transforming the initial HTML markup and outputting it to MemoryStream
   //object instance for further PDF conversion
   XslCompiledTransform XSLTransform = new XslCompiledTransform();
   Stream memoryStream = new MemoryStream();
   XSLTransform.Transform(reader, null, memoryStream);
   //Flushing the stream and positioning the cursor at the beginning
   //of the data in the stream.

   //Showing the markup on the page
   StreamReader streamReader=new StreamReader(memoryStream);
   string output=streamReader.ReadToEnd();
   HTMLoutput.Text = Server.HtmlEncode(output);

   //Converting result HTML page to PDF
   PD4ML PDFcreator = new PD4ML();
   PDFcreator.PageSize = PD4Constants.A4;
   PDFcreator.DocumentTitle = "The result PDF file";
   string path=Server.MapPath("Output.pdf");
   StreamWriter streamWriter = new StreamWriter(path);
   memoryStream.Position = 0;
   PDFcreator.render(memoryStream as MemoryStream, streamWriter);
   //Closing all the streams


That's it! Now let's come up with a short summary:

  • Use override “Render” method to manipulate and obtain xHTML markup.
  • Use custom XML namespace prefix to reach non-prefixed xHTML nodes.
  • Use little xslt “xmlns=” hack to get rid of numerous xmlns="" nodes.
  • Use <xsl:template match="xhtml:body//text()"> if you need to get rid of plain text, which isn't wrapped by any element.

I hope that the combination of a valid xHTML markup, which is taken “for granted” by Visual Studio developers and several easy tips, which were described above will give you countless possibilities of manipulating your document's data.


  • 1st March, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael Simonov
United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 2 Pin
conscientious_coder14-Mar-11 3:58
Memberconscientious_coder14-Mar-11 3:58 
Questionwhat about utf8 in PDF Pin
Member 32185759-Mar-11 0:14
MemberMember 32185759-Mar-11 0:14 
QuestionHow long... Pin
Dewey1-Mar-11 15:22
MemberDewey1-Mar-11 15:22 
Generalgood tip Pin
nonintanon1-Mar-11 5:31
Membernonintanon1-Mar-11 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.