How to convert HTML to PDF including images and CSS styles?

Hi,
We have some HTML with CSS styles and an embedded SVG which we want to convert to PDF.

We are using just creating a new Document:

public void WriteToPDF(Stream streamToWriteTo)
{
    Document document = new Document(new MemoryStream(Encoding.UTF32.GetBytes(_htmlTemplateText)));
    document.Save(streamToWriteTo, SaveFormat.Pdf);
}

This does not render styles or embedded SVG’s however.

Is this just a limitation of the rendering engine?

Is there a list of the limitations of converting HTML to PDF with Aspose somewhere?

Nick

Hi Nick,

Thanks for your inquiry. Could you please attach your input HTML file and output PDF document showing the undesired behaviour here for testing? I will investigate the issue on my side and provide you more information.

Secondly, you can find a list of certain limitations i.e. applied when importing from HTML file and exporting Word document to HTML file in the documentation below:
https://docs.aspose.com/words/net/product-overview/

Best regards,

Awais,

Before I take the time to provide examples, can you confirm whether or not these features are SUPPOSED to be supported? I have read the documentation here: https://docs.aspose.com/words/net/style-features-supported-on-pdf-export/
…and here: https://docs.aspose.com/words/net/drawing-object-features-supported-on-pdf-export/

I read this as when converting HTML to PDF:

  • SVG embedded is NOT supported
  • CSS stylesheets or embedded styles are NOT supported

Is that correct please?

Nick

Hi,

Thanks for your inquiry.

  1. The latest version of Aspose.Words (13.5.0) supports import of SVG images from HTML and preserves these images during rendering HTML to PDF. Internally in Aspose.Words document model the SVG images are converted to PNG images using resolution of 96 dpi. For example, the following HTML mark-up is supported:
<html>
<body>
    <div>
        <h1>Embed SVG code directly into the HTML</h1>
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
        </svg>
    </div>
</body>
</html>
  1. Please note that it is not guaranteed that Aspose.Words generated output PDF document will look exactly the same as the input HTML. This is because Aspose.Words was originally designed to work with Microsoft Word documents and HTML documents are quite different. However, we strive to continuously improve Aspose.Words’ HTML/CSS import engine. Please refer to the following section of the documentation which outlines everything you need to know about Style Features Supported on HTML Import:
    https://docs.aspose.com/words/net/style-features-supported-on-html-import/

Best regards,