Html to PDF: how to include the external images (src="http://...")

I have encourted a problem when converting a HTML to PDF.

My html is : “” which inclue only one external image.

The codes for conversion is :
string html = “”;

var htmlLoadOptions = new HtmlLoadOptions();
htmlLoadOptions.PageInfo.Width = Aspose.Pdf.Generator.PageSize.A4Width;
htmlLoadOptions.PageInfo.Height = Aspose.Pdf.Generator.PageSize.A4Height;
htmlLoadOptions.PageInfo.Margin.Bottom = 40;
htmlLoadOptions.PageInfo.Margin.Top = 80;
htmlLoadOptions.PageInfo.Margin.Left = 0;
htmlLoadOptions.PageInfo.Margin.Right = 0;
using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)), htmlLoadOptions))
{
using (MemoryStream output = new MemoryStream())
{
doc.IsLinearized = false;

	doc.Save(output);
}

}

But the image is lost on the PDF generated. I want to know if ASPOSE can generate a PDF from a HTML which contains external images? Thank you very much.

the html is like : <img src=“http://…/image.jpg”…>

@lucienzhang

Thanks for contacting support.

Aspose.Pdf for .NET supports the feature to load external images while converting HTML into PDF. However you need to specify complete path to image in HtmlLoadOption. Please check following code snippet where I have implemented the functionality as per your requirement.

string html = @"<img src='http://www.greatstone.co.uk/greatstone/uploads/type/aspose_logo_2014_color_print_small.png'>";

var htmlLoadOptions = new HtmlLoadOptions("http://www.greatstone.co.uk/greatstone/uploads/type/");
htmlLoadOptions.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;
htmlLoadOptions.PageInfo.Height = Aspose.Pdf.PageSize.A4.Height;
htmlLoadOptions.PageInfo.Margin.Bottom = 40;
htmlLoadOptions.PageInfo.Margin.Top = 80;
htmlLoadOptions.PageInfo.Margin.Left = 0;
htmlLoadOptions.PageInfo.Margin.Right = 0;
using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)), htmlLoadOptions))
{
  doc.IsLinearized = false;
  doc.Save(dataDir + "PDFWithExtImage.pdf");
}

For your reference, I have attached output, generated by above code snippet as well. Furthermore, I have observed that you are using old Aspose.Pdf.Generator approach in your code, which has been obsoleted in newer versions and it is recommended to use latest version. Please download Aspose.Pdf for .NET 17.8 from our website and in case of any further assistance, please feel free to contact us.

PDFWithExtImage.pdf (76.2 KB)

Hello, thank you very much for your response. But when I use the same codes as you show, I get a empty PDF without any image :frowning:

The applicaiton is a console applicaiton in which we integrate the newest version of ASPOSE as you proposed : I download the newest verison of ASPOSE (17.8.0.0).

We shoud configure some network parameters for it works?

Thank you very much!!!

Could you, please, send me the whole project of your sample codes?

@lucienzhang

Thanks for writing back.

There is no need to configure specific network settings to create the PDF through above shared code snippet. You only need to have a working internet connection or access to image directory if image is present to some physical location in your system. Furthermore, I have attached a sample console application which can be run by including Aspose.Pdf.dll and License file inside bin* folder.

In case if you still face any issue, please share some details regarding your working environment like OS Version, Target Framework, etc. We will again test the scenario in our environment and address it accordingly.

ConsoleApplication1.zip (9.8 KB)

Hello, thank you for your sample project. But I get the same result (empty pdf) even though I use this project. After many test, I noticed that the proxy problem in our network. Could you tell me if there is any proxy parameters to set in Aspose API? There is the log file in Aspose when it can not reach to external images ?

Thank you in advanced for your help.

@lucienzhang

Thanks for writing back.

It seems that your program/application is unable to download data using your network settings. You may please try on some other system with different network settings and share your findings with us.

There are no such options or method available in the API, to change the proxy while downloading. You may write your own C# code to configure valid proxy settings and use it while converting HTML to PDF.

Would you please explain a bit more about this log file? As per my assumptions, you want API to create a log file in case it skips the images from downloading.

PS: Please give try with different image URL, it is quite possible that you are unable to browse specific URL with your specified network settings.