Free Support Forum - aspose.com

HTML to PDF - Images do not render


#1

I am using the following code to convert from HTML to PDF. Although the PDF is created, no images are rendered. I have tried just about every possible combination of the baseUri and actual image address, but to no avail. The page detailing the conversion to PDF mechanism (HTML to PDF Conversion) does not include examples with images. Nor does it demonstrate the use of the basrUri property (which I’m assuming is what I need to specify)

public Stream Convert(string html, int height, int width, string baseUrl = "")
{
	try
	{
		var options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
		{
			PageSetup =
			{
				AnyPage = new Page(new Size(height, width)),
				AdjustToWidestPage = true
			}
		};

		var output = new MemoryStream();

		using (var stream = new MemoryStream())
		using (var writer = new StreamWriter(stream))
		{
			writer.Write(html);
			writer.Flush();
			stream.Position = 0;
			using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(options, output))
			using (var renderer = new HtmlRenderer())
			using (var document = new HTMLDocument(stream, baseUrl))
			{
				Console.WriteLine(document.Images.Length);
				Console.WriteLine(document.Images[0].Attributes["src"]);
				Console.WriteLine(document.Body.InnerHTML);
				renderer.Render(device, document);
			}

			output.Position = 0;
			return output;
		}
	}
	catch (Exception ex)
	{
		throw new ApplicationException("Error converting to PDF!", ex);
	}
}

#2

@smclean

Thank you for contacting support.

Would you please share source and generated ZIP files with us so that we may try to reproduce and investigate it in our environment.


#3

Thanks for the response.

I have attached a solution file containing a class that performs the conversion and a small test application that generates some very simple HTML and calls the conversion to PDF.

I have included a result PDF file, where you can see the issue (the alt attribute value is rendered, but the image itself is not).

Thank you in advance for taking a look.

DataBank.HtmlConverter.zip (296.3 KB)


#4

@smclean

Thank you for sharing requested data.

We have worked with the data shared by you and have been able to reproduce the issue in our environment. A ticket with ID HTMLNET-1810 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.