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 filepublic 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();
}
}
}