We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Html to PDF not working

Hi im using this code to convert html documents to pdf’s:
doc.Save(htmlStream,SaveFormat.Pdf);
my it doesent handle html files the right way instead i get a pdf with all the html tags. so i guess it handles the document as a txt document instead of a html. Do i need the html package for this to work ?

@henrikJohansen Could you please attach the problematic HTML document here for testing and provide full code you use to convert HTML to PDF? We will check the issue and provide you more information.

here is the code/method that converts the document:

public string ConvertDocumentToPdfBytes(byte[] document)
        {
            using (MemoryStream docStream = new MemoryStream(document))
            {
                Document doc = new Document(docStream);
                
                // Save the document as PDF into a stream.
                using (MemoryStream pdfStream = new MemoryStream())
                {                    
                    doc.Save(pdfStream, SaveFormat.Pdf);
                    // Get base64 string from the resulting stream.
                    string pdfBase64String = Convert.ToBase64String(pdfStream.ToArray());                   
                    return pdfBase64String;
                    
                }
            }
        }

I cant show you the specific file as it holds sensitive information but basically it’s just two pre tags and nothing else no doctype or html tags:

<pre>
txtxtxtxxt
txtxtxtxxtxt
txtxtxtxxt

txtxtxtxt
txtxtxtxxt
txtxtxtxxt

txtxtxxtxt
txtxtxxtxxt
txtxtxxtxxt
</pre>

i guess the question is how can i force the save method to recognize it as html

@henrikJohansen You can explicitly specify the load format in LoadOptions.LoadFormat property. Here is modified code:

public string ConvertDocumentToPdfBytes(byte[] document)
{
    using (MemoryStream docStream = new MemoryStream(document))
    {
        LoadOptions lo = new LoadOptions();
        lo.LoadFormat = LoadFormat.Html;
        Document doc = new Document(docStream, lo);

        // Save the document as PDF into a stream.
        using (MemoryStream pdfStream = new MemoryStream())
        {
            doc.Save(pdfStream, SaveFormat.Pdf);
            // Get base64 string from the resulting stream.
            string pdfBase64String = Convert.ToBase64String(pdfStream.ToArray());
            return pdfBase64String;

        }
    }
}
1 Like