Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert Xml To Html To Pdf

Posted on 2014-01-14
3
Medium Priority
?
619 Views
Last Modified: 2014-01-16
I am developing in C# with the 4.0 .Net Framework.  I am needing to take raw XML that a third-party sends us, apply a style sheet (also supplied by this third-party) to the XML, and then take the HTML that is generated from that, and save it as a PDF.

I am using XSLTransform to apply a style sheet to the XML, and get HTML generated.  I have that working.  However, I am struggling with how to convert that HTML to a PDF.  

Any suggestions?  Preferrable, there is already a solution for this by using the 4.0 .Net Framework;  preferrable I will not have to install any new libraries.  

If I can get the HTML to a System.Drawing.Image, I can then save that as a PDF.  We have PDFSharp that I can use for manipulating PDF's.

I have seen a lot of posts on this topic, but most posts that I have found seem older, and require downloading or purchasing new libraries.  I am hoping a more current solution exists.

Here is my code for applying the style sheet to the XML.  I have the HTML available in my XMLWriter, or in my "outputString".  (I am not convinced this is the best way to apply a style sheet to XML, so if there are any suggestions on better ways to do this, those are welcome).

string outputString = string.Empty;

                            XslCompiledTransform xslTransform = new XslCompiledTransform();
                            XsltSettings xsltSettings = new XsltSettings(true, true);
                            XmlUrlResolver resolver = new XmlUrlResolver();
                            xslTransform.Load("./ISOResultStyleSheetFiles/CS_XML_Output.xsl", xsltSettings, resolver);

                            using (StringReader sri = new StringReader(result.Result))
                            {
                                using (XmlReader xri = XmlReader.Create(sri))
                                {
                                    using (StringWriter sw = new StringWriter())
                                    {
                                        using (XmlWriter xwo = XmlWriter.Create(sw, xslTransform.OutputSettings))
                                        {
                                            xslTransform.Transform(xri, xwo);
                                            outputString = sw.ToString();
                                        }
                                    }
                                }
                            }

Open in new window

0
Comment
Question by:farminsure
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39780852
As far as I know there is no built-in functionality to convert xml ot HTML to PDF in .NET.

Therefore you'll need to use some third-party options. I can't see anything wrong with that.
You may try a free tool, e.g.:
https://code.google.com/p/wkhtmltopdf/
0
 

Author Comment

by:farminsure
ID: 39782088
Thanks for the suggestion, but I would prefer to not have to write the html out to a web page prior to converting it to a PDF.  From what I've read bout wkhtmltopdf, it takes the HTML from a URL and converts it to a PDF; but I don't have a URL, I just have the raw HTML in an object.  

Is there a way to convert HTML to an System.Drawing.Image?  If I can get HTML to a Image, then I can convert that image to a PDF using our existing PDFSharp tool.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1500 total points
ID: 39783306
You can do this.

First, enter the following search string "convert html to image" to the "Search Experts Exchange" text box on the top and you'll get some possible solutions, I've checked.

Second, try MS forum solution: http://forums.asp.net/t/1692392.aspx.
Or stackoverflow.com/questions/17832304/convert-html-string-to-image.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question