Forum How do I...?

Performance improving advice/tips?


We are entering the production phase of a roll-out with Prince converting XHTML documents to PDF format using the .NET interface and we are trying to eke out every bit of performance that we can. Not knowing what goes on inside the Prince engine I'd like some feedback on how we are using Prince to see if the experts can advise on possible performance increases.

The XML document passed into the Render method below is and HTML document that mostly passes the XHTML Transitional validation. The only things that are not compliant are a few attributes we use on a few HTML elements.

Here is our code that interfaces with the Prince engine:

        public MimeStream Render(XmlDocument doc)
            XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
            nsm.AddNamespace("xhtml", "");

            var p = new Prince(princeEnginePath);


            string baseURL = string.Format("file:///{0}/hwxml/hwxml/",
                Path.Combine(this.baseResourcePath, getLocalizationFromDoc(doc, nsm)));
            baseURL = baseURL.Replace("\\", "/");

            MemoryStream outMemStream = new MemoryStream(100000);

            bool result;
                result = p.ConvertString(doc.OuterXml, outMemStream);
            catch (Exception ex)
                throw new PrintException("Exception in Prince PDF rendering engine: " + ex.Message);

            if(result == false)
                throw new PrintException("Error converting document to PDF in Prince rendering engine.");

            outMemStream.Seek(0, SeekOrigin.Begin);
            return new MimeStream("application/pdf", outMemStream);

We do reference about 12 - 15 CSS files from the <head> section of the document. We have considered merging them into one or two but initial tests do not indicate that will buy us much. We are reading the CSS files directly from a local file system so there is no network latency involved in picking up all the various CSS files.

Any advice or tips?

Thank you.

Adrian Pfisterer
Healthwise, Inc.
How big is the HTML document? How many pages does the final PDF have? How long is it taking to produce, and how long do you want it to take? :)
Hi Mike,

An example source HTML document is about 140K. The resulting PDF is about 50 pages.

An upper-end typical use case is in the 50 page region.

The time spent in Prince for a document this size is about 6 seconds on my developer machine (2.5 GHz Intel Core 2 Duo, 4 gig RAM).

How long do I want it to take? As fast as possible using Prince. I'm doing my due diligence here to ensure I'm using the Prince library in the best and fastest possible way. For example, if I had been using one of the overloads to Convert that writes to the file system, then reading the resulting PDF up again to send it back to the client, that would not be the fastest possible way.

Another example: is ConvertMemoryStream faster than ConvertString? Or do you just convert the input memory stream to a string internally anyway? If there are insights to be gleaned by those who can look under the covers that's the kind of thing I'd like to know.

Many thanks...Adrian.
Six seconds sounds a little slow, unless the document is unusually complex. Does it use columns, or have big tables, or many little elements, or something like that?

The best way to see how long it should take to convert is to run Prince from the command line, eg. on Linux or MacOS X you could do this:
$ time prince input.html
Doesn't seem like all that complicated a document to me. I'm happy to email an example to you.

We do put all of the content in a table to be able to dynamically size the header and footer (by placing them in the thead and tfoot elements; see my previous post on that). Would that be slowing it down?

I just instrument the code to see the time; not lucky enough to work on Linux systems. :D
Actually it would be quite handy if you could email me ( a copy of the document and any linked resources like style sheets and images, then I can see exactly how Prince is spending its time during the conversion.