Handling img tags with relative paths when loading HTML into a Document

I have a string containing HTML that I’m passing to the Document constructor as a stream. This works fine, except the img tags in the HTML are all broken. The img tags use relative paths, not absolute paths. How do I tell the Document where to find the images referenced in the HTML?


Sample code:
/// 
/// Creates a PDF from HTML.
///
/// <param name=“theHTML”>The HTML to convert into a PDF
/// <param name=“theResourcesPath”>The path to the directory containing images, CSS, etc., referenced in the HTML
/// A byte array containing the PDF created from the HTML file
public byte[] ConvertHTMLToPDF(string theHTML, string theResourcesPath) {
	using (MemoryStream streamHTML = new MemoryStream(Encoding.ASCII.GetBytes(theHTML))) {
Document doc = new Document(streamHTML);
using (MemoryStream streamPDF = new MemoryStream()) {
doc.Save(streamPDF, SaveFormat.Pdf);
return streamPDF.GetBuffer();
}
}
}

Hi Steve,


Thanks for your inquiry. I think you need to set the BaseUri in the LoadOptions when loading the HTML document. Please see the API page here for details.

I hope, this will help.

Best Regards,